Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

182 lines · 5.4 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.asyncio9async def test_get(server):10    url = server.url11    async with httpx.AsyncClient() as http:12        response = await http.get(url)13    assert response.status_code == 20014    assert response.url == url15    assert response.content == b"Hello, world!"16    assert response.text == "Hello, world!"17    assert response.http_version == "HTTP/1.1"18    assert response.encoding == "iso-8859-1"19    assert response.request.url == url20    assert response.headers21    assert response.is_redirect is False22    assert repr(response) == "<Response [200 OK]>"23    assert response.elapsed > timedelta(0)242526@pytest.mark.asyncio27async def test_build_request(server):28    url = server.url.copy_with(path="/echo_headers")29    headers = {"Custom-header": "value"}3031    async with httpx.AsyncClient() as client:32        request = client.build_request("GET", url)33        request.headers.update(headers)34        response = await client.send(request)3536    assert response.status_code == 20037    assert response.url == url3839    assert response.json()["Custom-header"] == "value"404142@pytest.mark.asyncio43async def test_post(server):44    async with httpx.AsyncClient() as client:45        response = await client.post(server.url, data=b"Hello, world!")46    assert response.status_code == 20047    assert response.reason_phrase == "OK"484950@pytest.mark.asyncio51async def test_post_json(server):52    async with httpx.AsyncClient() as client:53        response = await client.post(server.url, json={"text": "Hello, world!"})54    assert response.status_code == 20055    assert response.reason_phrase == "OK"565758@pytest.mark.asyncio59async def test_stream_response(server):60    async with httpx.AsyncClient() as client:61        async with client.stream("GET", server.url) as response:62            content = await response.aread()63    assert response.status_code == 20064    assert content == b"Hello, world!"656667@pytest.mark.asyncio68async def test_stream_iterator(server):69    body = b""7071    async with httpx.AsyncClient() as client:72        async with client.stream("GET", server.url) as response:73            async for chunk in response.aiter_bytes():74                body += chunk7576    assert response.status_code == 20077    assert body == b"Hello, world!"787980@pytest.mark.asyncio81async def test_raw_iterator(server):82    body = b""8384    async with httpx.AsyncClient() as client:85        async with client.stream("GET", server.url) as response:86            async for chunk in response.aiter_raw():87                body += chunk8889    assert response.status_code == 20090    assert body == b"Hello, world!"919293@pytest.mark.asyncio94async def test_raise_for_status(server):95    async with httpx.AsyncClient() as client:96        for status_code in (200, 400, 404, 500, 505):97            response = await client.request(98                "GET", server.url.copy_with(path="/status/{}".format(status_code))99            )100            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 None106107108@pytest.mark.asyncio109async 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.reason_phrase == "OK"114115116@pytest.mark.asyncio117async 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.reason_phrase == "OK"122123124@pytest.mark.asyncio125async 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 == 200129    assert response.reason_phrase == "OK"130131132@pytest.mark.asyncio133async def test_patch(server):134    async with httpx.AsyncClient() as client:135        response = await client.patch(server.url, data=b"Hello, world!")136    assert response.status_code == 200137    assert response.reason_phrase == "OK"138139140@pytest.mark.asyncio141async 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.reason_phrase == "OK"146147148@pytest.mark.asyncio149async def test_base_url(server):150    base_url = server.url151    async with httpx.AsyncClient(base_url=base_url) as client:152        response = await client.get("/")153    assert response.status_code == 200154    assert response.url == base_url155156157@pytest.mark.asyncio158async def test_uds(uds_server):159    url = uds_server.url160    uds = uds_server.config.uds161    assert uds is not None162    async with httpx.AsyncClient(uds=uds) as client:163        response = await client.get(url)164    assert response.status_code == 200165    assert response.text == "Hello, world!"166    assert response.encoding == "iso-8859-1"167168169def test_merge_url():170    client = httpx.AsyncClient(base_url="https://www.paypal.com/")171    url = client.merge_url("http://www.paypal.com")172173    assert url.scheme == "https"174    assert url.is_ssl175176177@pytest.mark.asyncio178async def test_elapsed_delay(server):179    url = server.url.copy_with(path="/slow_response/100")180    async with httpx.AsyncClient() as client:181        response = await client.get(url)182    assert response.elapsed.total_seconds() > 0.0