Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

126 lines · 3.8 KiB · Python

Last changed by

Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0

History of this file

Take it

1import pytest23import httpcore456@pytest.mark.asyncio7async def test_get(server):8    url = "http://127.0.0.1:8000/"9    async with httpcore.AsyncClient() as client:10        response = await client.get(url)11    assert response.status_code == 20012    assert response.text == "Hello, world!"13    assert response.protocol == "HTTP/1.1"14    assert response.headers15    assert repr(response) == "<Response(200, 'OK')>"161718@pytest.mark.asyncio19async def test_post(server):20    url = "http://127.0.0.1:8000/"21    async with httpcore.AsyncClient() as client:22        response = await client.post(url, data=b"Hello, world!")23    assert response.status_code == 200242526@pytest.mark.asyncio27async def test_stream_response(server):28    async with httpcore.AsyncClient() as client:29        response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)30    assert response.status_code == 20031    body = await response.read()32    assert body == b"Hello, world!"33    assert response.content == b"Hello, world!"343536@pytest.mark.asyncio37async def test_access_content_stream_response(server):38    async with httpcore.AsyncClient() as client:39        response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)40    assert response.status_code == 20041    with pytest.raises(httpcore.ResponseNotRead):42        response.content434445@pytest.mark.asyncio46async def test_stream_request(server):47    async def hello_world():48        yield b"Hello, "49        yield b"world!"5051    async with httpcore.AsyncClient() as client:52        response = await client.request(53            "POST", "http://127.0.0.1:8000/", data=hello_world()54        )55    assert response.status_code == 200565758@pytest.mark.asyncio59async def test_raise_for_status(server):60    async with httpcore.AsyncClient() as client:61        for status_code in (200, 400, 404, 500, 505):62            response = await client.request(63                "GET", f"http://127.0.0.1:8000/status/{status_code}"64            )6566            if 400 <= status_code < 600:67                with pytest.raises(httpcore.exceptions.HttpError):68                    response.raise_for_status()69            else:70                assert response.raise_for_status() is None717273@pytest.mark.asyncio74async def test_options(server):75    url = "http://127.0.0.1:8000/"76    async with httpcore.AsyncClient() as client:77        response = await client.options(url)78    assert response.status_code == 20079    assert response.text == "Hello, world!"808182@pytest.mark.asyncio83async def test_head(server):84    url = "http://127.0.0.1:8000/"85    async with httpcore.AsyncClient() as client:86        response = await client.head(url)87    assert response.status_code == 20088    assert response.text == ""899091@pytest.mark.asyncio92async def test_put(server):93    url = "http://127.0.0.1:8000/"94    async with httpcore.AsyncClient() as client:95        response = await client.put(url, data=b"Hello, world!")96    assert response.status_code == 200979899@pytest.mark.asyncio100async def test_patch(server):101    url = "http://127.0.0.1:8000/"102    async with httpcore.AsyncClient() as client:103        response = await client.patch(url, data=b"Hello, world!")104    assert response.status_code == 200105106107@pytest.mark.asyncio108async def test_delete(server):109    url = "http://127.0.0.1:8000/"110    async with httpcore.AsyncClient() as client:111        response = await client.delete(url)112    assert response.status_code == 200113    assert response.text == "Hello, world!"114115116@pytest.mark.asyncio117async def test_100_continue(server):118    url = "http://127.0.0.1:8000/echo_body"119    headers = {"Expect": "100-continue"}120    data = b"Echo request body"121122    async with httpcore.AsyncClient() as client:123        response = await client.post(url, headers=headers, data=data)124125    assert response.status_code == 200126    assert response.content == data