test_client.py
This file
Path
tests/client/test_client.py
273 lines · 8.1 KiB · Python
Last changed by
Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
History of this fileTake it
The walk stopped after 200 commits: lines older than 225f1e2 point at it.
Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 1–2why, one step back1from datetime import timedelta2Context managed transports (#1218)Tom Christiebefore adoption534400e
line 3why, one step back3import httpcoreUse URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 4–10why, one step back4import pytest56import httpx789def test_get(server):10 url = server.urlHTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5
line 11why, one step back11 with httpx.Client(http2=True) as http:Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 12–17why, one step back12 response = http.get(url)13 assert response.status_code == 20014 assert response.url == url15 assert response.content == b"Hello, world!"16 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 18why, one step back18 assert response.encoding is NoneUse URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 19–24why, one step back19 assert response.request.url == url20 assert response.headers21 assert response.is_redirect is False22 assert repr(response) == "<Response [200 OK]>"23 assert response.elapsed > timedelta(0)24Full coverage, with exception of URLLib3Transport (#1086)Tom Christiebefore adoption2d491c9
lines 25–27why, one step back25 with pytest.raises(httpx.NotRedirectResponse):26 response.next()27Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
line 28why, one step back28Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 29–37why, one step back29@pytest.mark.parametrize(30 "url",31 [32 pytest.param("invalid://example.org", id="scheme-not-http(s)"),33 pytest.param("://example.org", id="no-scheme"),34 pytest.param("http://", id="no-host"),35 ],36)37def test_get_invalid_url(server, url):Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
line 38why, one step back38 with httpx.Client() as client:Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 39why, one step back39 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 40why, one step back40 client.get(url)Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
lines 41–42why, one step back4142Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 43–57why, one step back43def test_build_request(server):44 url = server.url.copy_with(path="/echo_headers")45 headers = {"Custom-header": "value"}4647 with httpx.Client() as client:48 request = client.build_request("GET", url)49 request.headers.update(headers)50 response = client.send(request)5152 assert response.status_code == 20053 assert response.url == url5455 assert response.json()["Custom-header"] == "value"5657Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
lines 58–73why, one step back58def test_build_post_request(server):59 url = server.url.copy_with(path="/echo_headers")60 headers = {"Custom-header": "value"}6162 with httpx.Client() as client:63 request = client.build_request("POST", url)64 request.headers.update(headers)65 response = client.send(request)6667 assert response.status_code == 20068 assert response.url == url6970 assert response.json()["Content-length"] == "0"71 assert response.json()["Custom-header"] == "value"7273Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 74–75why, one step back74def test_post(server):75 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 76why, one step back76 response = client.post(server.url, content=b"Hello, world!")Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 77–123why, one step back77 assert response.status_code == 20078 assert response.reason_phrase == "OK"798081def test_post_json(server):82 with httpx.Client() as client:83 response = client.post(server.url, json={"text": "Hello, world!"})84 assert response.status_code == 20085 assert response.reason_phrase == "OK"868788def test_stream_response(server):89 with httpx.Client() as client:90 with client.stream("GET", server.url) as response:91 content = response.read()92 assert response.status_code == 20093 assert content == b"Hello, world!"949596def test_stream_iterator(server):97 body = b""9899 with httpx.Client() as client:100 with client.stream("GET", server.url) as response:101 for chunk in response.iter_bytes():102 body += chunk103104 assert response.status_code == 200105 assert body == b"Hello, world!"106107108def test_raw_iterator(server):109 body = b""110111 with httpx.Client() as client:112 with client.stream("GET", server.url) as response:113 for chunk in response.iter_raw():114 body += chunk115116 assert response.status_code == 200117 assert body == b"Hello, world!"118119120def test_raise_for_status(server):121 with httpx.Client() as client:122 for status_code in (200, 400, 404, 500, 505):123 response = client.request(Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 124why, one step back124 "GET", server.url.copy_with(path=f"/status/{status_code}")Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 125–126why, one step back125 )126 if 400 <= status_code < 600:Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc
line 127why, one step back127 with pytest.raises(httpx.HTTPStatusError) as exc_info:Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 128–129why, one step back128 response.raise_for_status()129 assert exc_info.value.response == responseIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 130why, one step back130 assert exc_info.value.request.url.path == f"/status/{status_code}"Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
line 131why, one step back131 else:Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16
line 132why, one step back132 assert response.raise_for_status() is None # type: ignoreUse URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 133–150why, one step back133134135def test_options(server):136 with httpx.Client() as client:137 response = client.options(server.url)138 assert response.status_code == 200139 assert response.reason_phrase == "OK"140141142def test_head(server):143 with httpx.Client() as client:144 response = client.head(server.url)145 assert response.status_code == 200146 assert response.reason_phrase == "OK"147148149def test_put(server):150 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 151why, one step back151 response = client.put(server.url, content=b"Hello, world!")Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 152–157why, one step back152 assert response.status_code == 200153 assert response.reason_phrase == "OK"154155156def test_patch(server):157 with httpx.Client() as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 158why, one step back158 response = client.patch(server.url, content=b"Hello, world!")Use URLLib3 configuration options for URLLib3 transport (#970)Tom Christiebefore adoption225f1e2
lines 159–177why, one step back159 assert response.status_code == 200160 assert response.reason_phrase == "OK"161162163def test_delete(server):164 with httpx.Client() as client:165 response = client.delete(server.url)166 assert response.status_code == 200167 assert response.reason_phrase == "OK"168169170def test_base_url(server):171 base_url = server.url172 with httpx.Client(base_url=base_url) as client:173 response = client.get("/")174 assert response.status_code == 200175 assert response.url == base_url176177Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
line 178why, one step back178def test_merge_absolute_url():Drop HSTS Preloading (#1110)Florimond Mancabefore adoption78cf16a
line 179why, one step back179 client = httpx.Client(base_url="https://www.example.com/")Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
line 180why, one step back180 request = client.build_request("GET", "http://www.example.com/")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 181why, one step back181 assert request.url == "http://www.example.com/"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 182–186why, one step back182183184def test_merge_relative_url():185 client = httpx.Client(base_url="https://www.example.com/")186 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 187why, one step back187 assert request.url == "https://www.example.com/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 188–192why, one step back188189190def test_merge_relative_url_with_path():191 client = httpx.Client(base_url="https://www.example.com/some/path")192 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 193why, one step back193 assert request.url == "https://www.example.com/some/path/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 194–198why, one step back194195196def test_merge_relative_url_with_dotted_path():197 client = httpx.Client(base_url="https://www.example.com/some/path")198 request = client.build_request("GET", "../testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 199why, one step back199 assert request.url == "https://www.example.com/some/testing/123"Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 200–209why, one step back200201202def test_pool_limits_deprecated():203 limits = httpx.Limits()204205 with pytest.warns(DeprecationWarning):206 httpx.Client(pool_limits=limits)207208 with pytest.warns(DeprecationWarning):209 httpx.AsyncClient(pool_limits=limits)Context managed transports (#1218)Tom Christiebefore adoption534400e
lines 210–247why, one step back210211212def test_context_managed_transport():213 class Transport(httpcore.SyncHTTPTransport):214 def __init__(self):215 self.events = []216217 def close(self):218 # The base implementation of httpcore.SyncHTTPTransport just219 # calls into `.close`, so simple transport cases can just override220 # this method for any cleanup, where more complex cases221 # might want to additionally override `__enter__`/`__exit__`.222 self.events.append("transport.close")223224 def __enter__(self):225 super().__enter__()226 self.events.append("transport.__enter__")227228 def __exit__(self, *args):229 super().__exit__(*args)230 self.events.append("transport.__exit__")231232 # Note that we're including 'proxies' here to *also* run through the233 # proxy context management, although we can't easily test that at the234 # moment, since we can't add proxies as transport instances.235 #236 # Once we have a more generalised Mount API we'll be able to remove this237 # in favour of ensuring all mounts are context managed, which will238 # also neccessarily include proxies.239 transport = Transport()240 with httpx.Client(transport=transport, proxies="http://www.example.com"):241 pass242243 assert transport.events == [244 "transport.__enter__",245 "transport.close",246 "transport.__exit__",247 ]Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 248–273why, one step back248249250def test_that_client_is_closed_by_default():251 client = httpx.Client()252253 assert client.is_closed254255256def test_that_send_cause_client_to_be_not_closed():257 client = httpx.Client()258259 client.get("http://example.com")260261 assert not client.is_closed262263264def test_that_client_is_not_closed_in_with_block():265 with httpx.Client() as client:266 assert not client.is_closed267268269def test_that_client_is_closed_after_with_block():270 with httpx.Client() as client:271 pass272273 assert client.is_closed