Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

247 lines · 8.0 KiB · Python

Last changed by

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

History of this file

Take it

The walk stopped after 200 commits: lines older than 2c2c6a7 point at it.

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

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

Context managed transports (#1218)Tom Christiebefore adoption534400e

line 3why, one step back
3import httpcore

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 4–6why, one step back
4import pytest
56import httpx

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

line 7why, one step back
7from tests.utils import MockTransport

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 8–12why, one step back
8
910@pytest.mark.usefixtures("async_environment")11async def test_get(server):12 url = server.url

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

line 13why, one step back
13 async with httpx.AsyncClient(http2=True) as client:

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 14–21why, one step back
14 response = await client.get(url)
15 assert response.status_code == 20016 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"18 assert response.headers19 assert repr(response) == "<Response [200 OK]>"20 assert response.elapsed > timedelta(seconds=0)21

Full coverage, with exception of URLLib3Transport (#1086)Tom Christiebefore adoption2d491c9

lines 22–24why, one step back
22 with pytest.raises(httpx.NotRedirectResponse):
23 await response.anext()24

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

line 25why, one step back
25

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

lines 26–33why, one step back
26@pytest.mark.parametrize(
27 "url",28 [29 pytest.param("invalid://example.org", id="scheme-not-http(s)"),30 pytest.param("://example.org", id="no-scheme"),31 pytest.param("http://", id="no-host"),32 ],33)

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

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

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

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

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

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

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

line 37why, one step back
37 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

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

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

lines 39–41why, one step back
39
4041@pytest.mark.usefixtures("async_environment")

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 42–59why, one step back
42async def test_build_request(server):
43 url = server.url.copy_with(path="/echo_headers")44 headers = {"Custom-header": "value"}45 async with httpx.AsyncClient() as client:46 request = client.build_request("GET", url)47 request.headers.update(headers)48 response = await client.send(request)4950 assert response.status_code == 20051 assert response.url == url5253 assert response.json()["Custom-header"] == "value"545556@pytest.mark.usefixtures("async_environment")57async def test_post(server):58 url = server.url59 async with httpx.AsyncClient() as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 60why, one step back
60 response = await client.post(url, content=b"Hello, world!")

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 61–100why, one step back
61 assert response.status_code == 200
626364@pytest.mark.usefixtures("async_environment")65async def test_post_json(server):66 url = server.url67 async with httpx.AsyncClient() as client:68 response = await client.post(url, json={"text": "Hello, world!"})69 assert response.status_code == 200707172@pytest.mark.usefixtures("async_environment")73async def test_stream_response(server):74 async with httpx.AsyncClient() as client:75 async with client.stream("GET", server.url) as response:76 body = await response.aread()7778 assert response.status_code == 20079 assert body == b"Hello, world!"80 assert response.content == b"Hello, world!"818283@pytest.mark.usefixtures("async_environment")84async def test_access_content_stream_response(server):85 async with httpx.AsyncClient() as client:86 async with client.stream("GET", server.url) as response:87 pass8889 assert response.status_code == 20090 with pytest.raises(httpx.ResponseNotRead):91 response.content929394@pytest.mark.usefixtures("async_environment")95async def test_stream_request(server):96 async def hello_world():97 yield b"Hello, "98 yield b"world!"99100 async with httpx.AsyncClient() as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

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

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 102–113why, one step back
102 assert response.status_code == 200
103104105@pytest.mark.usefixtures("async_environment")106async def test_raise_for_status(server):107 async with httpx.AsyncClient() as client:108 for status_code in (200, 400, 404, 500, 505):109 response = await client.request(110 "GET", server.url.copy_with(path=f"/status/{status_code}")111 )112113 if 400 <= status_code < 600:

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

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

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 115–117why, one step back
115 response.raise_for_status()
116 assert exc_info.value.response == response117 else:

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

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

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 119–139why, one step back
119
120121@pytest.mark.usefixtures("async_environment")122async def test_options(server):123 async with httpx.AsyncClient() as client:124 response = await client.options(server.url)125 assert response.status_code == 200126 assert response.text == "Hello, world!"127128129@pytest.mark.usefixtures("async_environment")130async def test_head(server):131 async with httpx.AsyncClient() as client:132 response = await client.head(server.url)133 assert response.status_code == 200134 assert response.text == ""135136137@pytest.mark.usefixtures("async_environment")138async def test_put(server):139 async with httpx.AsyncClient() as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

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

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 141–146why, one step back
141 assert response.status_code == 200
142143144@pytest.mark.usefixtures("async_environment")145async def test_patch(server):146 async with httpx.AsyncClient() as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

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

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 148–161why, one step back
148 assert response.status_code == 200
149150151@pytest.mark.usefixtures("async_environment")152async def test_delete(server):153 async with httpx.AsyncClient() as client:154 response = await client.delete(server.url)155 assert response.status_code == 200156 assert response.text == "Hello, world!"157158159@pytest.mark.usefixtures("async_environment")160async def test_100_continue(server):161 headers = {"Expect": "100-continue"}

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 162why, one step back
162 content = b"Echo request body"

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 163–165why, one step back
163
164 async with httpx.AsyncClient() as client:165 response = await client.post(

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 166why, one step back
166 server.url.copy_with(path="/echo_body"), headers=headers, content=content

Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7

lines 167–169why, one step back
167 )
168169 assert response.status_code == 200

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 170why, one step back
170 assert response.content == content

Context managed transports (#1218)Tom Christiebefore adoption534400e

lines 171–209why, one step back
171
172173@pytest.mark.usefixtures("async_environment")174async def test_context_managed_transport():175 class Transport(httpcore.AsyncHTTPTransport):176 def __init__(self):177 self.events = []178179 async def aclose(self):180 # The base implementation of httpcore.AsyncHTTPTransport just181 # calls into `.aclose`, so simple transport cases can just override182 # this method for any cleanup, where more complex cases183 # might want to additionally override `__aenter__`/`__aexit__`.184 self.events.append("transport.aclose")185186 async def __aenter__(self):187 await super().__aenter__()188 self.events.append("transport.__aenter__")189190 async def __aexit__(self, *args):191 await super().__aexit__(*args)192 self.events.append("transport.__aexit__")193194 # Note that we're including 'proxies' here to *also* run through the195 # proxy context management, although we can't easily test that at the196 # moment, since we can't add proxies as transport instances.197 #198 # Once we have a more generalised Mount API we'll be able to remove this199 # in favour of ensuring all mounts are context managed, which will200 # also neccessarily include proxies.201 transport = Transport()202 async with httpx.AsyncClient(transport=transport, proxies="http://www.example.com"):203 pass204205 assert transport.events == [206 "transport.__aenter__",207 "transport.aclose",208 "transport.__aexit__",209 ]

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

lines 210–211why, one step back
210
211

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 212–213why, one step back
212def hello_world(request):
213 return httpx.Response(200, text="Hello, world!")

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

lines 214–216why, one step back
214
215216@pytest.mark.usefixtures("async_environment")

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 217–218why, one step back
217async def test_client_closed_state_using_implicit_open():
218 client = httpx.AsyncClient(transport=MockTransport(hello_world))

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

line 219why, one step back
219

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

line 220why, one step back
220 assert not client.is_closed

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

lines 221–225why, one step back
221 await client.get("http://example.com")
222223 assert not client.is_closed224 await client.aclose()225

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 226–228why, one step back
226 assert client.is_closed
227 with pytest.raises(RuntimeError):228 await client.get("http://example.com")

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

line 229why, one step back
229

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

line 230why, one step back
230

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

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

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 232–233why, one step back
232async def test_client_closed_state_using_with_block():
233 async with httpx.AsyncClient(transport=MockTransport(hello_world)) as client:

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

line 234why, one step back
234 assert not client.is_closed

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

line 235why, one step back
235 await client.get("http://example.com")

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

lines 236–237why, one step back
236
237 assert client.is_closed

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 238–239why, one step back
238 with pytest.raises(RuntimeError):
239 await client.get("http://example.com")

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

lines 240–242why, one step back
240
241242@pytest.mark.usefixtures("async_environment")

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

lines 243–245why, one step back
243async def test_deleting_unclosed_async_client_causes_warning():
244 client = httpx.AsyncClient(transport=MockTransport(hello_world))245 await client.get("http://example.com")

Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c

line 246why, one step back
246 with pytest.warns(UserWarning):

Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5

line 247why, one step back
247 del client