test_async_client.py
This file
Path
tests/client/test_async_client.py
148 lines · 4.8 KiB · Python
Last changed by
Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256
History of this fileTake it
1from datetime import timedelta23import pytest45import httpx678async def test_get(server, backend):9 url = server.url10 async with httpx.AsyncClient(backend=backend) as client:11 response = await client.get(url)12 assert response.status_code == 20013 assert response.text == "Hello, world!"14 assert response.http_version == "HTTP/1.1"15 assert response.headers16 assert repr(response) == "<Response [200 OK]>"17 assert response.elapsed > timedelta(seconds=0)181920async def test_build_request(server, backend):21 url = server.url.copy_with(path="/echo_headers")22 headers = {"Custom-header": "value"}23 async with httpx.AsyncClient(backend=backend) as client:24 request = client.build_request("GET", url)25 request.headers.update(headers)26 response = await client.send(request)2728 assert response.status_code == 20029 assert response.url == url3031 assert response.json()["Custom-header"] == "value"323334@pytest.mark.asyncio35async def test_get_no_backend(server):36 """37 Verify that the client is capable of making a simple request if not given a backend.38 """39 url = server.url40 async with httpx.AsyncClient() as client:41 response = await client.get(url)42 assert response.status_code == 20043 assert response.text == "Hello, world!"44 assert response.http_version == "HTTP/1.1"45 assert response.headers46 assert repr(response) == "<Response [200 OK]>"474849async def test_post(server, backend):50 url = server.url51 async with httpx.AsyncClient(backend=backend) as client:52 response = await client.post(url, data=b"Hello, world!")53 assert response.status_code == 200545556async def test_post_json(server, backend):57 url = server.url58 async with httpx.AsyncClient(backend=backend) as client:59 response = await client.post(url, json={"text": "Hello, world!"})60 assert response.status_code == 200616263async def test_stream_response(server, backend):64 async with httpx.AsyncClient(backend=backend) as client:65 response = await client.request("GET", server.url, stream=True)66 assert response.status_code == 20067 body = await response.read()68 assert body == b"Hello, world!"69 assert response.content == b"Hello, world!"707172async def test_access_content_stream_response(server, backend):73 async with httpx.AsyncClient(backend=backend) as client:74 response = await client.request("GET", server.url, stream=True)75 assert response.status_code == 20076 with pytest.raises(httpx.ResponseNotRead):77 response.content787980async def test_stream_request(server, backend):81 async def hello_world():82 yield b"Hello, "83 yield b"world!"8485 async with httpx.AsyncClient(backend=backend) as client:86 response = await client.request("POST", server.url, data=hello_world())87 assert response.status_code == 200888990async def test_raise_for_status(server, backend):91 async with httpx.AsyncClient(backend=backend) as client:92 for status_code in (200, 400, 404, 500, 505):93 response = await client.request(94 "GET", server.url.copy_with(path=f"/status/{status_code}")95 )9697 if 400 <= status_code < 600:98 with pytest.raises(httpx.HTTPError) as exc_info:99 response.raise_for_status()100 assert exc_info.value.response == response101 else:102 assert response.raise_for_status() is None103104105async def test_options(server, backend):106 async with httpx.AsyncClient(backend=backend) as client:107 response = await client.options(server.url)108 assert response.status_code == 200109 assert response.text == "Hello, world!"110111112async def test_head(server, backend):113 async with httpx.AsyncClient(backend=backend) as client:114 response = await client.head(server.url)115 assert response.status_code == 200116 assert response.text == ""117118119async def test_put(server, backend):120 async with httpx.AsyncClient(backend=backend) as client:121 response = await client.put(server.url, data=b"Hello, world!")122 assert response.status_code == 200123124125async def test_patch(server, backend):126 async with httpx.AsyncClient(backend=backend) as client:127 response = await client.patch(server.url, data=b"Hello, world!")128 assert response.status_code == 200129130131async def test_delete(server, backend):132 async with httpx.AsyncClient(backend=backend) as client:133 response = await client.delete(server.url)134 assert response.status_code == 200135 assert response.text == "Hello, world!"136137138async def test_100_continue(server, backend):139 headers = {"Expect": "100-continue"}140 data = b"Echo request body"141142 async with httpx.AsyncClient(backend=backend) as client:143 response = await client.post(144 server.url.copy_with(path="/echo_body"), headers=headers, data=data145 )146147 assert response.status_code == 200148 assert response.content == data