Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

176 lines · 5.3 KiB · Python

Last changed by

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

History of this file

Take it

The walk stopped after 200 commits: lines older than 994403b point at it.

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 1–2why, one step back
1from datetime import timedelta
2

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 3–8why, one step back
3import pytest
45import httpx678async def test_get(server, backend):

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

line 9why, one step back
9 url = server.url

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 10why, one step back
10 async with httpx.Client() as client:

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 11–16why, one step back
11 response = await client.get(url)
12 assert response.status_code == 20013 assert response.text == "Hello, world!"14 assert response.http_version == "HTTP/1.1"15 assert response.headers16 assert repr(response) == "<Response [200 OK]>"

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 17why, one step back
17 assert response.elapsed > timedelta(seconds=0)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 18–19why, one step back
18
19

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 20–22why, one step back
20async def test_build_request(server, backend):
21 url = server.url.copy_with(path="/echo_headers")22 headers = {"Custom-header": "value"}

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 23why, one step back
23 async with httpx.Client() as client:

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 24–33why, one step back
24 request = client.build_request("GET", url)
25 request.headers.update(headers)26 response = await client.send(request)2728 assert response.status_code == 20029 assert response.url == url3031 assert response.json()["Custom-header"] == "value"3233

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 34–38why, one step back
34@pytest.mark.asyncio
35async def test_get_no_backend(server):36 """37 Verify that the client is capable of making a simple request if not given a backend.38 """

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

line 39why, one step back
39 url = server.url

Drop sync (#544)Tom Christiebefore adoption206c537

line 40why, one step back
40 async with httpx.Client() as client:

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 41–49why, one step back
41 response = await client.get(url)
42 assert response.status_code == 20043 assert response.text == "Hello, world!"44 assert response.http_version == "HTTP/1.1"45 assert response.headers46 assert repr(response) == "<Response [200 OK]>"474849async def test_post(server, backend):

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

line 50why, one step back
50 url = server.url

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 51why, one step back
51 async with httpx.Client() as client:

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 52–56why, one step back
52 response = await client.post(url, data=b"Hello, world!")
53 assert response.status_code == 200545556async def test_post_json(server, backend):

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

line 57why, one step back
57 url = server.url

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 58why, one step back
58 async with httpx.Client() as client:

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 59–63why, one step back
59 response = await client.post(url, json={"text": "Hello, world!"})
60 assert response.status_code == 200616263async def test_stream_response(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 64why, one step back
64 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 65–67why, one step back
65 async with client.stream("GET", server.url) as response:
66 body = await response.read()67

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 68–73why, one step back
68 assert response.status_code == 200
69 assert body == b"Hello, world!"70 assert response.content == b"Hello, world!"717273async def test_access_content_stream_response(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 74why, one step back
74 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 75–77why, one step back
75 async with client.stream("GET", server.url) as response:
76 pass77

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 78–87why, one step back
78 assert response.status_code == 200
79 with pytest.raises(httpx.ResponseNotRead):80 response.content818283async def test_stream_request(server, backend):84 async def hello_world():85 yield b"Hello, "86 yield b"world!"87

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 88why, one step back
88 async with httpx.Client() as client:

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

line 89why, one step back
89 response = await client.request("POST", server.url, data=hello_world())

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 90–93why, one step back
90 assert response.status_code == 200
919293async def test_raise_for_status(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 95–96why, one step back
95 for status_code in (200, 400, 404, 500, 505):
96 response = await client.request(

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

line 97why, one step back
97 "GET", server.url.copy_with(path=f"/status/{status_code}")

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 98–100why, one step back
98 )
99100 if 400 <= status_code < 600:

Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 102–108why, one step back
102 response.raise_for_status()
103 assert exc_info.value.response == response104 else:105 assert response.raise_for_status() is None106107108async def test_options(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 109why, one step back
109 async with httpx.Client() as client:

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

line 110why, one step back
110 response = await client.options(server.url)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 111–115why, one step back
111 assert response.status_code == 200
112 assert response.text == "Hello, world!"113114115async def test_head(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 116why, one step back
116 async with httpx.Client() as client:

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

line 117why, one step back
117 response = await client.head(server.url)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 118–122why, one step back
118 assert response.status_code == 200
119 assert response.text == ""120121122async def test_put(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 123why, one step back
123 async with httpx.Client() as client:

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

line 124why, one step back
124 response = await client.put(server.url, data=b"Hello, world!")

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 125–128why, one step back
125 assert response.status_code == 200
126127128async def test_patch(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 129why, one step back
129 async with httpx.Client() as client:

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

line 130why, one step back
130 response = await client.patch(server.url, data=b"Hello, world!")

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 131–134why, one step back
131 assert response.status_code == 200
132133134async def test_delete(server, backend):

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 135why, one step back
135 async with httpx.Client() as client:

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

line 136why, one step back
136 response = await client.delete(server.url)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 137–144why, one step back
137 assert response.status_code == 200
138 assert response.text == "Hello, world!"139140141async def test_100_continue(server, backend):142 headers = {"Expect": "100-continue"}143 data = b"Echo request body"144

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 145why, one step back
145 async with httpx.Client() as client:

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

lines 146–148why, one step back
146 response = await client.post(
147 server.url.copy_with(path="/echo_body"), headers=headers, data=data148 )

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 149–151why, one step back
149
150 assert response.status_code == 200151 assert response.content == data

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 152–157why, one step back
152
153154async def test_uds(uds_server, backend):155 url = uds_server.url156 uds = uds_server.config.uds157 assert uds is not None

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

line 158why, one step back
158 async with httpx.Client(uds=uds) as client:

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 159–162why, one step back
159 response = await client.get(url)
160 assert response.status_code == 200161 assert response.text == "Hello, world!"162 assert response.encoding == "iso-8859-1"

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

lines 163–176why, one step back
163
164165@pytest.mark.parametrize(166 "backend",167 [168 pytest.param("asyncio", marks=pytest.mark.asyncio),169 pytest.param("trio", marks=pytest.mark.trio),170 ],171)172async def test_explicit_backend(server, backend):173 async with httpx.Client(backend=backend) as client:174 response = await client.get(server.url)175 assert response.status_code == 200176 assert response.text == "Hello, world!"