Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

157 lines · 4.9 KiB · Python

Last changed by

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

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

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

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

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

lines 22–28why, one step back
22async def test_get_invalid_url(server):
23 async with httpx.AsyncClient() as client:24 with pytest.raises(httpx.InvalidURL):25 await client.get("invalid://example.org")262728@pytest.mark.usefixtures("async_environment")

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

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

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

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

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

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

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

lines 33–42why, one step back
33 request = client.build_request("GET", url)
34 request.headers.update(headers)35 response = await client.send(request)3637 assert response.status_code == 20038 assert response.url == url3940 assert response.json()["Custom-header"] == "value"4142

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

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

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

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

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

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

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

lines 47–50why, one step back
47 response = await client.post(url, data=b"Hello, world!")
48 assert response.status_code == 2004950

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

lines 51–52why, one step back
51@pytest.mark.usefixtures("async_environment")
52async def test_post_json(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, json={"text": "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_stream_response(server):

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

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

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

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

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

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

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

lines 64–69why, one step back
64
65 assert response.status_code == 20066 assert body == b"Hello, world!"67 assert response.content == b"Hello, world!"6869

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

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

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

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

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

lines 73–80why, one step back
73 async with client.stream("GET", server.url) as response:
74 pass7576 assert response.status_code == 20077 with pytest.raises(httpx.ResponseNotRead):78 response.content7980

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

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

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

lines 83–86why, one step back
83 async def hello_world():
84 yield b"Hello, "85 yield b"world!"86

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

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

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

lines 88–91why, one step back
88 response = await client.request("POST", server.url, data=hello_world())
89 assert response.status_code == 2009091

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

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

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

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

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

lines 95–104why, one step back
95 for status_code in (200, 400, 404, 500, 505):
96 response = await client.request(97 "GET", server.url.copy_with(path=f"/status/{status_code}")98 )99100 if 400 <= status_code < 600:101 with pytest.raises(httpx.HTTPError) as exc_info:102 response.raise_for_status()103 assert exc_info.value.response == response104 else:

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

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

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

lines 106–107why, one step back
106
107

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

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

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

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

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

lines 111–115why, one step back
111 response = await client.options(server.url)
112 assert response.status_code == 200113 assert response.text == "Hello, world!"114115

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

lines 116–117why, one step back
116@pytest.mark.usefixtures("async_environment")
117async def test_head(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.head(server.url)
120 assert response.status_code == 200121 assert response.text == ""122123

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

lines 124–125why, one step back
124@pytest.mark.usefixtures("async_environment")
125async def test_put(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–130why, one step back
127 response = await client.put(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_patch(server):

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

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

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

lines 134–137why, one step back
134 response = await client.patch(server.url, data=b"Hello, world!")
135 assert response.status_code == 200136137

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

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

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

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

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

lines 141–145why, one step back
141 response = await client.delete(server.url)
142 assert response.status_code == 200143 assert response.text == "Hello, world!"144145

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

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

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

lines 148–150why, one step back
148 headers = {"Expect": "100-continue"}
149 data = b"Echo request body"150

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

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

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

lines 152–157why, one step back
152 response = await client.post(
153 server.url.copy_with(path="/echo_body"), headers=headers, data=data154 )155156 assert response.status_code == 200157 assert response.content == data