Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

275 lines · 8.1 KiB · Python

Last changed by

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

History of this file

Take 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 back
1from datetime import timedelta
2

Context managed transports (#1218)Tom Christiebefore adoption534400e

line 3why, one step back
3import httpcore

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 4–10why, one step back
4import pytest
56import httpx789def test_get(server):10 url = server.url

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

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 19–24why, one step back
19 assert response.request.url == url
20 assert response.headers21 assert response.is_redirect is False22 assert repr(response) == "<Response [200 OK]>"23 assert response.elapsed > timedelta(0)24

Full coverage, with exception of URLLib3Transport (#1086)Tom Christiebefore adoption2d491c9

lines 25–27why, one step back
25 with pytest.raises(httpx.NotRedirectResponse):
26 response.next()27

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

line 28why, one step back
28

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

lines 29–37why, one step back
29@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 back
38 with httpx.Client() as client:

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

line 39why, one step back
39 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 40why, one step back
40 client.get(url)

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

lines 41–42why, one step back
41
42

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 43–57why, one step back
43def 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"5657

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

lines 58–73why, one step back
58def 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"7273

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

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

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

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 77–123why, one step back
77 assert response.status_code == 200
78 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 back
124 "GET", server.url.copy_with(path=f"/status/{status_code}")

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 125–126why, one step back
125 )
126 if 400 <= status_code < 600:

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

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

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

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

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

line 131why, one step back
131 else:

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

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

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 133–150why, one step back
133
134135def 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 back
151 response = client.put(server.url, content=b"Hello, world!")

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 152–157why, one step back
152 assert response.status_code == 200
153 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 back
158 response = client.patch(server.url, content=b"Hello, world!")

Add issue templates (#880)Florimond Mancabefore adoption4ef5de4

lines 159–177why, one step back
159 assert response.status_code == 200
160 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_url176177

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

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

Drop HSTS Preloading (#1110)Florimond Mancabefore adoption78cf16a

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

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

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

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

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

Deprecate URL.is_ssl (#1128)Tom Christiebefore adoption0e73be8

lines 182–183why, one step back
182 with pytest.warns(DeprecationWarning):
183 assert not request.url.is_ssl

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 184–188why, one step back
184
185186def 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 back
189 assert request.url == "https://www.example.com/testing/123"

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 190–194why, one step back
190
191192def 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 back
195 assert request.url == "https://www.example.com/some/path/testing/123"

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 196–200why, one step back
196
197198def 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 back
201 assert request.url == "https://www.example.com/some/testing/123"

Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925

lines 202–211why, one step back
202
203204def 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 back
212
213214def 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 back
250
251252def 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