Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

134 lines · 4.0 KiB · Python

Last changed by

http3 (#86)Tom Christiebefore adoptionc9747aa

History of this file

Take it

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 1–2why, one step back
1import pytest
2

http3 (#86)Tom Christiebefore adoptionc9747aa

line 3why, one step back
3import http3

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 4–8why, one step back
4
56@pytest.mark.asyncio7async def test_get(server):8 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 9why, one step back
9 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 10–20why, one step back
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/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 21why, one step back
21 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 22–26why, one step back
22 response = await client.post(url, data=b"Hello, world!")
23 assert response.status_code == 200242526@pytest.mark.asyncio

Add json interfacesTom Christiebefore adoption5a63d31

lines 27–28why, one step back
27async def test_post_json(server):
28 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 29why, one step back
29 async with http3.AsyncClient() as client:

Add json interfacesTom Christiebefore adoption5a63d31

lines 30–34why, one step back
30 response = await client.post(url, json={"text": "Hello, world!"})
31 assert response.status_code == 200323334@pytest.mark.asyncio

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 35why, one step back
35async def test_stream_response(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 36why, one step back
36 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 37–45why, one step back
37 response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)
38 assert response.status_code == 20039 body = await response.read()40 assert body == b"Hello, world!"41 assert response.content == b"Hello, world!"424344@pytest.mark.asyncio45async def test_access_content_stream_response(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 46why, one step back
46 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 47–48why, one step back
47 response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)
48 assert response.status_code == 200

http3 (#86)Tom Christiebefore adoptionc9747aa

line 49why, one step back
49 with pytest.raises(http3.ResponseNotRead):

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 50–58why, one step back
50 response.content
515253@pytest.mark.asyncio54async def test_stream_request(server):55 async def hello_world():56 yield b"Hello, "57 yield b"world!"58

http3 (#86)Tom Christiebefore adoptionc9747aa

line 59why, one step back
59 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 60–67why, one step back
60 response = await client.request(
61 "POST", "http://127.0.0.1:8000/", data=hello_world()62 )63 assert response.status_code == 200646566@pytest.mark.asyncio67async def test_raise_for_status(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 68why, one step back
68 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 69–74why, one step back
69 for status_code in (200, 400, 404, 500, 505):
70 response = await client.request(71 "GET", f"http://127.0.0.1:8000/status/{status_code}"72 )7374 if 400 <= status_code < 600:

http3 (#86)Tom Christiebefore adoptionc9747aa

line 75why, one step back
75 with pytest.raises(http3.exceptions.HttpError):

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 76–83why, one step back
76 response.raise_for_status()
77 else:78 assert response.raise_for_status() is None798081@pytest.mark.asyncio82async def test_options(server):83 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 84why, one step back
84 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 85–92why, one step back
85 response = await client.options(url)
86 assert response.status_code == 20087 assert response.text == "Hello, world!"888990@pytest.mark.asyncio91async def test_head(server):92 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 93why, one step back
93 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 94–101why, one step back
94 response = await client.head(url)
95 assert response.status_code == 20096 assert response.text == ""979899@pytest.mark.asyncio100async def test_put(server):101 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 102why, one step back
102 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 103–109why, one step back
103 response = await client.put(url, data=b"Hello, world!")
104 assert response.status_code == 200105106107@pytest.mark.asyncio108async def test_patch(server):109 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 110why, one step back
110 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 111–117why, one step back
111 response = await client.patch(url, data=b"Hello, world!")
112 assert response.status_code == 200113114115@pytest.mark.asyncio116async def test_delete(server):117 url = "http://127.0.0.1:8000/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 118why, one step back
118 async with http3.AsyncClient() as client:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 119–121why, one step back
119 response = await client.delete(url)
120 assert response.status_code == 200121 assert response.text == "Hello, world!"

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

lines 122–129why, one step back
122
123124@pytest.mark.asyncio125async def test_100_continue(server):126 url = "http://127.0.0.1:8000/echo_body"127 headers = {"Expect": "100-continue"}128 data = b"Echo request body"129

http3 (#86)Tom Christiebefore adoptionc9747aa

line 130why, one step back
130 async with http3.AsyncClient() as client:

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

lines 131–134why, one step back
131 response = await client.post(url, headers=headers, data=data)
132133 assert response.status_code == 200134 assert response.content == data