Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

169 lines · 5.3 KiB · Python

Last changed by

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

History of this file

Take it

1from datetime import timedelta23import pytest45import httpx678@pytest.mark.usefixtures("async_environment")9async def test_get(server):10    url = server.url11    async with httpx.AsyncClient() as client:12        response = await client.get(url)13    assert response.status_code == 20014    assert response.text == "Hello, world!"15    assert response.http_version == "HTTP/1.1"16    assert response.headers17    assert repr(response) == "<Response [200 OK]>"18    assert response.elapsed > timedelta(seconds=0)192021@pytest.mark.usefixtures("async_environment")22async def test_build_request(server):23    url = server.url.copy_with(path="/echo_headers")24    headers = {"Custom-header": "value"}25    async with httpx.AsyncClient() as client:26        request = client.build_request("GET", url)27        request.headers.update(headers)28        response = await client.send(request)2930    assert response.status_code == 20031    assert response.url == url3233    assert response.json()["Custom-header"] == "value"343536@pytest.mark.usefixtures("async_environment")37async def test_post(server):38    url = server.url39    async with httpx.AsyncClient() as client:40        response = await client.post(url, data=b"Hello, world!")41    assert response.status_code == 200424344@pytest.mark.usefixtures("async_environment")45async def test_post_json(server):46    url = server.url47    async with httpx.AsyncClient() as client:48        response = await client.post(url, json={"text": "Hello, world!"})49    assert response.status_code == 200505152@pytest.mark.usefixtures("async_environment")53async def test_stream_response(server):54    async with httpx.AsyncClient() as client:55        async with client.stream("GET", server.url) as response:56            body = await response.aread()5758    assert response.status_code == 20059    assert body == b"Hello, world!"60    assert response.content == b"Hello, world!"616263@pytest.mark.usefixtures("async_environment")64async def test_access_content_stream_response(server):65    async with httpx.AsyncClient() as client:66        async with client.stream("GET", server.url) as response:67            pass6869    assert response.status_code == 20070    with pytest.raises(httpx.ResponseNotRead):71        response.content727374@pytest.mark.usefixtures("async_environment")75async def test_stream_request(server):76    async def hello_world():77        yield b"Hello, "78        yield b"world!"7980    async with httpx.AsyncClient() as client:81        response = await client.request("POST", server.url, data=hello_world())82    assert response.status_code == 200838485@pytest.mark.usefixtures("async_environment")86async def test_raise_for_status(server):87    async with httpx.AsyncClient() as client:88        for status_code in (200, 400, 404, 500, 505):89            response = await client.request(90                "GET", server.url.copy_with(path=f"/status/{status_code}")91            )9293            if 400 <= status_code < 600:94                with pytest.raises(httpx.HTTPError) as exc_info:95                    response.raise_for_status()96                assert exc_info.value.response == response97            else:98                assert response.raise_for_status() is None99100101@pytest.mark.usefixtures("async_environment")102async def test_options(server):103    async with httpx.AsyncClient() as client:104        response = await client.options(server.url)105    assert response.status_code == 200106    assert response.text == "Hello, world!"107108109@pytest.mark.usefixtures("async_environment")110async def test_head(server):111    async with httpx.AsyncClient() as client:112        response = await client.head(server.url)113    assert response.status_code == 200114    assert response.text == ""115116117@pytest.mark.usefixtures("async_environment")118async def test_put(server):119    async with httpx.AsyncClient() as client:120        response = await client.put(server.url, data=b"Hello, world!")121    assert response.status_code == 200122123124@pytest.mark.usefixtures("async_environment")125async def test_patch(server):126    async with httpx.AsyncClient() as client:127        response = await client.patch(server.url, data=b"Hello, world!")128    assert response.status_code == 200129130131@pytest.mark.usefixtures("async_environment")132async def test_delete(server):133    async with httpx.AsyncClient() as client:134        response = await client.delete(server.url)135    assert response.status_code == 200136    assert response.text == "Hello, world!"137138139@pytest.mark.usefixtures("async_environment")140async def test_100_continue(server):141    headers = {"Expect": "100-continue"}142    data = b"Echo request body"143144    async with httpx.AsyncClient() as client:145        response = await client.post(146            server.url.copy_with(path="/echo_body"), headers=headers, data=data147        )148149    assert response.status_code == 200150    assert response.content == data151152153@pytest.mark.usefixtures("async_environment")154async def test_uds(uds_server):155    url = uds_server.url156    uds = uds_server.config.uds157    assert uds is not None158    async with httpx.AsyncClient(uds=uds) as client:159        response = await client.get(url)160    assert response.status_code == 200161    assert response.text == "Hello, world!"162    assert response.encoding == "iso-8859-1"163164165async def test_explicit_backend(server, async_environment):166    async with httpx.AsyncClient(backend=async_environment) as client:167        response = await client.get(server.url)168    assert response.status_code == 200169    assert response.text == "Hello, world!"