test_async_client.py
This file
Path
tests/client/test_async_client.py
184 lines · 5.7 KiB · Python
Last changed by
Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
History of this fileTake it
1from datetime import timedelta23import httpcore4import pytest56import httpx7from httpx import ASGIDispatch8910@pytest.mark.usefixtures("async_environment")11async def test_get(server):12 url = server.url13 async with httpx.AsyncClient() as client:14 response = await client.get(url)15 assert response.status_code == 20016 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"18 assert response.headers19 assert repr(response) == "<Response [200 OK]>"20 assert response.elapsed > timedelta(seconds=0)212223@pytest.mark.usefixtures("async_environment")24async def test_get_invalid_url(server):25 async with httpx.AsyncClient() as client:26 with pytest.raises(httpx.InvalidURL):27 await client.get("invalid://example.org")282930@pytest.mark.usefixtures("async_environment")31async def test_build_request(server):32 url = server.url.copy_with(path="/echo_headers")33 headers = {"Custom-header": "value"}34 async with httpx.AsyncClient() as client:35 request = client.build_request("GET", url)36 request.headers.update(headers)37 response = await client.send(request)3839 assert response.status_code == 20040 assert response.url == url4142 assert response.json()["Custom-header"] == "value"434445@pytest.mark.usefixtures("async_environment")46async def test_post(server):47 url = server.url48 async with httpx.AsyncClient() as client:49 response = await client.post(url, data=b"Hello, world!")50 assert response.status_code == 200515253@pytest.mark.usefixtures("async_environment")54async def test_post_json(server):55 url = server.url56 async with httpx.AsyncClient() as client:57 response = await client.post(url, json={"text": "Hello, world!"})58 assert response.status_code == 200596061@pytest.mark.usefixtures("async_environment")62async def test_stream_response(server):63 async with httpx.AsyncClient() as client:64 async with client.stream("GET", server.url) as response:65 body = await response.aread()6667 assert response.status_code == 20068 assert body == b"Hello, world!"69 assert response.content == b"Hello, world!"707172@pytest.mark.usefixtures("async_environment")73async def test_access_content_stream_response(server):74 async with httpx.AsyncClient() as client:75 async with client.stream("GET", server.url) as response:76 pass7778 assert response.status_code == 20079 with pytest.raises(httpx.ResponseNotRead):80 response.content818283@pytest.mark.usefixtures("async_environment")84async def test_stream_request(server):85 async def hello_world():86 yield b"Hello, "87 yield b"world!"8889 async with httpx.AsyncClient() as client:90 response = await client.request("POST", server.url, data=hello_world())91 assert response.status_code == 200929394@pytest.mark.usefixtures("async_environment")95async def test_raise_for_status(server):96 async with httpx.AsyncClient() as client:97 for status_code in (200, 400, 404, 500, 505):98 response = await client.request(99 "GET", server.url.copy_with(path=f"/status/{status_code}")100 )101102 if 400 <= status_code < 600:103 with pytest.raises(httpx.HTTPError) as exc_info:104 response.raise_for_status()105 assert exc_info.value.response == response106 else:107 assert response.raise_for_status() is None # type: ignore108109110@pytest.mark.usefixtures("async_environment")111async def test_options(server):112 async with httpx.AsyncClient() as client:113 response = await client.options(server.url)114 assert response.status_code == 200115 assert response.text == "Hello, world!"116117118@pytest.mark.usefixtures("async_environment")119async def test_head(server):120 async with httpx.AsyncClient() as client:121 response = await client.head(server.url)122 assert response.status_code == 200123 assert response.text == ""124125126@pytest.mark.usefixtures("async_environment")127async def test_put(server):128 async with httpx.AsyncClient() as client:129 response = await client.put(server.url, data=b"Hello, world!")130 assert response.status_code == 200131132133@pytest.mark.usefixtures("async_environment")134async def test_patch(server):135 async with httpx.AsyncClient() as client:136 response = await client.patch(server.url, data=b"Hello, world!")137 assert response.status_code == 200138139140@pytest.mark.usefixtures("async_environment")141async def test_delete(server):142 async with httpx.AsyncClient() as client:143 response = await client.delete(server.url)144 assert response.status_code == 200145 assert response.text == "Hello, world!"146147148@pytest.mark.usefixtures("async_environment")149async def test_100_continue(server):150 headers = {"Expect": "100-continue"}151 data = b"Echo request body"152153 async with httpx.AsyncClient() as client:154 response = await client.post(155 server.url.copy_with(path="/echo_body"), headers=headers, data=data156 )157158 assert response.status_code == 200159 assert response.content == data160161162def test_dispatch_deprecated():163 dispatch = httpcore.AsyncHTTPTransport()164165 with pytest.warns(DeprecationWarning) as record:166 client = httpx.AsyncClient(dispatch=dispatch)167168 assert client.transport is dispatch169 assert len(record) == 1170 assert record[0].message.args[0] == (171 "The dispatch argument is deprecated since v0.13 and will be "172 "removed in a future release, please use 'transport'"173 )174175176def test_asgi_dispatch_deprecated():177 with pytest.warns(DeprecationWarning) as record:178 ASGIDispatch(None)179180 assert len(record) == 1181 assert (182 record[0].message.args[0]183 == "ASGIDispatch is deprecated, please use ASGITransport"184 )