test_client.py
This file
Path
tests/client/test_client.py
276 lines · 8.3 KiB · Python
Last changed by
Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
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–11why, one step back8910def test_get(server):11 url = server.urlHTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5
line 12why, one step back12 with httpx.Client(http2=True) as http:Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 13–18why, one step back13 response = http.get(url)14 assert response.status_code == 20015 assert response.url == url16 assert response.content == b"Hello, world!"17 assert response.text == "Hello, world!"18 assert response.http_version == "HTTP/1.1"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 19why, one step back19 assert response.encoding is NoneVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 20–25why, one step back20 assert response.request.url == url21 assert response.headers22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)25Full coverage, with exception of URLLib3Transport (#1086)Tom Christiebefore adoption2d491c9
lines 26–28why, one step back26 with pytest.raises(httpx.NotRedirectResponse):27 response.next()28Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
line 29why, one step back29Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 30–38why, one step back30@pytest.mark.parametrize(31 "url",32 [33 pytest.param("invalid://example.org", id="scheme-not-http(s)"),34 pytest.param("://example.org", id="no-scheme"),35 pytest.param("http://", id="no-host"),36 ],37)38def test_get_invalid_url(server, url):Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
line 39why, one step back39 with httpx.Client() as client:Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 40why, one step back40 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 41why, one step back41 client.get(url)Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
lines 42–43why, one step back4243Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 44–58why, one step back44def test_build_request(server):45 url = server.url.copy_with(path="/echo_headers")46 headers = {"Custom-header": "value"}4748 with httpx.Client() as client:49 request = client.build_request("GET", url)50 request.headers.update(headers)51 response = client.send(request)5253 assert response.status_code == 20054 assert response.url == url5556 assert response.json()["Custom-header"] == "value"5758Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
lines 59–74why, one step back59def test_build_post_request(server):60 url = server.url.copy_with(path="/echo_headers")61 headers = {"Custom-header": "value"}6263 with httpx.Client() as client:64 request = client.build_request("POST", url)65 request.headers.update(headers)66 response = client.send(request)6768 assert response.status_code == 20069 assert response.url == url7071 assert response.json()["Content-length"] == "0"72 assert response.json()["Custom-header"] == "value"7374Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 75–76why, one step back75def test_post(server):76 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 77why, one step back77 response = client.post(server.url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 78–124why, one step back78 assert response.status_code == 20079 assert response.reason_phrase == "OK"808182def test_post_json(server):83 with httpx.Client() as client:84 response = client.post(server.url, json={"text": "Hello, world!"})85 assert response.status_code == 20086 assert response.reason_phrase == "OK"878889def test_stream_response(server):90 with httpx.Client() as client:91 with client.stream("GET", server.url) as response:92 content = response.read()93 assert response.status_code == 20094 assert content == b"Hello, world!"959697def test_stream_iterator(server):98 body = b""99100 with httpx.Client() as client:101 with client.stream("GET", server.url) as response:102 for chunk in response.iter_bytes():103 body += chunk104105 assert response.status_code == 200106 assert body == b"Hello, world!"107108109def test_raw_iterator(server):110 body = b""111112 with httpx.Client() as client:113 with client.stream("GET", server.url) as response:114 for chunk in response.iter_raw():115 body += chunk116117 assert response.status_code == 200118 assert body == b"Hello, world!"119120121def test_raise_for_status(server):122 with httpx.Client() as client:123 for status_code in (200, 400, 404, 500, 505):124 response = client.request(Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 125why, one step back125 "GET", server.url.copy_with(path=f"/status/{status_code}")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 126–127why, one step back126 )127 if 400 <= status_code < 600:Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc
line 128why, one step back128 with pytest.raises(httpx.HTTPStatusError) as exc_info:Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 129–130why, one step back129 response.raise_for_status()130 assert exc_info.value.response == responseIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 131why, one step back131 assert exc_info.value.request.url.path == f"/status/{status_code}"Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
line 132why, one step back132 else:Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16
line 133why, one step back133 assert response.raise_for_status() is None # type: ignoreVersion 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 134–151why, one step back134135136def test_options(server):137 with httpx.Client() as client:138 response = client.options(server.url)139 assert response.status_code == 200140 assert response.reason_phrase == "OK"141142143def test_head(server):144 with httpx.Client() as client:145 response = client.head(server.url)146 assert response.status_code == 200147 assert response.reason_phrase == "OK"148149150def test_put(server):151 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 152why, one step back152 response = client.put(server.url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 153–158why, one step back153 assert response.status_code == 200154 assert response.reason_phrase == "OK"155156157def test_patch(server):158 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 159why, one step back159 response = client.patch(server.url, content=b"Hello, world!")Version 0.13 (#971)Tom Christiebefore adoption2c2c6a7
lines 160–178why, one step back160 assert response.status_code == 200161 assert response.reason_phrase == "OK"162163164def test_delete(server):165 with httpx.Client() as client:166 response = client.delete(server.url)167 assert response.status_code == 200168 assert response.reason_phrase == "OK"169170171def test_base_url(server):172 base_url = server.url173 with httpx.Client(base_url=base_url) as client:174 response = client.get("/")175 assert response.status_code == 200176 assert response.url == base_url177178Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
line 179why, one step back179def test_merge_absolute_url():Drop HSTS Preloading (#1110)Florimond Mancabefore adoption78cf16a
line 180why, one step back180 client = httpx.Client(base_url="https://www.example.com/")Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
line 181why, one step back181 request = client.build_request("GET", "http://www.example.com/")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 182why, one step back182 assert request.url == "http://www.example.com/"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 183–187why, one step back183184185def test_merge_relative_url():186 client = httpx.Client(base_url="https://www.example.com/")187 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 188why, one step back188 assert request.url == "https://www.example.com/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 189–193why, one step back189190191def test_merge_relative_url_with_path():192 client = httpx.Client(base_url="https://www.example.com/some/path")193 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 194why, one step back194 assert request.url == "https://www.example.com/some/path/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 195–199why, one step back195196197def test_merge_relative_url_with_dotted_path():198 client = httpx.Client(base_url="https://www.example.com/some/path")199 request = client.build_request("GET", "../testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 200why, one step back200 assert request.url == "https://www.example.com/some/testing/123"Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 201–210why, one step back201202203def test_pool_limits_deprecated():204 limits = httpx.Limits()205206 with pytest.warns(DeprecationWarning):207 httpx.Client(pool_limits=limits)208209 with pytest.warns(DeprecationWarning):210 httpx.AsyncClient(pool_limits=limits)Context managed transports (#1218)Tom Christiebefore adoption534400e
lines 211–248why, one step back211212213def test_context_managed_transport():214 class Transport(httpcore.SyncHTTPTransport):215 def __init__(self):216 self.events = []217218 def close(self):219 # The base implementation of httpcore.SyncHTTPTransport just220 # calls into `.close`, so simple transport cases can just override221 # this method for any cleanup, where more complex cases222 # might want to additionally override `__enter__`/`__exit__`.223 self.events.append("transport.close")224225 def __enter__(self):226 super().__enter__()227 self.events.append("transport.__enter__")228229 def __exit__(self, *args):230 super().__exit__(*args)231 self.events.append("transport.__exit__")232233 # Note that we're including 'proxies' here to *also* run through the234 # proxy context management, although we can't easily test that at the235 # moment, since we can't add proxies as transport instances.236 #237 # Once we have a more generalised Mount API we'll be able to remove this238 # in favour of ensuring all mounts are context managed, which will239 # also neccessarily include proxies.240 transport = Transport()241 with httpx.Client(transport=transport, proxies="http://www.example.com"):242 pass243244 assert transport.events == [245 "transport.__enter__",246 "transport.close",247 "transport.__exit__",248 ]Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 249–250why, one step back249250Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 251–252why, one step back251def hello_world(request):252 return httpx.Response(200, text="Hello, world!")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 253–254why, one step back253254Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 255–256why, one step back255def test_client_closed_state_using_implicit_open():256 client = httpx.Client(transport=MockTransport(hello_world))Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 257why, one step back257Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 258why, one step back258 assert not client.is_closedIssue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 259–261why, one step back259 client.get("http://example.com")260261 assert not client.is_closedTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 262why, one step back262 client.close()Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 263why, one step back263Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 264–266why, one step back264 assert client.is_closed265 with pytest.raises(RuntimeError):266 client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 267why, one step back267Tighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 268–270why, one step back268269def test_client_closed_state_using_with_block():270 with httpx.Client(transport=MockTransport(hello_world)) as client:Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
line 271why, one step back271 assert not client.is_closedTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
line 272why, one step back272 client.get("http://example.com")Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 273–274why, one step back273274 assert client.is_closedTighten client closed-state behaviour (#1346)Tom Christiebefore adoption2a2bbe5
lines 275–276why, one step back275 with pytest.raises(RuntimeError):276 client.get("http://example.com")