Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

131 lines · 4.3 KiB · Python

Last changed by

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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]>"151617@pytest.mark.asyncio18async def test_get_no_backend(server):19    """20    Verify that the client is capable of making a simple request if not given a backend.21    """22    url = server.url23    async with httpx.AsyncClient() as client:24        response = await client.get(url)25    assert response.status_code == 20026    assert response.text == "Hello, world!"27    assert response.http_version == "HTTP/1.1"28    assert response.headers29    assert repr(response) == "<Response [200 OK]>"303132async def test_post(server, backend):33    url = server.url34    async with httpx.AsyncClient(backend=backend) as client:35        response = await client.post(url, data=b"Hello, world!")36    assert response.status_code == 200373839async def test_post_json(server, backend):40    url = server.url41    async with httpx.AsyncClient(backend=backend) as client:42        response = await client.post(url, json={"text": "Hello, world!"})43    assert response.status_code == 200444546async def test_stream_response(server, backend):47    async with httpx.AsyncClient(backend=backend) as client:48        response = await client.request("GET", server.url, stream=True)49    assert response.status_code == 20050    body = await response.read()51    assert body == b"Hello, world!"52    assert response.content == b"Hello, world!"535455async def test_access_content_stream_response(server, backend):56    async with httpx.AsyncClient(backend=backend) as client:57        response = await client.request("GET", server.url, stream=True)58    assert response.status_code == 20059    with pytest.raises(httpx.ResponseNotRead):60        response.content616263async def test_stream_request(server, backend):64    async def hello_world():65        yield b"Hello, "66        yield b"world!"6768    async with httpx.AsyncClient(backend=backend) as client:69        response = await client.request("POST", server.url, data=hello_world())70    assert response.status_code == 200717273async def test_raise_for_status(server, backend):74    async with httpx.AsyncClient(backend=backend) as client:75        for status_code in (200, 400, 404, 500, 505):76            response = await client.request(77                "GET", server.url.copy_with(path=f"/status/{status_code}")78            )7980            if 400 <= status_code < 600:81                with pytest.raises(httpx.exceptions.HTTPError) as exc_info:82                    response.raise_for_status()83                assert exc_info.value.response == response84            else:85                assert response.raise_for_status() is None868788async def test_options(server, backend):89    async with httpx.AsyncClient(backend=backend) as client:90        response = await client.options(server.url)91    assert response.status_code == 20092    assert response.text == "Hello, world!"939495async def test_head(server, backend):96    async with httpx.AsyncClient(backend=backend) as client:97        response = await client.head(server.url)98    assert response.status_code == 20099    assert response.text == ""100101102async def test_put(server, backend):103    async with httpx.AsyncClient(backend=backend) as client:104        response = await client.put(server.url, data=b"Hello, world!")105    assert response.status_code == 200106107108async def test_patch(server, backend):109    async with httpx.AsyncClient(backend=backend) as client:110        response = await client.patch(server.url, data=b"Hello, world!")111    assert response.status_code == 200112113114async def test_delete(server, backend):115    async with httpx.AsyncClient(backend=backend) as client:116        response = await client.delete(server.url)117    assert response.status_code == 200118    assert response.text == "Hello, world!"119120121async def test_100_continue(server, backend):122    headers = {"Expect": "100-continue"}123    data = b"Echo request body"124125    async with httpx.AsyncClient(backend=backend) as client:126        response = await client.post(127            server.url.copy_with(path="/echo_body"), headers=headers, data=data128        )129130    assert response.status_code == 200131    assert response.content == data