Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

302 lines · 9.1 KiB · Python

Last changed by

Preserve header casing (#1338)Tom Christiebefore adoptionc725387

History of this file

Take it

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 8–11why, one step back
8
910def test_get(server):11 url = server.url

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

line 12why, one step back
12 with httpx.Client(http2=True) as http:

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 13–18why, one step back
13 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 back
19 assert response.encoding is None

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 20–26why, one step back
20 assert response.request.url == url
21 assert response.headers22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"24 assert response.elapsed > timedelta(0)2526

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

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

Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351

line 36why, one step back
36 with httpx.Client() 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 client.get(url)

Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351

lines 39–40why, one step back
39
40

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 41–55why, one step back
41def test_build_request(server):
42 url = server.url.copy_with(path="/echo_headers")43 headers = {"Custom-header": "value"}4445 with httpx.Client() as client:46 request = client.build_request("GET", url)47 request.headers.update(headers)48 response = client.send(request)4950 assert response.status_code == 20051 assert response.url == url5253 assert response.json()["Custom-header"] == "value"5455

Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a

lines 56–71why, one step back
56def test_build_post_request(server):
57 url = server.url.copy_with(path="/echo_headers")58 headers = {"Custom-header": "value"}5960 with httpx.Client() as client:61 request = client.build_request("POST", url)62 request.headers.update(headers)63 response = client.send(request)6465 assert response.status_code == 20066 assert response.url == url6768 assert response.json()["Content-length"] == "0"69 assert response.json()["Custom-header"] == "value"7071

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 72–73why, one step back
72def test_post(server):
73 with httpx.Client() as client:

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

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 75–121why, one step back
75 assert response.status_code == 200
76 assert response.reason_phrase == "OK"777879def test_post_json(server):80 with httpx.Client() as client:81 response = client.post(server.url, json={"text": "Hello, world!"})82 assert response.status_code == 20083 assert response.reason_phrase == "OK"848586def test_stream_response(server):87 with httpx.Client() as client:88 with client.stream("GET", server.url) as response:89 content = response.read()90 assert response.status_code == 20091 assert content == b"Hello, world!"929394def test_stream_iterator(server):95 body = b""9697 with httpx.Client() as client:98 with client.stream("GET", server.url) as response:99 for chunk in response.iter_bytes():100 body += chunk101102 assert response.status_code == 200103 assert body == b"Hello, world!"104105106def test_raw_iterator(server):107 body = b""108109 with httpx.Client() as client:110 with client.stream("GET", server.url) as response:111 for chunk in response.iter_raw():112 body += chunk113114 assert response.status_code == 200115 assert body == b"Hello, world!"116117118def test_raise_for_status(server):119 with httpx.Client() as client:120 for status_code in (200, 400, 404, 500, 505):121 response = client.request(

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

line 122why, one step back
122 "GET", server.url.copy_with(path=f"/status/{status_code}")

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 123–124why, one step back
123 )
124 if 400 <= status_code < 600:

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

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 126–127why, one step back
126 response.raise_for_status()
127 assert exc_info.value.response == response

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

line 128why, one step back
128 assert exc_info.value.request.url.path == f"/status/{status_code}"

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

line 129why, one step back
129 else:

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

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 131–148why, one step back
131
132133def test_options(server):134 with httpx.Client() as client:135 response = client.options(server.url)136 assert response.status_code == 200137 assert response.reason_phrase == "OK"138139140def test_head(server):141 with httpx.Client() as client:142 response = client.head(server.url)143 assert response.status_code == 200144 assert response.reason_phrase == "OK"145146147def test_put(server):148 with httpx.Client() as client:

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

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 150–155why, one step back
150 assert response.status_code == 200
151 assert response.reason_phrase == "OK"152153154def test_patch(server):155 with httpx.Client() as client:

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

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

Add urllib3proxy transport to init (#979)Tom Christiebefore adoptionc30acf0

lines 157–175why, one step back
157 assert response.status_code == 200
158 assert response.reason_phrase == "OK"159160161def test_delete(server):162 with httpx.Client() as client:163 response = client.delete(server.url)164 assert response.status_code == 200165 assert response.reason_phrase == "OK"166167168def test_base_url(server):169 base_url = server.url170 with httpx.Client(base_url=base_url) as client:171 response = client.get("/")172 assert response.status_code == 200173 assert response.url == base_url174175

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

line 176why, one step back
176def test_merge_absolute_url():

Drop HSTS Preloading (#1110)Florimond Mancabefore adoption78cf16a

line 177why, one step back
177 client = httpx.Client(base_url="https://www.example.com/")

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

line 178why, one step back
178 request = client.build_request("GET", "http://www.example.com/")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 179why, one step back
179 assert request.url == "http://www.example.com/"

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 180–184why, one step back
180
181182def test_merge_relative_url():183 client = httpx.Client(base_url="https://www.example.com/")184 request = client.build_request("GET", "/testing/123")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 185why, one step back
185 assert request.url == "https://www.example.com/testing/123"

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 186–190why, one step back
186
187188def test_merge_relative_url_with_path():189 client = httpx.Client(base_url="https://www.example.com/some/path")190 request = client.build_request("GET", "/testing/123")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 191why, one step back
191 assert request.url == "https://www.example.com/some/path/testing/123"

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 192–196why, one step back
192
193194def test_merge_relative_url_with_dotted_path():195 client = httpx.Client(base_url="https://www.example.com/some/path")196 request = client.build_request("GET", "../testing/123")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 197why, one step back
197 assert request.url == "https://www.example.com/some/testing/123"

Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925

lines 198–207why, one step back
198
199200def test_pool_limits_deprecated():201 limits = httpx.Limits()202203 with pytest.warns(DeprecationWarning):204 httpx.Client(pool_limits=limits)205206 with pytest.warns(DeprecationWarning):207 httpx.AsyncClient(pool_limits=limits)

Context managed transports (#1218)Tom Christiebefore adoption534400e

lines 208–245why, one step back
208
209210def test_context_managed_transport():211 class Transport(httpcore.SyncHTTPTransport):212 def __init__(self):213 self.events = []214215 def close(self):216 # The base implementation of httpcore.SyncHTTPTransport just217 # calls into `.close`, so simple transport cases can just override218 # this method for any cleanup, where more complex cases219 # might want to additionally override `__enter__`/`__exit__`.220 self.events.append("transport.close")221222 def __enter__(self):223 super().__enter__()224 self.events.append("transport.__enter__")225226 def __exit__(self, *args):227 super().__exit__(*args)228 self.events.append("transport.__exit__")229230 # Note that we're including 'proxies' here to *also* run through the231 # proxy context management, although we can't easily test that at the232 # moment, since we can't add proxies as transport instances.233 #234 # Once we have a more generalised Mount API we'll be able to remove this235 # in favour of ensuring all mounts are context managed, which will236 # also neccessarily include proxies.237 transport = Transport()238 with httpx.Client(transport=transport, proxies="http://www.example.com"):239 pass240241 assert transport.events == [242 "transport.__enter__",243 "transport.close",244 "transport.__exit__",245 ]

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

lines 246–247why, one step back
246
247

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

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

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

lines 250–251why, one step back
250
251

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

lines 252–253why, one step back
252def test_client_closed_state_using_implicit_open():
253 client = httpx.Client(transport=MockTransport(hello_world))

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

line 254why, one step back
254

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

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

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

lines 256–258why, one step back
256 client.get("http://example.com")
257258 assert not client.is_closed

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

line 259why, one step back
259 client.close()

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

line 260why, one step back
260

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

lines 261–263why, one step back
261 assert client.is_closed
262 with pytest.raises(RuntimeError):263 client.get("http://example.com")

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

line 264why, one step back
264

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

lines 265–267why, one step back
265
266def test_client_closed_state_using_with_block():267 with httpx.Client(transport=MockTransport(hello_world)) as client:

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

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

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

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

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

lines 270–271why, one step back
270
271 assert client.is_closed

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

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

Preserve header casing (#1338)Tom Christiebefore adoptionc725387

lines 274–302why, one step back
274
275276def echo_raw_headers(request: httpx.Request) -> httpx.Response:277 data = [278 (name.decode("ascii"), value.decode("ascii"))279 for name, value in request.headers.raw280 ]281 return httpx.Response(200, json=data)282283284def test_raw_client_header():285 """286 Set a header in the Client.287 """288 url = "http://example.org/echo_headers"289 headers = {"Example-Header": "example-value"}290291 client = httpx.Client(transport=MockTransport(echo_raw_headers), headers=headers)292 response = client.get(url)293294 assert response.status_code == 200295 assert response.json() == [296 ["Host", "example.org"],297 ["Accept", "*/*"],298 ["Accept-Encoding", "gzip, deflate, br"],299 ["Connection", "keep-alive"],300 ["User-Agent", f"python-httpx/{httpx.__version__}"],301 ["Example-Header", "example-value"],302 ]