Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

398 lines · 11.6 KiB · Python

Last changed by

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

History of this file

Take it

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 1–7why, one step back
1import datetime
2import json3from unittest import mock45import pytest67import httpx

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

Update advanced.mdTom Christiebefore adoptionddc4885

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")

Update advanced.mdTom Christiebefore adoptionddc4885

line 11why, one step back
11

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

line 12why, one step back
12

Update advanced.mdTom Christiebefore adoptionddc4885

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

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

lines 32–35why, one step back
32 assert not response.is_error
333435def 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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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)

Update advanced.mdTom Christiebefore adoptionddc4885

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,

Update advanced.mdTom Christiebefore adoptionddc4885

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 )

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 127–141why, one step back
127def test_read():
128 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)129130 assert response.status_code == 200131 assert response.text == "Hello, world!"132 assert response.encoding == "ascii"133 assert response.is_closed134135 content = response.read()136137 assert content == b"Hello, world!"138 assert response.content == b"Hello, world!"139 assert response.is_closed140141

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 143why, one step back
143async def test_aread():

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 145–150why, one step back
145
146 assert response.status_code == 200147 assert response.text == "Hello, world!"148 assert response.encoding == "ascii"149 assert response.is_closed150

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 152–157why, one step back
152
153 assert content == b"Hello, world!"154 assert response.content == b"Hello, world!"155 assert response.is_closed156157

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 158–167why, one step back
158def test_iter_raw():
159 stream = IteratorStream(iterator=streaming_body())160 response = httpx.Response(200, stream=stream, request=REQUEST)161162 raw = b""163 for part in response.iter_raw():164 raw += part165 assert raw == b"Hello, world!"166167

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 169–171why, one step back
169async def test_aiter_raw():
170 stream = AsyncIteratorStream(aiterator=async_streaming_body())171 response = httpx.Response(200, stream=stream, request=REQUEST)

Update advanced.mdTom Christiebefore adoptionddc4885

lines 172–173why, one step back
172
173 raw = b""

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 175–178why, one step back
175 raw += part
176 assert raw == b"Hello, world!"177178

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 179–187why, one step back
179def test_iter_bytes():
180 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)181182 content = b""183 for part in response.iter_bytes():184 content += part185 assert content == b"Hello, world!"186187

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 189why, one step back
189async def test_aiter_bytes():

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 194–197why, one step back
194 content += part
195 assert content == b"Hello, world!"196197

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 198why, one step back
198def test_iter_text():

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

line 200why, one step back
200

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 201–204why, one step back
201 content = ""
202 for part in response.iter_text():203 content += part204 assert content == "Hello, world!"

Update advanced.mdTom Christiebefore adoptionddc4885

line 205why, one step back
205

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 206–210why, one step back
206
207@pytest.mark.asyncio208async def test_aiter_text():209 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)210

Update advanced.mdTom Christiebefore adoptionddc4885

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 213–216why, one step back
213 content += part
214 assert content == "Hello, world!"215216

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 217why, one step back
217def test_iter_lines():

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

line 219why, one step back
219

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 220–223why, one step back
220 content = []
221 for line in response.iter_lines():222 content.append(line)223 assert content == ["Hello,\n", "world!"]

Update advanced.mdTom Christiebefore adoptionddc4885

line 224why, one step back
224

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 225–229why, one step back
225
226@pytest.mark.asyncio227async def test_aiter_lines():228 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)229

Update advanced.mdTom Christiebefore adoptionddc4885

line 230why, one step back
230 content = []

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 232–235why, one step back
232 content.append(line)
233 assert content == ["Hello,\n", "world!"]234235

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 236–238why, one step back
236def test_sync_streaming_response():
237 stream = IteratorStream(iterator=streaming_body())238 response = httpx.Response(200, stream=stream, request=REQUEST)

Update advanced.mdTom Christiebefore adoptionddc4885

line 239why, one step back
239

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 240–241why, one step back
240 assert response.status_code == 200
241 assert not response.is_closed

Update advanced.mdTom Christiebefore adoptionddc4885

line 242why, one step back
242

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 243–244why, one step back
243 content = response.read()
244

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 246–247why, one step back
246 assert response.content == b"Hello, world!"
247 assert response.is_closed

Update advanced.mdTom Christiebefore adoptionddc4885

lines 248–250why, one step back
248
249250@pytest.mark.asyncio

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 251why, one step back
251async def test_async_streaming_response():

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

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 254–257why, one step back
254
255 assert response.status_code == 200256 assert not response.is_closed257

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 259–264why, one step back
259
260 assert content == b"Hello, world!"261 assert response.content == b"Hello, world!"262 assert response.is_closed263264

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 265–276why, one step back
265def test_cannot_read_after_stream_consumed():
266 stream = IteratorStream(iterator=streaming_body())267 response = httpx.Response(200, stream=stream, request=REQUEST)268269 content = b""270 for part in response.iter_bytes():271 content += part272273 with pytest.raises(httpx.StreamConsumed):274 response.read()275276

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 278why, one step back
278async def test_cannot_aread_after_stream_consumed():

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

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 281–282why, one step back
281
282 content = b""

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 284–286why, one step back
284 content += part
285286 with pytest.raises(httpx.StreamConsumed):

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 288–289why, one step back
288
289

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 290–306why, one step back
290def test_cannot_read_after_response_closed():
291 is_closed = False292293 def close_func():294 nonlocal is_closed295 is_closed = True296297 stream = IteratorStream(iterator=streaming_body(), close_func=close_func)298 response = httpx.Response(200, stream=stream, request=REQUEST)299300 response.close()301 assert is_closed302303 with pytest.raises(httpx.ResponseClosed):304 response.read()305306

Update advanced.mdTom Christiebefore adoptionddc4885

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 308–317why, one step back
308async def test_cannot_aread_after_response_closed():
309 is_closed = False310311 async def close_func():312 nonlocal is_closed313 is_closed = True314315 stream = AsyncIteratorStream(316 aiterator=async_streaming_body(), close_func=close_func317 )

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

line 319why, one step back
319

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

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

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 321why, one step back
321 assert is_closed

Update advanced.mdTom Christiebefore adoptionddc4885

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 325–326why, one step back
325
326

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

lines 327–335why, one step back
327@pytest.mark.asyncio
328async def test_elapsed_not_available_until_closed():329 stream = AsyncIteratorStream(aiterator=async_streaming_body())330 response = httpx.Response(200, stream=stream, request=REQUEST)331332 with pytest.raises(RuntimeError):333 response.elapsed334335

Update advanced.mdTom Christiebefore adoptionddc4885

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 338–346why, one step back
338 assert response.status_code == 600
339 assert response.reason_phrase == ""340 assert response.text == ""341342343def test_json_with_specified_encoding():344 data = {"greeting": "hello", "recipient": "world"}345 content = json.dumps(data).encode("utf-16")346 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 348–354why, one step back
348 assert response.json() == data
349350351def test_json_with_options():352 data = {"greeting": "hello", "recipient": "world", "amount": 1}353 content = json.dumps(data).encode("utf-16")354 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

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

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

lines 364–371why, one step back
364 assert response.json() == data
365366367def test_json_without_specified_encoding_decode_error():368 data = {"greeting": "hello", "recipient": "world"}369 content = json.dumps(data).encode("utf-32-be")370 headers = {"Content-Type": "application/json"}371 # force incorrect guess from `guess_json_utf` to trigger error

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

line 372why, one step back
372 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):

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

lines 373–375why, one step back
373 response = httpx.Response(
374 200, content=content, headers=headers, request=REQUEST375 )

Update advanced.mdTom Christiebefore adoptionddc4885

lines 376–396why, one step back
376 with pytest.raises(json.JSONDecodeError):
377 response.json()378379380@pytest.mark.parametrize(381 "headers, expected",382 [383 (384 {"Link": "<https://example.com>; rel='preload'"},385 {"preload": {"rel": "preload", "url": "https://example.com"}},386 ),387 (388 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},389 {390 "hub": {"url": "/hub", "rel": "hub"},391 "self": {"url": "/resource", "rel": "self"},392 },393 ),394 ],395)396def test_link_headers(headers, expected):

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

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

Update advanced.mdTom Christiebefore adoptionddc4885

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