test_async_client.py
This file
Path
tests/client/test_async_client.py
137 lines · 4.5 KiB · Python
Last changed by
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
History of this fileTake it
1import pytest23import httpx456async def test_get(server, backend):7 url = "http://127.0.0.1:8000/"8 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 = "http://127.0.0.1:8000/"23 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 = "http://127.0.0.1:8000/"34 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 = "http://127.0.0.1:8000/"41 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", "http://127.0.0.1:8000/", 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", "http://127.0.0.1:8000/", 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(70 "POST", "http://127.0.0.1:8000/", data=hello_world()71 )72 assert response.status_code == 200737475async def test_raise_for_status(server, backend):76 async with httpx.AsyncClient(backend=backend) as client:77 for status_code in (200, 400, 404, 500, 505):78 response = await client.request(79 "GET", f"http://127.0.0.1:8000/status/{status_code}"80 )8182 if 400 <= status_code < 600:83 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:84 response.raise_for_status()85 assert exc_info.value.response == response86 else:87 assert response.raise_for_status() is None888990async def test_options(server, backend):91 url = "http://127.0.0.1:8000/"92 async with httpx.AsyncClient(backend=backend) as client:93 response = await client.options(url)94 assert response.status_code == 20095 assert response.text == "Hello, world!"969798async def test_head(server, backend):99 url = "http://127.0.0.1:8000/"100 async with httpx.AsyncClient(backend=backend) as client:101 response = await client.head(url)102 assert response.status_code == 200103 assert response.text == ""104105106async def test_put(server, backend):107 url = "http://127.0.0.1:8000/"108 async with httpx.AsyncClient(backend=backend) as client:109 response = await client.put(url, data=b"Hello, world!")110 assert response.status_code == 200111112113async def test_patch(server, backend):114 url = "http://127.0.0.1:8000/"115 async with httpx.AsyncClient(backend=backend) as client:116 response = await client.patch(url, data=b"Hello, world!")117 assert response.status_code == 200118119120async def test_delete(server, backend):121 url = "http://127.0.0.1:8000/"122 async with httpx.AsyncClient(backend=backend) as client:123 response = await client.delete(url)124 assert response.status_code == 200125 assert response.text == "Hello, world!"126127128async def test_100_continue(server, backend):129 url = "http://127.0.0.1:8000/echo_body"130 headers = {"Expect": "100-continue"}131 data = b"Echo request body"132133 async with httpx.AsyncClient(backend=backend) as client:134 response = await client.post(url, headers=headers, data=data)135136 assert response.status_code == 200137 assert response.content == data