Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

145 lines · 4.8 KiB · Python

Last changed by

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

History of this file

Take it

1import pytest23import httpx456async def test_get(server, backend):7    url = server.url8    async with httpx.AsyncClient(backend=backend) as client:9        response = await client.get(url)10    assert response.status_code == 20011    assert response.text == "Hello, world!"12    assert response.http_version == "HTTP/1.1"13    assert response.headers14    assert repr(response) == "<Response [200 OK]>"151617async def test_build_request(server, backend):18    url = server.url.copy_with(path="/echo_headers")19    headers = {"Custom-header": "value"}20    async with httpx.AsyncClient(backend=backend) as client:21        request = client.build_request("GET", url)22        request.headers.update(headers)23        response = await client.send(request)2425    assert response.status_code == 20026    assert response.url == url2728    assert response.json()["Custom-header"] == "value"293031@pytest.mark.asyncio32async def test_get_no_backend(server):33    """34    Verify that the client is capable of making a simple request if not given a backend.35    """36    url = server.url37    async with httpx.AsyncClient() as client:38        response = await client.get(url)39    assert response.status_code == 20040    assert response.text == "Hello, world!"41    assert response.http_version == "HTTP/1.1"42    assert response.headers43    assert repr(response) == "<Response [200 OK]>"444546async def test_post(server, backend):47    url = server.url48    async with httpx.AsyncClient(backend=backend) as client:49        response = await client.post(url, data=b"Hello, world!")50    assert response.status_code == 200515253async def test_post_json(server, backend):54    url = server.url55    async with httpx.AsyncClient(backend=backend) as client:56        response = await client.post(url, json={"text": "Hello, world!"})57    assert response.status_code == 200585960async def test_stream_response(server, backend):61    async with httpx.AsyncClient(backend=backend) as client:62        response = await client.request("GET", server.url, stream=True)63    assert response.status_code == 20064    body = await response.read()65    assert body == b"Hello, world!"66    assert response.content == b"Hello, world!"676869async def test_access_content_stream_response(server, backend):70    async with httpx.AsyncClient(backend=backend) as client:71        response = await client.request("GET", server.url, stream=True)72    assert response.status_code == 20073    with pytest.raises(httpx.ResponseNotRead):74        response.content757677async def test_stream_request(server, backend):78    async def hello_world():79        yield b"Hello, "80        yield b"world!"8182    async with httpx.AsyncClient(backend=backend) as client:83        response = await client.request("POST", server.url, data=hello_world())84    assert response.status_code == 200858687async def test_raise_for_status(server, backend):88    async with httpx.AsyncClient(backend=backend) as client:89        for status_code in (200, 400, 404, 500, 505):90            response = await client.request(91                "GET", server.url.copy_with(path=f"/status/{status_code}")92            )9394            if 400 <= status_code < 600:95                with pytest.raises(httpx.exceptions.HTTPError) as exc_info:96                    response.raise_for_status()97                assert exc_info.value.response == response98            else:99                assert response.raise_for_status() is None100101102async def test_options(server, backend):103    async with httpx.AsyncClient(backend=backend) as client:104        response = await client.options(server.url)105    assert response.status_code == 200106    assert response.text == "Hello, world!"107108109async def test_head(server, backend):110    async with httpx.AsyncClient(backend=backend) as client:111        response = await client.head(server.url)112    assert response.status_code == 200113    assert response.text == ""114115116async def test_put(server, backend):117    async with httpx.AsyncClient(backend=backend) as client:118        response = await client.put(server.url, data=b"Hello, world!")119    assert response.status_code == 200120121122async def test_patch(server, backend):123    async with httpx.AsyncClient(backend=backend) as client:124        response = await client.patch(server.url, data=b"Hello, world!")125    assert response.status_code == 200126127128async def test_delete(server, backend):129    async with httpx.AsyncClient(backend=backend) as client:130        response = await client.delete(server.url)131    assert response.status_code == 200132    assert response.text == "Hello, world!"133134135async def test_100_continue(server, backend):136    headers = {"Expect": "100-continue"}137    data = b"Echo request body"138139    async with httpx.AsyncClient(backend=backend) as client:140        response = await client.post(141            server.url.copy_with(path="/echo_body"), headers=headers, data=data142        )143144    assert response.status_code == 200145    assert response.content == data