Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

135 lines · 4.0 KiB · Python

Last changed by

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3import httpx

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/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 10–14why, 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.headers

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

line 15why, one step back
15 assert repr(response) == "<Response [200 OK]>"

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

lines 16–20why, one step back
16
1718@pytest.mark.asyncio19async def test_post(server):20 url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 21why, one step back
21 async with httpx.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/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 29why, one step back
29 async with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 36why, one step back
36 async with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 46why, one step back
46 async with httpx.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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 49why, one step back
49 with pytest.raises(httpx.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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 59why, one step back
59 async with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 68why, one step back
68 async with httpx.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:

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 75why, one step back
75 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:

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

line 76why, one step back
76 response.raise_for_status()

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 77why, one step back
77 assert exc_info.value.response == response

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

lines 78–84why, one step back
78 else:
79 assert response.raise_for_status() is None808182@pytest.mark.asyncio83async def test_options(server):84 url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 85why, one step back
85 async with httpx.AsyncClient() as client:

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

lines 86–93why, one step back
86 response = await client.options(url)
87 assert response.status_code == 20088 assert response.text == "Hello, world!"899091@pytest.mark.asyncio92async def test_head(server):93 url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 94why, one step back
94 async with httpx.AsyncClient() as client:

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

lines 95–102why, one step back
95 response = await client.head(url)
96 assert response.status_code == 20097 assert response.text == ""9899100@pytest.mark.asyncio101async def test_put(server):102 url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 103why, one step back
103 async with httpx.AsyncClient() as client:

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 111why, one step back
111 async with httpx.AsyncClient() as client:

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 119why, one step back
119 async with httpx.AsyncClient() as client:

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

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

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 131why, one step back
131 async with httpx.AsyncClient() as client:

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

lines 132–135why, one step back
132 response = await client.post(url, headers=headers, data=data)
133134 assert response.status_code == 200135 assert response.content == data