test_client.py
This file
Path
tests/client/test_client.py
194 lines · 5.7 KiB · Python
Last changed by
Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
History of this fileTake it
1from datetime import timedelta2from time import sleep34import pytest56import httpx789@pytest.mark.asyncio10async def test_get(server):11 url = server.url12 async with httpx.Client() as http:13 response = await http.get(url)14 assert response.status_code == 20015 assert response.url == url16 assert response.content == b"Hello, world!"17 assert response.text == "Hello, world!"18 assert response.http_version == "HTTP/1.1"19 assert response.encoding == "iso-8859-1"20 assert response.request.url == url21 assert response.headers22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)252627@pytest.mark.asyncio28async def test_build_request(server):29 url = server.url.copy_with(path="/echo_headers")30 headers = {"Custom-header": "value"}3132 async with httpx.Client() as client:33 request = client.build_request("GET", url)34 request.headers.update(headers)35 response = await client.send(request)3637 assert response.status_code == 20038 assert response.url == url3940 assert response.json()["Custom-header"] == "value"414243@pytest.mark.asyncio44async def test_post(server):45 async with httpx.Client() as client:46 response = await client.post(server.url, data=b"Hello, world!")47 assert response.status_code == 20048 assert response.reason_phrase == "OK"495051@pytest.mark.asyncio52async def test_post_json(server):53 async with httpx.Client() as client:54 response = await client.post(server.url, json={"text": "Hello, world!"})55 assert response.status_code == 20056 assert response.reason_phrase == "OK"575859@pytest.mark.asyncio60async def test_stream_response(server):61 async with httpx.Client() as client:62 async with client.stream("GET", server.url) as response:63 content = await response.read()64 assert response.status_code == 20065 assert content == b"Hello, world!"666768@pytest.mark.asyncio69async def test_stream_iterator(server):70 body = b""7172 async with httpx.Client() as client:73 async with client.stream("GET", server.url) as response:74 async for chunk in response.aiter_bytes():75 body += chunk7677 assert response.status_code == 20078 assert body == b"Hello, world!"798081@pytest.mark.asyncio82async def test_raw_iterator(server):83 body = b""8485 async with httpx.Client() as client:86 async with client.stream("GET", server.url) as response:87 async for chunk in response.aiter_raw():88 body += chunk8990 assert response.status_code == 20091 assert body == b"Hello, world!"929394@pytest.mark.asyncio95async def test_raise_for_status(server):96 async with httpx.Client() as client:97 for status_code in (200, 400, 404, 500, 505):98 response = await client.request(99 "GET", server.url.copy_with(path="/status/{}".format(status_code))100 )101 if 400 <= status_code < 600:102 with pytest.raises(httpx.HTTPError) as exc_info:103 response.raise_for_status()104 assert exc_info.value.response == response105 else:106 assert response.raise_for_status() is None107108109@pytest.mark.asyncio110async def test_options(server):111 async with httpx.Client() as client:112 response = await client.options(server.url)113 assert response.status_code == 200114 assert response.reason_phrase == "OK"115116117@pytest.mark.asyncio118async def test_head(server):119 async with httpx.Client() as client:120 response = await client.head(server.url)121 assert response.status_code == 200122 assert response.reason_phrase == "OK"123124125@pytest.mark.asyncio126async def test_put(server):127 async with httpx.Client() as client:128 response = await client.put(server.url, data=b"Hello, world!")129 assert response.status_code == 200130 assert response.reason_phrase == "OK"131132133@pytest.mark.asyncio134async def test_patch(server):135 async with httpx.Client() as client:136 response = await client.patch(server.url, data=b"Hello, world!")137 assert response.status_code == 200138 assert response.reason_phrase == "OK"139140141@pytest.mark.asyncio142async def test_delete(server):143 async with httpx.Client() as client:144 response = await client.delete(server.url)145 assert response.status_code == 200146 assert response.reason_phrase == "OK"147148149@pytest.mark.asyncio150async def test_base_url(server):151 base_url = server.url152 async with httpx.Client(base_url=base_url) as client:153 response = await client.get("/")154 assert response.status_code == 200155 assert response.url == base_url156157158@pytest.mark.asyncio159async def test_uds(uds_server):160 url = uds_server.url161 uds = uds_server.config.uds162 assert uds is not None163 async with httpx.Client(uds=uds) as client:164 response = await client.get(url)165 assert response.status_code == 200166 assert response.text == "Hello, world!"167 assert response.encoding == "iso-8859-1"168169170def test_merge_url():171 client = httpx.Client(base_url="https://www.paypal.com/")172 url = client.merge_url("http://www.paypal.com")173174 assert url.scheme == "https"175 assert url.is_ssl176177178@pytest.mark.asyncio179async def test_elapsed_delay(server):180 url = server.url.copy_with(path="/slow_response/100")181 async with httpx.Client() as client:182 response = await client.get(url)183 assert response.elapsed.total_seconds() == pytest.approx(0.1, rel=0.2)184185186@pytest.mark.asyncio187async def test_elapsed_delay_ignores_read_time(server):188 url = server.url.copy_with(path="/slow_response/100")189 async with httpx.Client() as client:190 async with client.stream("GET", url) as response:191 sleep(0.2)192 await response.read()193194 assert response.elapsed.total_seconds() == pytest.approx(0.1, rel=0.2)