Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

210 lines · 6.3 KiB · Python

Last changed by

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

History of this file

Take it

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 1–7why, one step back
1from datetime import timedelta
23import pytest45import httpx67

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 8why, one step back
8def test_get(server):

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 9why, one step back
9 url = server.url

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

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 11why, one step back
11 response = http.get(url)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 12–23why, one step back
12 assert response.status_code == 200
13 assert response.url == url14 assert response.content == b"Hello, world!"15 assert response.text == "Hello, world!"16 assert response.http_version == "HTTP/1.1"17 assert response.encoding == "iso-8859-1"18 assert response.request.url == url19 assert response.headers20 assert response.is_redirect is False21 assert repr(response) == "<Response [200 OK]>"22 assert response.elapsed > timedelta(0)23

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

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 27why, one step back
27

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

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

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

line 37why, one step back
37 with httpx.Client() as client:

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

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

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

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

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

lines 40–41why, one step back
40
41

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 42why, one step back
42def test_build_request(server):

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 43–45why, one step back
43 url = server.url.copy_with(path="/echo_headers")
44 headers = {"Custom-header": "value"}45

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 46why, one step back
46 with httpx.Client() as client:

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 47–48why, one step back
47 request = client.build_request("GET", url)
48 request.headers.update(headers)

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 49why, one step back
49 response = client.send(request)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 50–56why, one step back
50
51 assert response.status_code == 20052 assert response.url == url5354 assert response.json()["Custom-header"] == "value"5556

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

lines 57–72why, one step back
57def test_build_post_request(server):
58 url = server.url.copy_with(path="/echo_headers")59 headers = {"Custom-header": "value"}6061 with httpx.Client() as client:62 request = client.build_request("POST", url)63 request.headers.update(headers)64 response = client.send(request)6566 assert response.status_code == 20067 assert response.url == url6869 assert response.json()["Content-length"] == "0"70 assert response.json()["Custom-header"] == "value"7172

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 73–75why, one step back
73def test_post(server):
74 with httpx.Client() as client:75 response = client.post(server.url, data=b"Hello, world!")

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 76–79why, one step back
76 assert response.status_code == 200
77 assert response.reason_phrase == "OK"7879

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 80–82why, one step back
80def test_post_json(server):
81 with httpx.Client() as client:82 response = client.post(server.url, json={"text": "Hello, world!"})

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 83–86why, one step back
83 assert response.status_code == 200
84 assert response.reason_phrase == "OK"8586

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 87–90why, one step back
87def test_stream_response(server):
88 with httpx.Client() as client:89 with client.stream("GET", server.url) as response:90 content = response.read()

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 91–94why, one step back
91 assert response.status_code == 200
92 assert content == b"Hello, world!"9394

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 95why, one step back
95def test_stream_iterator(server):

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 96–97why, one step back
96 body = b""
97

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 98–100why, one step back
98 with httpx.Client() as client:
99 with client.stream("GET", server.url) as response:100 for chunk in response.iter_bytes():

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 101–106why, one step back
101 body += chunk
102103 assert response.status_code == 200104 assert body == b"Hello, world!"105106

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 107why, one step back
107def test_raw_iterator(server):

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 108–109why, one step back
108 body = b""
109

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 110–112why, one step back
110 with httpx.Client() as client:
111 with client.stream("GET", server.url) as response:112 for chunk in response.iter_raw():

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 113–118why, one step back
113 body += chunk
114115 assert response.status_code == 200116 assert body == b"Hello, world!"117118

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 119–120why, one step back
119def test_raise_for_status(server):
120 with httpx.Client() as client:

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 121why, one step back
121 for status_code in (200, 400, 404, 500, 505):

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 122why, one step back
122 response = client.request(

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

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

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

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

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

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

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 130why, one step back
130 else:

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

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

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 132–133why, one step back
132
133

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 134–136why, one step back
134def test_options(server):
135 with httpx.Client() as client:136 response = client.options(server.url)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 137–140why, one step back
137 assert response.status_code == 200
138 assert response.reason_phrase == "OK"139140

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 141–143why, one step back
141def test_head(server):
142 with httpx.Client() as client:143 response = client.head(server.url)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 144–147why, one step back
144 assert response.status_code == 200
145 assert response.reason_phrase == "OK"146147

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 148–150why, one step back
148def test_put(server):
149 with httpx.Client() as client:150 response = client.put(server.url, data=b"Hello, world!")

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 151–154why, one step back
151 assert response.status_code == 200
152 assert response.reason_phrase == "OK"153154

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 155–157why, one step back
155def test_patch(server):
156 with httpx.Client() as client:157 response = client.patch(server.url, data=b"Hello, world!")

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 158–161why, one step back
158 assert response.status_code == 200
159 assert response.reason_phrase == "OK"160161

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 162–164why, one step back
162def test_delete(server):
163 with httpx.Client() as client:164 response = client.delete(server.url)

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 165–168why, one step back
165 assert response.status_code == 200
166 assert response.reason_phrase == "OK"167168

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 169why, one step back
169def test_base_url(server):

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

line 170why, one step back
170 base_url = server.url

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 171–172why, one step back
171 with httpx.Client(base_url=base_url) as client:
172 response = client.get("/")

Drop unused SSLConfig.with_overrides (#702)Tom Christiebefore adoptionfe86c0c

lines 173–176why, one step back
173 assert response.status_code == 200
174 assert response.url == base_url175176

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

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

Drop HSTS Preloading (#1110)Florimond Mancabefore adoption78cf16a

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

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 179–180why, one step back
179 request = client.build_request("GET", "http://www.example.com/")
180 assert request.url == httpx.URL("http://www.example.com/")

Deprecate URL.is_ssl (#1128)Tom Christiebefore adoption0e73be8

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

Base URL improvements (#1130)Tom Christiebefore adoptiona3392c6

lines 183–200why, one step back
183
184185def test_merge_relative_url():186 client = httpx.Client(base_url="https://www.example.com/")187 request = client.build_request("GET", "/testing/123")188 assert request.url == httpx.URL("https://www.example.com/testing/123")189190191def 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")194 assert request.url == httpx.URL("https://www.example.com/some/path/testing/123")195196197def 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")200 assert request.url == httpx.URL("https://www.example.com/some/testing/123")

Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925

lines 201–210why, one step back
201
202203def 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)