Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

176 lines · 5.3 KiB · Python

Last changed by

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

History of this file

Take it

1from datetime import timedelta23import pytest45import httpx678async def test_get(server, backend):9    url = server.url10    async with httpx.Client() 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.Client() 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.Client() 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.Client() 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.Client() 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.Client() as client:65        async with client.stream("GET", server.url) as response:66            body = await response.read()6768    assert response.status_code == 20069    assert body == b"Hello, world!"70    assert response.content == b"Hello, world!"717273async def test_access_content_stream_response(server, backend):74    async with httpx.Client() 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.content818283async def test_stream_request(server, backend):84    async def hello_world():85        yield b"Hello, "86        yield b"world!"8788    async with httpx.Client() as client:89        response = await client.request("POST", server.url, data=hello_world())90    assert response.status_code == 200919293async def test_raise_for_status(server, backend):94    async with httpx.Client() as client:95        for status_code in (200, 400, 404, 500, 505):96            response = await client.request(97                "GET", server.url.copy_with(path=f"/status/{status_code}")98            )99100            if 400 <= status_code < 600:101                with pytest.raises(httpx.HTTPError) as exc_info:102                    response.raise_for_status()103                assert exc_info.value.response == response104            else:105                assert response.raise_for_status() is None106107108async def test_options(server, backend):109    async with httpx.Client() as client:110        response = await client.options(server.url)111    assert response.status_code == 200112    assert response.text == "Hello, world!"113114115async def test_head(server, backend):116    async with httpx.Client() as client:117        response = await client.head(server.url)118    assert response.status_code == 200119    assert response.text == ""120121122async def test_put(server, backend):123    async with httpx.Client() as client:124        response = await client.put(server.url, data=b"Hello, world!")125    assert response.status_code == 200126127128async def test_patch(server, backend):129    async with httpx.Client() as client:130        response = await client.patch(server.url, data=b"Hello, world!")131    assert response.status_code == 200132133134async def test_delete(server, backend):135    async with httpx.Client() as client:136        response = await client.delete(server.url)137    assert response.status_code == 200138    assert response.text == "Hello, world!"139140141async def test_100_continue(server, backend):142    headers = {"Expect": "100-continue"}143    data = b"Echo request body"144145    async with httpx.Client() as client:146        response = await client.post(147            server.url.copy_with(path="/echo_body"), headers=headers, data=data148        )149150    assert response.status_code == 200151    assert response.content == data152153154async def test_uds(uds_server, backend):155    url = uds_server.url156    uds = uds_server.config.uds157    assert uds is not None158    async with httpx.Client(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"163164165@pytest.mark.parametrize(166    "backend",167    [168        pytest.param("asyncio", marks=pytest.mark.asyncio),169        pytest.param("trio", marks=pytest.mark.trio),170    ],171)172async def test_explicit_backend(server, backend):173    async with httpx.Client(backend=backend) as client:174        response = await client.get(server.url)175    assert response.status_code == 200176    assert response.text == "Hello, world!"