Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

165 lines · 5.1 KiB · Python

Last changed by

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

History of this file

Take it

The walk stopped after 200 commits: lines older than 869714f point at it.

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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:

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

lines 21–28why, one step back
21@pytest.mark.parametrize(
22 "url",23 [24 pytest.param("invalid://example.org", id="scheme-not-http(s)"),25 pytest.param("://example.org", id="no-scheme"),26 pytest.param("http://", id="no-host"),27 ],28)

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

line 29why, one step back
29@pytest.mark.usefixtures("async_environment")

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 30why, one step back
30async def test_get_invalid_url(server, url):

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 31–32why, one step back
31 async with httpx.AsyncClient() as client:
32 with pytest.raises(httpx.InvalidURL):

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 33why, one step back
33 await client.get(url)

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 34–36why, one step back
34
3536@pytest.mark.usefixtures("async_environment")

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

line 37why, one step back
37async def test_build_request(server):

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 41–50why, one step back
41 request = client.build_request("GET", url)
42 request.headers.update(headers)43 response = await client.send(request)4445 assert response.status_code == 20046 assert response.url == url4748 assert response.json()["Custom-header"] == "value"4950

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 55–58why, one step back
55 response = await client.post(url, data=b"Hello, world!")
56 assert response.status_code == 2005758

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 63–66why, one step back
63 response = await client.post(url, json={"text": "Hello, world!"})
64 assert response.status_code == 2006566

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 72–77why, one step back
72
73 assert response.status_code == 20074 assert body == b"Hello, world!"75 assert response.content == b"Hello, world!"7677

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 81–88why, one step back
81 async with client.stream("GET", server.url) as response:
82 pass8384 assert response.status_code == 20085 with pytest.raises(httpx.ResponseNotRead):86 response.content8788

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 91–94why, one step back
91 async def hello_world():
92 yield b"Hello, "93 yield b"world!"94

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 96–99why, one step back
96 response = await client.request("POST", server.url, data=hello_world())
97 assert response.status_code == 2009899

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 103–108why, one step back
103 for status_code in (200, 400, 404, 500, 505):
104 response = await client.request(105 "GET", server.url.copy_with(path=f"/status/{status_code}")106 )107108 if 400 <= status_code < 600:

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 110–112why, one step back
110 response.raise_for_status()
111 assert exc_info.value.response == response112 else:

Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16

line 113why, one step back
113 assert response.raise_for_status() is None # type: ignore

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 114–115why, one step back
114
115

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

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

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 127–131why, one step back
127 response = await client.head(server.url)
128 assert response.status_code == 200129 assert response.text == ""130131

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 135–138why, one step back
135 response = await client.put(server.url, data=b"Hello, world!")
136 assert response.status_code == 200137138

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 142–145why, one step back
142 response = await client.patch(server.url, data=b"Hello, world!")
143 assert response.status_code == 200144145

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

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

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 149–153why, one step back
149 response = await client.delete(server.url)
150 assert response.status_code == 200151 assert response.text == "Hello, world!"152153

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 156–158why, one step back
156 headers = {"Expect": "100-continue"}
157 data = b"Echo request body"158

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

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

Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f

lines 160–165why, one step back
160 response = await client.post(
161 server.url.copy_with(path="/echo_body"), headers=headers, data=data162 )163164 assert response.status_code == 200165 assert response.content == data