test_client.py
This file
Path
tests/client/test_client.py
275 lines · 8.1 KiB · Python
Last changed by
Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
History of this fileTake it
The walk stopped after 200 commits: lines older than 4ef5de4 point at it.
Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
lines 1–2why, one step back1from datetime import timedelta2Context managed transports (#1218)Tom Christiebefore adoption534400e
line 3why, one step back3import httpcoreAdd issue templates (#880)Florimond Mancabefore adoption4ef5de4
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:Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
lines 12–24why, 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"18 assert response.encoding == "iso-8859-1"19 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()27Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
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 back4142Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
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"7273Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
lines 74–123why, one step back74def test_post(server):75 with httpx.Client() as client:76 response = client.post(server.url, data=b"Hello, world!")77 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}")Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
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:Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
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}"Add issue templates (#880)Florimond Mancabefore adoption4ef5de4
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: ignoreAdd issue templates (#880)Florimond Mancabefore adoption4ef5de4
lines 133–177why, 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:151 response = client.put(server.url, data=b"Hello, world!")152 assert response.status_code == 200153 assert response.reason_phrase == "OK"154155156def test_patch(server):157 with httpx.Client() as client:158 response = client.patch(server.url, data=b"Hello, world!")159 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/"Deprecate URL.is_ssl (#1128)Tom Christiebefore adoption0e73be8
lines 182–183why, one step back182 with pytest.warns(DeprecationWarning):183 assert not request.url.is_sslBase URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 184–188why, one step back184185186def test_merge_relative_url():187 client = httpx.Client(base_url="https://www.example.com/")188 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 189why, one step back189 assert request.url == "https://www.example.com/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 190–194why, one step back190191192def test_merge_relative_url_with_path():193 client = httpx.Client(base_url="https://www.example.com/some/path")194 request = client.build_request("GET", "/testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 195why, one step back195 assert request.url == "https://www.example.com/some/path/testing/123"Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6
lines 196–200why, one step back196197198def test_merge_relative_url_with_dotted_path():199 client = httpx.Client(base_url="https://www.example.com/some/path")200 request = client.build_request("GET", "../testing/123")Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209
line 201why, one step back201 assert request.url == "https://www.example.com/some/testing/123"Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 202–211why, one step back202203204def test_pool_limits_deprecated():205 limits = httpx.Limits()206207 with pytest.warns(DeprecationWarning):208 httpx.Client(pool_limits=limits)209210 with pytest.warns(DeprecationWarning):211 httpx.AsyncClient(pool_limits=limits)Context managed transports (#1218)Tom Christiebefore adoption534400e
lines 212–249why, one step back212213214def test_context_managed_transport():215 class Transport(httpcore.SyncHTTPTransport):216 def __init__(self):217 self.events = []218219 def close(self):220 # The base implementation of httpcore.SyncHTTPTransport just221 # calls into `.close`, so simple transport cases can just override222 # this method for any cleanup, where more complex cases223 # might want to additionally override `__enter__`/`__exit__`.224 self.events.append("transport.close")225226 def __enter__(self):227 super().__enter__()228 self.events.append("transport.__enter__")229230 def __exit__(self, *args):231 super().__exit__(*args)232 self.events.append("transport.__exit__")233234 # Note that we're including 'proxies' here to *also* run through the235 # proxy context management, although we can't easily test that at the236 # moment, since we can't add proxies as transport instances.237 #238 # Once we have a more generalised Mount API we'll be able to remove this239 # in favour of ensuring all mounts are context managed, which will240 # also neccessarily include proxies.241 transport = Transport()242 with httpx.Client(transport=transport, proxies="http://www.example.com"):243 pass244245 assert transport.events == [246 "transport.__enter__",247 "transport.close",248 "transport.__exit__",249 ]Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 250–275why, one step back250251252def test_that_client_is_closed_by_default():253 client = httpx.Client()254255 assert client.is_closed256257258def test_that_send_cause_client_to_be_not_closed():259 client = httpx.Client()260261 client.get("http://example.com")262263 assert not client.is_closed264265266def test_that_client_is_not_closed_in_with_block():267 with httpx.Client() as client:268 assert not client.is_closed269270271def test_that_client_is_closed_after_with_block():272 with httpx.Client() as client:273 pass274275 assert client.is_closed