Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

162 lines · 5.1 KiB · Python

Last changed by

Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417

History of this file

Take it

The walk stopped after 200 commits: lines older than 074cd25 point at it.

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 1–7why, one step back
1from datetime import timedelta
23import pytest45import httpx67

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 8–9why, one step back
8@pytest.mark.usefixtures("async_environment")
9async def test_get(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 12–20why, one step back
12 response = await client.get(url)
13 assert response.status_code == 20014 assert response.text == "Hello, world!"15 assert response.http_version == "HTTP/1.1"16 assert response.headers17 assert repr(response) == "<Response [200 OK]>"18 assert response.elapsed > timedelta(seconds=0)1920

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 21–22why, one step back
21@pytest.mark.usefixtures("async_environment")
22async def test_build_request(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 23–24why, one step back
23 url = server.url.copy_with(path="/echo_headers")
24 headers = {"Custom-header": "value"}

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 26–35why, one step back
26 request = client.build_request("GET", url)
27 request.headers.update(headers)28 response = await client.send(request)2930 assert response.status_code == 20031 assert response.url == url3233 assert response.json()["Custom-header"] == "value"3435

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 36–37why, one step back
36@pytest.mark.usefixtures("async_environment")
37async def test_post(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 40–43why, one step back
40 response = await client.post(url, data=b"Hello, world!")
41 assert response.status_code == 2004243

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 44–45why, one step back
44@pytest.mark.usefixtures("async_environment")
45async def test_post_json(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

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

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 48–51why, one step back
48 response = await client.post(url, json={"text": "Hello, world!"})
49 assert response.status_code == 2005051

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 52–53why, one step back
52@pytest.mark.usefixtures("async_environment")
53async def test_stream_response(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

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

line 55why, one step back
55 async with client.stream("GET", server.url) as response:

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 56why, one step back
56 body = await response.aread()

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

line 57why, one step back
57

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 58–62why, one step back
58 assert response.status_code == 200
59 assert body == b"Hello, world!"60 assert response.content == b"Hello, world!"6162

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 63–64why, one step back
63@pytest.mark.usefixtures("async_environment")
64async def test_access_content_stream_response(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

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

lines 66–68why, one step back
66 async with client.stream("GET", server.url) as response:
67 pass68

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 69–73why, one step back
69 assert response.status_code == 200
70 with pytest.raises(httpx.ResponseNotRead):71 response.content7273

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 74–75why, one step back
74@pytest.mark.usefixtures("async_environment")
75async def test_stream_request(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 76–79why, one step back
76 async def hello_world():
77 yield b"Hello, "78 yield b"world!"79

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 81–84why, one step back
81 response = await client.request("POST", server.url, data=hello_world())
82 assert response.status_code == 2008384

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 85–86why, one step back
85@pytest.mark.usefixtures("async_environment")
86async def test_raise_for_status(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 88–100why, one step back
88 for status_code in (200, 400, 404, 500, 505):
89 response = await client.request(90 "GET", server.url.copy_with(path=f"/status/{status_code}")91 )9293 if 400 <= status_code < 600:94 with pytest.raises(httpx.HTTPError) as exc_info:95 response.raise_for_status()96 assert exc_info.value.response == response97 else:98 assert response.raise_for_status() is None99100

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 101–102why, one step back
101@pytest.mark.usefixtures("async_environment")
102async def test_options(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 104–108why, one step back
104 response = await client.options(server.url)
105 assert response.status_code == 200106 assert response.text == "Hello, world!"107108

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 109–110why, one step back
109@pytest.mark.usefixtures("async_environment")
110async def test_head(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 112–116why, one step back
112 response = await client.head(server.url)
113 assert response.status_code == 200114 assert response.text == ""115116

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 117–118why, one step back
117@pytest.mark.usefixtures("async_environment")
118async def test_put(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 120–123why, one step back
120 response = await client.put(server.url, data=b"Hello, world!")
121 assert response.status_code == 200122123

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 124–125why, one step back
124@pytest.mark.usefixtures("async_environment")
125async def test_patch(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 127–130why, one step back
127 response = await client.patch(server.url, data=b"Hello, world!")
128 assert response.status_code == 200129130

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 131–132why, one step back
131@pytest.mark.usefixtures("async_environment")
132async def test_delete(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 134–138why, one step back
134 response = await client.delete(server.url)
135 assert response.status_code == 200136 assert response.text == "Hello, world!"137138

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 139–140why, one step back
139@pytest.mark.usefixtures("async_environment")
140async def test_100_continue(server):

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 141–143why, one step back
141 headers = {"Expect": "100-continue"}
142 data = b"Echo request body"143

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 145–150why, one step back
145 response = await client.post(
146 server.url.copy_with(path="/echo_body"), headers=headers, data=data147 )148149 assert response.status_code == 200150 assert response.content == data

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

lines 151–152why, one step back
151
152

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 153–154why, one step back
153@pytest.mark.usefixtures("async_environment")
154async def test_uds(uds_server):

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

lines 155–157why, one step back
155 url = uds_server.url
156 uds = uds_server.config.uds157 assert uds is not None

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 158why, one step back
158 async with httpx.AsyncClient(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"