Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

309 lines · 9.3 KiB · Python

Last changed by

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

History of this file

Take it

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

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 1why, one step back
1import datetime

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 2–7why, one step back
2import json
3from unittest import mock45import pytest67import httpx

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 8why, one step back
8from httpx.content_streams import AsyncIteratorStream

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 9why, one step back
9

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 10why, one step back
10REQUEST = httpx.Request("GET", "https://example.org")

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 11why, one step back
11

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 12why, one step back
12

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 13–22why, one step back
13def streaming_body():
14 yield b"Hello, "15 yield b"world!"161718async def async_streaming_body():19 yield b"Hello, "20 yield b"world!"2122

response.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e

lines 23–24why, one step back
23@pytest.mark.asyncio
24async def test_response():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 25why, one step back
25 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

response.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e

line 26why, one step back
26

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 27–29why, one step back
27 assert response.status_code == 200
28 assert response.reason_phrase == "OK"29 assert response.text == "Hello, world!"

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 30why, one step back
30 assert response.request is REQUEST

response.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e

line 31why, one step back
31 assert response.elapsed >= datetime.timedelta(0)

Add Response.is_error (#574)Tom Christiebefore adoptionfdaa012

line 32why, one step back
32 assert not response.is_error

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 33–35why, one step back
33
3435def test_response_repr():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 36why, one step back
36 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 37–45why, one step back
37 assert repr(response) == "<Response [200 OK]>"
383940def test_response_content_type_encoding():41 """42 Use the charset encoding in the Content-Type header if possible.43 """44 headers = {"Content-Type": "text-plain; charset=latin-1"}45 content = "Latin 1: ÿ".encode("latin-1")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 46why, one step back
46 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 47–55why, one step back
47 assert response.text == "Latin 1: ÿ"
48 assert response.encoding == "latin-1"495051def test_response_autodetect_encoding():52 """53 Autodetect encoding if there is no charset info in a Content-Type header.54 """55 content = "おはようございます。".encode("EUC-JP")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 56why, one step back
56 response = httpx.Response(200, content=content, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 57–66why, one step back
57 assert response.text == "おはようございます。"
58 assert response.encoding == "EUC-JP"596061def test_response_fallback_to_autodetect():62 """63 Fallback to autodetection if we get an invalid charset in the Content-Type header.64 """65 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}66 content = "おはようございます。".encode("EUC-JP")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 67why, one step back
67 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 68–78why, one step back
68 assert response.text == "おはようございます。"
69 assert response.encoding == "EUC-JP"707172def test_response_default_text_encoding():73 """74 A media type of 'text/*' with no charset should default to ISO-8859-1.75 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.176 """77 content = b"Hello, world!"78 headers = {"Content-Type": "text/plain"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 79why, one step back
79 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 80–88why, one step back
80 assert response.status_code == 200
81 assert response.encoding == "iso-8859-1"82 assert response.text == "Hello, world!"838485def test_response_default_encoding():86 """87 Default to utf-8 if all else fails.88 """

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 89why, one step back
89 response = httpx.Response(200, content=b"", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 90–98why, one step back
90 assert response.text == ""
91 assert response.encoding == "utf-8"929394def test_response_non_text_encoding():95 """96 Default to apparent encoding for non-text content-type headers.97 """98 headers = {"Content-Type": "image/png"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 99why, one step back
99 response = httpx.Response(200, content=b"xyz", headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 100–108why, one step back
100 assert response.text == "xyz"
101 assert response.encoding == "ascii"102103104def test_response_set_explicit_encoding():105 headers = {106 "Content-Type": "text-plain; charset=utf-8"107 } # Deliberately incorrect charset108 response = httpx.Response(

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 109why, one step back
109 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers, request=REQUEST,

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 110–116why, one step back
110 )
111 response.encoding = "latin-1"112 assert response.text == "Latin 1: ÿ"113 assert response.encoding == "latin-1"114115116def test_response_force_encoding():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 117–119why, one step back
117 response = httpx.Response(
118 200, content="Snowman: ☃".encode("utf-8"), request=REQUEST119 )

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 120–126why, one step back
120 response.encoding = "iso-8859-1"
121 assert response.status_code == 200122 assert response.reason_phrase == "OK"123 assert response.text == "Snowman: â\x98\x83"124 assert response.encoding == "iso-8859-1"125126

Drop sync (#544)Tom Christiebefore adoption206c537

lines 127–128why, one step back
127@pytest.mark.asyncio
128async def test_read_response():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 129why, one step back
129 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 130–135why, one step back
130
131 assert response.status_code == 200132 assert response.text == "Hello, world!"133 assert response.encoding == "ascii"134 assert response.is_closed135

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 136why, one step back
136 content = await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 137–142why, one step back
137
138 assert content == b"Hello, world!"139 assert response.content == b"Hello, world!"140 assert response.is_closed141142

Drop sync (#544)Tom Christiebefore adoption206c537

lines 143–144why, one step back
143@pytest.mark.asyncio
144async def test_raw_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 145why, one step back
145 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 146–147why, one step back
146
147 raw = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 148why, one step back
148 async for part in response.aiter_raw():

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 149–152why, one step back
149 raw += part
150 assert raw == b"Hello, world!"151152

Drop sync (#544)Tom Christiebefore adoption206c537

line 153why, one step back
153@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 154why, one step back
154async def test_bytes_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 155why, one step back
155 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 156–157why, one step back
156
157 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 158why, one step back
158 async for part in response.aiter_bytes():

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 159–162why, one step back
159 content += part
160 assert content == b"Hello, world!"161162

Drop sync (#544)Tom Christiebefore adoption206c537

line 163why, one step back
163@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 164why, one step back
164async def test_text_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 165why, one step back
165 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 166why, one step back
166

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 167why, one step back
167 await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 168why, one step back
168

Drop sync (#544)Tom Christiebefore adoption206c537

line 169why, one step back
169 content = ""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 170why, one step back
170 async for part in response.aiter_text():

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 171why, one step back
171 content += part

Drop sync (#544)Tom Christiebefore adoption206c537

line 172why, one step back
172 assert content == "Hello, world!"

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 173–175why, one step back
173
174175@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 176why, one step back
176async def test_lines_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 177why, one step back
177 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

line 178why, one step back
178

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 179why, one step back
179 await response.aread()

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 180–181why, one step back
180
181 content = []

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 182why, one step back
182 async for line in response.aiter_lines():

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 183–187why, one step back
183 content.append(line)
184 assert content == ["Hello,\n", "world!"]185186187@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 188why, one step back
188async def test_stream_interface_after_read():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 189why, one step back
189 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 190why, one step back
190

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 191why, one step back
191 await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 192–193why, one step back
192
193 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 194why, one step back
194 async for part in response.aiter_bytes():

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 195–199why, one step back
195 content += part
196 assert content == b"Hello, world!"197198199@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 200why, one step back
200async def test_streaming_response():

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 201why, one step back
201 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 202why, one step back
202 response = httpx.Response(200, stream=stream, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 203–206why, one step back
203
204 assert response.status_code == 200205 assert not response.is_closed206

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 207why, one step back
207 content = await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 208–214why, one step back
208
209 assert content == b"Hello, world!"210 assert response.content == b"Hello, world!"211 assert response.is_closed212213214@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 215why, one step back
215async def test_cannot_read_after_stream_consumed():

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 216why, one step back
216 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 217why, one step back
217 response = httpx.Response(200, stream=stream, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 218–219why, one step back
218
219 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 220why, one step back
220 async for part in response.aiter_bytes():

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 221–223why, one step back
221 content += part
222223 with pytest.raises(httpx.StreamConsumed):

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 224why, one step back
224 await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 225–227why, one step back
225
226227@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 228why, one step back
228async def test_cannot_read_after_response_closed():

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 229why, one step back
229 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 230why, one step back
230 response = httpx.Response(200, stream=stream, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 231why, one step back
231

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 232why, one step back
232 await response.aclose()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 233–234why, one step back
233
234 with pytest.raises(httpx.ResponseClosed):

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 235why, one step back
235 await response.aread()

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 236–237why, one step back
236
237

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

lines 238–246why, one step back
238@pytest.mark.asyncio
239async def test_elapsed_not_available_until_closed():240 stream = AsyncIteratorStream(aiterator=async_streaming_body())241 response = httpx.Response(200, stream=stream, request=REQUEST)242243 with pytest.raises(RuntimeError):244 response.elapsed245246

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 247why, one step back
247def test_unknown_status_code():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 248why, one step back
248 response = httpx.Response(600, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 249–257why, one step back
249 assert response.status_code == 600
250 assert response.reason_phrase == ""251 assert response.text == ""252253254def test_json_with_specified_encoding():255 data = {"greeting": "hello", "recipient": "world"}256 content = json.dumps(data).encode("utf-16")257 headers = {"Content-Type": "application/json, charset=utf-16"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 258why, one step back
258 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 259–265why, one step back
259 assert response.json() == data
260261262def test_json_with_options():263 data = {"greeting": "hello", "recipient": "world", "amount": 1}264 content = json.dumps(data).encode("utf-16")265 headers = {"Content-Type": "application/json, charset=utf-16"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 266why, one step back
266 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 267–273why, one step back
267 assert response.json(parse_int=str)["amount"] == "1"
268269270def test_json_without_specified_encoding():271 data = {"greeting": "hello", "recipient": "world"}272 content = json.dumps(data).encode("utf-32-be")273 headers = {"Content-Type": "application/json"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 274why, one step back
274 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 275–283why, one step back
275 assert response.json() == data
276277278def test_json_without_specified_encoding_decode_error():279 data = {"greeting": "hello", "recipient": "world"}280 content = json.dumps(data).encode("utf-32-be")281 headers = {"Content-Type": "application/json"}282 # force incorrect guess from `guess_json_utf` to trigger error283 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 284–286why, one step back
284 response = httpx.Response(
285 200, content=content, headers=headers, request=REQUEST286 )

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

lines 287–307why, one step back
287 with pytest.raises(json.JSONDecodeError):
288 response.json()289290291@pytest.mark.parametrize(292 "headers, expected",293 [294 (295 {"Link": "<https://example.com>; rel='preload'"},296 {"preload": {"rel": "preload", "url": "https://example.com"}},297 ),298 (299 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},300 {301 "hub": {"url": "/hub", "rel": "hub"},302 "self": {"url": "/resource", "rel": "self"},303 },304 ),305 ],306)307def test_link_headers(headers, expected):

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 308why, one step back
308 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)

Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352

line 309why, one step back
309 assert response.links == expected