Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

168 lines · 5.3 KiB · Python

Last changed by

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

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(http2=True) 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)1920    with pytest.raises(httpx.NotRedirectResponse):21        await response.anext()222324@pytest.mark.parametrize(25    "url",26    [27        pytest.param("invalid://example.org", id="scheme-not-http(s)"),28        pytest.param("://example.org", id="no-scheme"),29        pytest.param("http://", id="no-host"),30    ],31)32@pytest.mark.usefixtures("async_environment")33async def test_get_invalid_url(server, url):34    async with httpx.AsyncClient() as client:35        with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):36            await client.get(url)373839@pytest.mark.usefixtures("async_environment")40async def test_build_request(server):41    url = server.url.copy_with(path="/echo_headers")42    headers = {"Custom-header": "value"}43    async with httpx.AsyncClient() as client:44        request = client.build_request("GET", url)45        request.headers.update(headers)46        response = await client.send(request)4748    assert response.status_code == 20049    assert response.url == url5051    assert response.json()["Custom-header"] == "value"525354@pytest.mark.usefixtures("async_environment")55async def test_post(server):56    url = server.url57    async with httpx.AsyncClient() as client:58        response = await client.post(url, data=b"Hello, world!")59    assert response.status_code == 200606162@pytest.mark.usefixtures("async_environment")63async def test_post_json(server):64    url = server.url65    async with httpx.AsyncClient() as client:66        response = await client.post(url, json={"text": "Hello, world!"})67    assert response.status_code == 200686970@pytest.mark.usefixtures("async_environment")71async def test_stream_response(server):72    async with httpx.AsyncClient() as client:73        async with client.stream("GET", server.url) as response:74            body = await response.aread()7576    assert response.status_code == 20077    assert body == b"Hello, world!"78    assert response.content == b"Hello, world!"798081@pytest.mark.usefixtures("async_environment")82async def test_access_content_stream_response(server):83    async with httpx.AsyncClient() as client:84        async with client.stream("GET", server.url) as response:85            pass8687    assert response.status_code == 20088    with pytest.raises(httpx.ResponseNotRead):89        response.content909192@pytest.mark.usefixtures("async_environment")93async def test_stream_request(server):94    async def hello_world():95        yield b"Hello, "96        yield b"world!"9798    async with httpx.AsyncClient() as client:99        response = await client.request("POST", server.url, data=hello_world())100    assert response.status_code == 200101102103@pytest.mark.usefixtures("async_environment")104async def test_raise_for_status(server):105    async with httpx.AsyncClient() as client:106        for status_code in (200, 400, 404, 500, 505):107            response = await client.request(108                "GET", server.url.copy_with(path=f"/status/{status_code}")109            )110111            if 400 <= status_code < 600:112                with pytest.raises(httpx.HTTPStatusError) as exc_info:113                    response.raise_for_status()114                assert exc_info.value.response == response115            else:116                assert response.raise_for_status() is None  # type: ignore117118119@pytest.mark.usefixtures("async_environment")120async def test_options(server):121    async with httpx.AsyncClient() as client:122        response = await client.options(server.url)123    assert response.status_code == 200124    assert response.text == "Hello, world!"125126127@pytest.mark.usefixtures("async_environment")128async def test_head(server):129    async with httpx.AsyncClient() as client:130        response = await client.head(server.url)131    assert response.status_code == 200132    assert response.text == ""133134135@pytest.mark.usefixtures("async_environment")136async def test_put(server):137    async with httpx.AsyncClient() as client:138        response = await client.put(server.url, data=b"Hello, world!")139    assert response.status_code == 200140141142@pytest.mark.usefixtures("async_environment")143async def test_patch(server):144    async with httpx.AsyncClient() as client:145        response = await client.patch(server.url, data=b"Hello, world!")146    assert response.status_code == 200147148149@pytest.mark.usefixtures("async_environment")150async def test_delete(server):151    async with httpx.AsyncClient() as client:152        response = await client.delete(server.url)153    assert response.status_code == 200154    assert response.text == "Hello, world!"155156157@pytest.mark.usefixtures("async_environment")158async def test_100_continue(server):159    headers = {"Expect": "100-continue"}160    data = b"Echo request body"161162    async with httpx.AsyncClient() as client:163        response = await client.post(164            server.url.copy_with(path="/echo_body"), headers=headers, data=data165        )166167    assert response.status_code == 200168    assert response.content == data