Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

157 lines · 4.9 KiB · Python

Last changed by

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

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_get_invalid_url(server):23    async with httpx.AsyncClient() as client:24        with pytest.raises(httpx.InvalidURL):25            await client.get("invalid://example.org")262728@pytest.mark.usefixtures("async_environment")29async def test_build_request(server):30    url = server.url.copy_with(path="/echo_headers")31    headers = {"Custom-header": "value"}32    async with httpx.AsyncClient() as client:33        request = client.build_request("GET", url)34        request.headers.update(headers)35        response = await client.send(request)3637    assert response.status_code == 20038    assert response.url == url3940    assert response.json()["Custom-header"] == "value"414243@pytest.mark.usefixtures("async_environment")44async def test_post(server):45    url = server.url46    async with httpx.AsyncClient() as client:47        response = await client.post(url, data=b"Hello, world!")48    assert response.status_code == 200495051@pytest.mark.usefixtures("async_environment")52async def test_post_json(server):53    url = server.url54    async with httpx.AsyncClient() as client:55        response = await client.post(url, json={"text": "Hello, world!"})56    assert response.status_code == 200575859@pytest.mark.usefixtures("async_environment")60async def test_stream_response(server):61    async with httpx.AsyncClient() as client:62        async with client.stream("GET", server.url) as response:63            body = await response.aread()6465    assert response.status_code == 20066    assert body == b"Hello, world!"67    assert response.content == b"Hello, world!"686970@pytest.mark.usefixtures("async_environment")71async def test_access_content_stream_response(server):72    async with httpx.AsyncClient() as client:73        async with client.stream("GET", server.url) as response:74            pass7576    assert response.status_code == 20077    with pytest.raises(httpx.ResponseNotRead):78        response.content798081@pytest.mark.usefixtures("async_environment")82async def test_stream_request(server):83    async def hello_world():84        yield b"Hello, "85        yield b"world!"8687    async with httpx.AsyncClient() as client:88        response = await client.request("POST", server.url, data=hello_world())89    assert response.status_code == 200909192@pytest.mark.usefixtures("async_environment")93async def test_raise_for_status(server):94    async with httpx.AsyncClient() 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 None  # type: ignore106107108@pytest.mark.usefixtures("async_environment")109async def test_options(server):110    async with httpx.AsyncClient() as client:111        response = await client.options(server.url)112    assert response.status_code == 200113    assert response.text == "Hello, world!"114115116@pytest.mark.usefixtures("async_environment")117async def test_head(server):118    async with httpx.AsyncClient() as client:119        response = await client.head(server.url)120    assert response.status_code == 200121    assert response.text == ""122123124@pytest.mark.usefixtures("async_environment")125async def test_put(server):126    async with httpx.AsyncClient() as client:127        response = await client.put(server.url, data=b"Hello, world!")128    assert response.status_code == 200129130131@pytest.mark.usefixtures("async_environment")132async def test_patch(server):133    async with httpx.AsyncClient() as client:134        response = await client.patch(server.url, data=b"Hello, world!")135    assert response.status_code == 200136137138@pytest.mark.usefixtures("async_environment")139async def test_delete(server):140    async with httpx.AsyncClient() as client:141        response = await client.delete(server.url)142    assert response.status_code == 200143    assert response.text == "Hello, world!"144145146@pytest.mark.usefixtures("async_environment")147async def test_100_continue(server):148    headers = {"Expect": "100-continue"}149    data = b"Echo request body"150151    async with httpx.AsyncClient() as client:152        response = await client.post(153            server.url.copy_with(path="/echo_body"), headers=headers, data=data154        )155156    assert response.status_code == 200157    assert response.content == data