test_async_client.py
This file
Path
tests/client/test_async_client.py
244 lines · 7.9 KiB · Python
Last changed by
Drop `.next()`/`.anext()` in favour of `response.next_request` (#1339)Tom Christiebefore adoption0eed6a3
History of this fileTake 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 back1from datetime import timedelta2Context managed transports (#1218)Tom Christiebefore adoption534400e
line 3why, one step back3import httpcoreVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 4–6why, one step back4import pytest56import httpxTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 7why, one step back7from tests.utils import MockTransportVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 8–12why, one step back8910@pytest.mark.usefixtures("async_environment")11async def test_get(server):12 url = server.urlHTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5
line 13why, one step back13 async with httpx.AsyncClient(http2=True) as client:Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 14–22why, one step back14 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)2122Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 23–30why, one step back23@pytest.mark.parametrize(24 "url",25 [26 pytest.param("invalid://example.org", id="scheme-not-http(s)"),27 pytest.param("://example.org", id="no-scheme"),28 pytest.param("http://", id="no-host"),29 ],30)Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
line 31why, one step back31@pytest.mark.usefixtures("async_environment")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 32why, one step back32async def test_get_invalid_url(server, url):Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 33why, one step back33 async with httpx.AsyncClient() as client:Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 34why, one step back34 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 35why, one step back35 await client.get(url)Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
lines 36–38why, one step back363738@pytest.mark.usefixtures("async_environment")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 39–56why, one step back39async def test_build_request(server):40 url = server.url.copy_with(path="/echo_headers")41 headers = {"Custom-header": "value"}42 async with httpx.AsyncClient() as client:43 request = client.build_request("GET", url)44 request.headers.update(headers)45 response = await client.send(request)4647 assert response.status_code == 20048 assert response.url == url4950 assert response.json()["Custom-header"] == "value"515253@pytest.mark.usefixtures("async_environment")54async def test_post(server):55 url = server.url56 async with httpx.AsyncClient() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 57why, one step back57 response = await client.post(url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 58–97why, one step back58 assert response.status_code == 200596061@pytest.mark.usefixtures("async_environment")62async def test_post_json(server):63 url = server.url64 async with httpx.AsyncClient() as client:65 response = await client.post(url, json={"text": "Hello, world!"})66 assert response.status_code == 200676869@pytest.mark.usefixtures("async_environment")70async def test_stream_response(server):71 async with httpx.AsyncClient() as client:72 async with client.stream("GET", server.url) as response:73 body = await response.aread()7475 assert response.status_code == 20076 assert body == b"Hello, world!"77 assert response.content == b"Hello, world!"787980@pytest.mark.usefixtures("async_environment")81async def test_access_content_stream_response(server):82 async with httpx.AsyncClient() as client:83 async with client.stream("GET", server.url) as response:84 pass8586 assert response.status_code == 20087 with pytest.raises(httpx.ResponseNotRead):88 response.content899091@pytest.mark.usefixtures("async_environment")92async def test_stream_request(server):93 async def hello_world():94 yield b"Hello, "95 yield b"world!"9697 async with httpx.AsyncClient() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 98why, one step back98 response = await client.request("POST", server.url, content=hello_world())Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 99–110why, one step back99 assert response.status_code == 200100101102@pytest.mark.usefixtures("async_environment")103async def test_raise_for_status(server):104 async with httpx.AsyncClient() as client:105 for status_code in (200, 400, 404, 500, 505):106 response = await client.request(107 "GET", server.url.copy_with(path=f"/status/{status_code}")108 )109110 if 400 <= status_code < 600:Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc
line 111why, one step back111 with pytest.raises(httpx.HTTPStatusError) as exc_info:Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 112–114why, one step back112 response.raise_for_status()113 assert exc_info.value.response == response114 else:Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16
line 115why, one step back115 assert response.raise_for_status() is None # type: ignoreVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 116–136why, one step back116117118@pytest.mark.usefixtures("async_environment")119async def test_options(server):120 async with httpx.AsyncClient() as client:121 response = await client.options(server.url)122 assert response.status_code == 200123 assert response.text == "Hello, world!"124125126@pytest.mark.usefixtures("async_environment")127async def test_head(server):128 async with httpx.AsyncClient() as client:129 response = await client.head(server.url)130 assert response.status_code == 200131 assert response.text == ""132133134@pytest.mark.usefixtures("async_environment")135async def test_put(server):136 async with httpx.AsyncClient() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 137why, one step back137 response = await client.put(server.url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 138–143why, one step back138 assert response.status_code == 200139140141@pytest.mark.usefixtures("async_environment")142async def test_patch(server):143 async with httpx.AsyncClient() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 144why, one step back144 response = await client.patch(server.url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 145–158why, one step back145 assert response.status_code == 200146147148@pytest.mark.usefixtures("async_environment")149async def test_delete(server):150 async with httpx.AsyncClient() as client:151 response = await client.delete(server.url)152 assert response.status_code == 200153 assert response.text == "Hello, world!"154155156@pytest.mark.usefixtures("async_environment")157async def test_100_continue(server):158 headers = {"Expect": "100-continue"}Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 159why, one step back159 content = b"Echo request body"Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 160–162why, one step back160161 async with httpx.AsyncClient() as client:162 response = await client.post(Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 163why, one step back163 server.url.copy_with(path="/echo_body"), headers=headers, content=contentVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 164–166why, one step back164 )165166 assert response.status_code == 200Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 167why, one step back167 assert response.content == contentContext managed transports (#1218)Tom Christiebefore adoption534400e
lines 168–206why, one step back168169170@pytest.mark.usefixtures("async_environment")171async def test_context_managed_transport():172 class Transport(httpcore.AsyncHTTPTransport):173 def __init__(self):174 self.events = []175176 async def aclose(self):177 # The base implementation of httpcore.AsyncHTTPTransport just178 # calls into `.aclose`, so simple transport cases can just override179 # this method for any cleanup, where more complex cases180 # might want to additionally override `__aenter__`/`__aexit__`.181 self.events.append("transport.aclose")182183 async def __aenter__(self):184 await super().__aenter__()185 self.events.append("transport.__aenter__")186187 async def __aexit__(self, *args):188 await super().__aexit__(*args)189 self.events.append("transport.__aexit__")190191 # Note that we're including 'proxies' here to *also* run through the192 # proxy context management, although we can't easily test that at the193 # moment, since we can't add proxies as transport instances.194 #195 # Once we have a more generalised Mount API we'll be able to remove this196 # in favour of ensuring all mounts are context managed, which will197 # also neccessarily include proxies.198 transport = Transport()199 async with httpx.AsyncClient(transport=transport, proxies="http://www.example.com"):200 pass201202 assert transport.events == [203 "transport.__aenter__",204 "transport.aclose",205 "transport.__aexit__",206 ]Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 207–208why, one step back207208Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 209–210why, one step back209def hello_world(request):210 return httpx.Response(200, text="Hello, world!")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 211–213why, one step back211212213@pytest.mark.usefixtures("async_environment")Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 214–215why, one step back214async def test_client_closed_state_using_implicit_open():215 client = httpx.AsyncClient(transport=MockTransport(hello_world))Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 216why, one step back216Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 217why, one step back217 assert not client.is_closedIssue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 218–222why, one step back218 await client.get("http://example.com")219220 assert not client.is_closed221 await client.aclose()222Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 223–225why, one step back223 assert client.is_closed224 with pytest.raises(RuntimeError):225 await client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 226why, one step back226Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 227why, one step back227Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 228why, one step back228@pytest.mark.usefixtures("async_environment")Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 229–230why, one step back229async def test_client_closed_state_using_with_block():230 async with httpx.AsyncClient(transport=MockTransport(hello_world)) as client:Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 231why, one step back231 assert not client.is_closedTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 232why, one step back232 await client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 233–234why, one step back233234 assert client.is_closedTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 235–236why, one step back235 with pytest.raises(RuntimeError):236 await client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 237–239why, one step back237238239@pytest.mark.usefixtures("async_environment")Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 240–242why, one step back240async def test_deleting_unclosed_async_client_causes_warning():241 client = httpx.AsyncClient(transport=MockTransport(hello_world))242 await client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 243why, one step back243 with pytest.warns(UserWarning):Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 244why, one step back244 del client