Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

744 lines · 18.9 KiB · Python

Last changed by

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

History of this file

Take it

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

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 1–3why, one step back
1import json
2from unittest import mock3

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 4why, one step back
4import brotli

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 5–9why, one step back
5import pytest
67import httpx89

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 10–15why, one step back
10class StreamingBody:
11 def __iter__(self):12 yield b"Hello, "13 yield b"world!"1415

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 16–25why, one step back
16def streaming_body():
17 yield b"Hello, "18 yield b"world!"192021async def async_streaming_body():22 yield b"Hello, "23 yield b"world!"2425

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 26why, one step back
26def test_response():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 27–31why, one step back
27 response = httpx.Response(
28 200,29 content=b"Hello, world!",30 request=httpx.Request("GET", "https://example.org"),31 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 32–35why, one step back
32
33 assert response.status_code == 20034 assert response.reason_phrase == "OK"35 assert response.text == "Hello, world!"

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 36–37why, one step back
36 assert response.request.method == "GET"
37 assert response.request.url == "https://example.org"

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 38–40why, one step back
38 assert not response.is_error
3940

Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd

lines 41–46why, one step back
41def test_response_content():
42 response = httpx.Response(200, content="Hello, world!")4344 assert response.status_code == 20045 assert response.reason_phrase == "OK"46 assert response.text == "Hello, world!"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 47why, one step back
47 assert response.headers == {"Content-Length": "13"}

Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd

lines 48–49why, one step back
48
49

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 50–55why, one step back
50def test_response_text():
51 response = httpx.Response(200, text="Hello, world!")5253 assert response.status_code == 20054 assert response.reason_phrase == "OK"55 assert response.text == "Hello, world!"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 56–59why, one step back
56 assert response.headers == {
57 "Content-Length": "13",58 "Content-Type": "text/plain; charset=utf-8",59 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 60–67why, one step back
60
6162def test_response_html():63 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")6465 assert response.status_code == 20066 assert response.reason_phrase == "OK"67 assert response.text == "<html><body>Hello, world!</html></body>"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 68–71why, one step back
68 assert response.headers == {
69 "Content-Length": "39",70 "Content-Type": "text/html; charset=utf-8",71 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 72–79why, one step back
72
7374def test_response_json():75 response = httpx.Response(200, json={"hello": "world"})7677 assert response.status_code == 20078 assert response.reason_phrase == "OK"79 assert response.json() == {"hello": "world"}

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 80–83why, one step back
80 assert response.headers == {
81 "Content-Length": "18",82 "Content-Type": "application/json",83 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 84–85why, one step back
84
85

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 86why, one step back
86def test_raise_for_status():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 87–88why, one step back
87 request = httpx.Request("GET", "https://example.org")
88

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 89why, one step back
89 # 2xx status codes are not an error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 90why, one step back
90 response = httpx.Response(200, request=request)

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 91–93why, one step back
91 response.raise_for_status()
9293 # 4xx status codes are a client error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 94why, one step back
94 response = httpx.Response(403, request=request)

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 95–98why, one step back
95 with pytest.raises(httpx.HTTPStatusError):
96 response.raise_for_status()9798 # 5xx status codes are a server error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 99why, one step back
99 response = httpx.Response(500, request=request)

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 100–109why, one step back
100 with pytest.raises(httpx.HTTPStatusError):
101 response.raise_for_status()102103 # Calling .raise_for_status without setting a request instance is104 # not valid. Should raise a runtime error.105 response = httpx.Response(200)106 with pytest.raises(RuntimeError):107 response.raise_for_status()108109

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 110why, one step back
110def test_response_repr():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 111–114why, one step back
111 response = httpx.Response(
112 200,113 content=b"Hello, world!",114 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 115–123why, one step back
115 assert repr(response) == "<Response [200 OK]>"
116117118def test_response_content_type_encoding():119 """120 Use the charset encoding in the Content-Type header if possible.121 """122 headers = {"Content-Type": "text-plain; charset=latin-1"}123 content = "Latin 1: ÿ".encode("latin-1")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 124–128why, one step back
124 response = httpx.Response(
125 200,126 content=content,127 headers=headers,128 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 129–134why, one step back
129 assert response.text == "Latin 1: ÿ"
130 assert response.encoding == "latin-1"131132133def test_response_autodetect_encoding():134 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 135why, one step back
135 Autodetect encoding if there is no Content-Type header.

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 136why, one step back
136 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 137why, one step back
137 content = "おはようございます。".encode("utf-8")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 138–141why, one step back
138 response = httpx.Response(
139 200,140 content=content,141 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 142why, one step back
142 assert response.text == "おはようございます。"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 143why, one step back
143 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 144–150why, one step back
144
145146def test_response_fallback_to_autodetect():147 """148 Fallback to autodetection if we get an invalid charset in the Content-Type header.149 """150 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 151why, one step back
151 content = "おはようございます。".encode("utf-8")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 152–156why, one step back
152 response = httpx.Response(
153 200,154 content=content,155 headers=headers,156 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 157why, one step back
157 assert response.text == "おはようございます。"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 158why, one step back
158 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 159–160why, one step back
159
160

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 161why, one step back
161def test_response_no_charset_with_ascii_content():

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 162why, one step back
162 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 163–164why, one step back
163 A response with ascii encoded content should decode correctly,
164 even with no charset specified.

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 165–167why, one step back
165 """
166 content = b"Hello, world!"167 headers = {"Content-Type": "text/plain"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 168–172why, one step back
168 response = httpx.Response(
169 200,170 content=content,171 headers=headers,172 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 173why, one step back
173 assert response.status_code == 200

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 174why, one step back
174 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 175–177why, one step back
175 assert response.text == "Hello, world!"
176177

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 179why, one step back
179 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 180–181why, one step back
180 A response with UTF-8 encoded content should decode correctly,
181 even with no charset specified.

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 182why, one step back
182 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 183–184why, one step back
183 content = "Unicode Snowman: ☃".encode("utf-8")
184 headers = {"Content-Type": "text/plain"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 185–186why, one step back
185 response = httpx.Response(
186 200,

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 187–188why, one step back
187 content=content,
188 headers=headers,

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 189why, one step back
189 )

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 190–191why, one step back
190 assert response.text == "Unicode Snowman: ☃"
191 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 192–193why, one step back
192
193

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 194–225why, one step back
194def test_response_no_charset_with_iso_8859_1_content():
195 """196 A response with ISO 8859-1 encoded content should decode correctly,197 even with no charset specified.198 """199 content = "Accented: Österreich".encode("iso-8859-1")200 headers = {"Content-Type": "text/plain"}201 response = httpx.Response(202 200,203 content=content,204 headers=headers,205 )206 assert response.text == "Accented: Österreich"207 assert response.encoding is None208209210def test_response_no_charset_with_cp_1252_content():211 """212 A response with Windows 1252 encoded content should decode correctly,213 even with no charset specified.214 """215 content = "Euro Currency: €".encode("cp1252")216 headers = {"Content-Type": "text/plain"}217 response = httpx.Response(218 200,219 content=content,220 headers=headers,221 )222 assert response.text == "Euro Currency: €"223 assert response.encoding is None224225

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 226–230why, one step back
226def test_response_non_text_encoding():
227 """228 Default to apparent encoding for non-text content-type headers.229 """230 headers = {"Content-Type": "image/png"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 231–235why, one step back
231 response = httpx.Response(
232 200,233 content=b"xyz",234 headers=headers,235 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 236why, one step back
236 assert response.text == "xyz"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 237why, one step back
237 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 238–244why, one step back
238
239240def test_response_set_explicit_encoding():241 headers = {242 "Content-Type": "text-plain; charset=utf-8"243 } # Deliberately incorrect charset244 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 245–247why, one step back
245 200,
246 content="Latin 1: ÿ".encode("latin-1"),247 headers=headers,

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 248–255why, one step back
248 )
249 response.encoding = "latin-1"250 assert response.text == "Latin 1: ÿ"251 assert response.encoding == "latin-1"252253254def test_response_force_encoding():255 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 256–257why, one step back
256 200,
257 content="Snowman: ☃".encode("utf-8"),

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 258–266why, one step back
258 )
259 response.encoding = "iso-8859-1"260 assert response.status_code == 200261 assert response.reason_phrase == "OK"262 assert response.text == "Snowman: â\x98\x83"263 assert response.encoding == "iso-8859-1"264265266def test_read():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 267–270why, one step back
267 response = httpx.Response(
268 200,269 content=b"Hello, world!",270 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 271–273why, one step back
271
272 assert response.status_code == 200273 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 274why, one step back
274 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 275–283why, one step back
275 assert response.is_closed
276277 content = response.read()278279 assert content == b"Hello, world!"280 assert response.content == b"Hello, world!"281 assert response.is_closed282283

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 284–298why, one step back
284def test_empty_read():
285 response = httpx.Response(200)286287 assert response.status_code == 200288 assert response.text == ""289 assert response.encoding is None290 assert response.is_closed291292 content = response.read()293294 assert content == b""295 assert response.content == b""296 assert response.is_closed297298

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 299–300why, one step back
299@pytest.mark.asyncio
300async def test_aread():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 301–304why, one step back
301 response = httpx.Response(
302 200,303 content=b"Hello, world!",304 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 305–307why, one step back
305
306 assert response.status_code == 200307 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 308why, one step back
308 assert response.encoding is None

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 309–317why, one step back
309 assert response.is_closed
310311 content = await response.aread()312313 assert content == b"Hello, world!"314 assert response.content == b"Hello, world!"315 assert response.is_closed316317

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 318–333why, one step back
318@pytest.mark.asyncio
319async def test_empty_aread():320 response = httpx.Response(200)321322 assert response.status_code == 200323 assert response.text == ""324 assert response.encoding is None325 assert response.is_closed326327 content = await response.aread()328329 assert content == b""330 assert response.content == b""331 assert response.is_closed332333

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 334why, one step back
334def test_iter_raw():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 335–336why, one step back
335 response = httpx.Response(
336 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 337why, one step back
337 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 338why, one step back
338 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 339–345why, one step back
339
340 raw = b""341 for part in response.iter_raw():342 raw += part343 assert raw == b"Hello, world!"344345

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 346–367why, one step back
346def test_iter_raw_on_iterable():
347 response = httpx.Response(348 200,349 content=StreamingBody(),350 )351352 raw = b""353 for part in response.iter_raw():354 raw += part355 assert raw == b"Hello, world!"356357358def test_iter_raw_on_async():359 response = httpx.Response(360 200,361 content=async_streaming_body(),362 )363364 with pytest.raises(RuntimeError):365 [part for part in response.iter_raw()]366367

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

line 368why, one step back
368def test_iter_raw_increments_updates_counter():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 369why, one step back
369 response = httpx.Response(200, content=streaming_body())

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 370–376why, one step back
370
371 num_downloaded = response.num_bytes_downloaded372 for part in response.iter_raw():373 assert len(part) == (response.num_bytes_downloaded - num_downloaded)374 num_downloaded = response.num_bytes_downloaded375376

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 377–378why, one step back
377@pytest.mark.asyncio
378async def test_aiter_raw():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 379why, one step back
379 response = httpx.Response(200, content=async_streaming_body())

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 380–384why, one step back
380
381 raw = b""382 async for part in response.aiter_raw():383 raw += part384 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 385–387why, one step back
385
386387@pytest.mark.asyncio

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 388–398why, one step back
388async def test_aiter_raw_on_sync():
389 response = httpx.Response(390 200,391 content=streaming_body(),392 )393394 with pytest.raises(RuntimeError):395 [part async for part in response.aiter_raw()]396397398@pytest.mark.asyncio

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

line 399why, one step back
399async def test_aiter_raw_increments_updates_counter():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 400why, one step back
400 response = httpx.Response(200, content=async_streaming_body())

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 401–405why, one step back
401
402 num_downloaded = response.num_bytes_downloaded403 async for part in response.aiter_raw():404 assert len(part) == (response.num_bytes_downloaded - num_downloaded)405 num_downloaded = response.num_bytes_downloaded

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 406–408why, one step back
406
407408def test_iter_bytes():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 409–412why, one step back
409 response = httpx.Response(
410 200,411 content=b"Hello, world!",412 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 413–421why, one step back
413
414 content = b""415 for part in response.iter_bytes():416 content += part417 assert content == b"Hello, world!"418419420@pytest.mark.asyncio421async def test_aiter_bytes():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 422–425why, one step back
422 response = httpx.Response(
423 200,424 content=b"Hello, world!",425 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 426–433why, one step back
426
427 content = b""428 async for part in response.aiter_bytes():429 content += part430 assert content == b"Hello, world!"431432433def test_iter_text():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 434–437why, one step back
434 response = httpx.Response(
435 200,436 content=b"Hello, world!",437 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 438–446why, one step back
438
439 content = ""440 for part in response.iter_text():441 content += part442 assert content == "Hello, world!"443444445@pytest.mark.asyncio446async def test_aiter_text():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 447–450why, one step back
447 response = httpx.Response(
448 200,449 content=b"Hello, world!",450 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 451–458why, one step back
451
452 content = ""453 async for part in response.aiter_text():454 content += part455 assert content == "Hello, world!"456457458def test_iter_lines():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 459–462why, one step back
459 response = httpx.Response(
460 200,461 content=b"Hello,\nworld!",462 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 463–471why, one step back
463
464 content = []465 for line in response.iter_lines():466 content.append(line)467 assert content == ["Hello,\n", "world!"]468469470@pytest.mark.asyncio471async def test_aiter_lines():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 472–475why, one step back
472 response = httpx.Response(
473 200,474 content=b"Hello,\nworld!",475 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 476–483why, one step back
476
477 content = []478 async for line in response.aiter_lines():479 content.append(line)480 assert content == ["Hello,\n", "world!"]481482483def test_sync_streaming_response():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 484–485why, one step back
484 response = httpx.Response(
485 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 486why, one step back
486 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 487why, one step back
487 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 488–500why, one step back
488
489 assert response.status_code == 200490 assert not response.is_closed491492 content = response.read()493494 assert content == b"Hello, world!"495 assert response.content == b"Hello, world!"496 assert response.is_closed497498499@pytest.mark.asyncio500async def test_async_streaming_response():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 501–502why, one step back
501 response = httpx.Response(
502 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 503why, one step back
503 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 504why, one step back
504 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 505–516why, one step back
505
506 assert response.status_code == 200507 assert not response.is_closed508509 content = await response.aread()510511 assert content == b"Hello, world!"512 assert response.content == b"Hello, world!"513 assert response.is_closed514515516def test_cannot_read_after_stream_consumed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 517–518why, one step back
517 response = httpx.Response(
518 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 519why, one step back
519 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 520why, one step back
520 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 521–531why, one step back
521
522 content = b""523 for part in response.iter_bytes():524 content += part525526 with pytest.raises(httpx.StreamConsumed):527 response.read()528529530@pytest.mark.asyncio531async def test_cannot_aread_after_stream_consumed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 532–533why, one step back
532 response = httpx.Response(
533 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 534why, one step back
534 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 535why, one step back
535 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 536–545why, one step back
536
537 content = b""538 async for part in response.aiter_bytes():539 content += part540541 with pytest.raises(httpx.StreamConsumed):542 await response.aread()543544545def test_cannot_read_after_response_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 546–547why, one step back
546 response = httpx.Response(
547 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 548why, one step back
548 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 549why, one step back
549 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 550–557why, one step back
550
551 response.close()552 with pytest.raises(httpx.ResponseClosed):553 response.read()554555556@pytest.mark.asyncio557async def test_cannot_aread_after_response_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 558–559why, one step back
558 response = httpx.Response(
559 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 560why, one step back
560 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 561why, one step back
561 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 562–569why, one step back
562
563 await response.aclose()564 with pytest.raises(httpx.ResponseClosed):565 await response.aread()566567568@pytest.mark.asyncio569async def test_elapsed_not_available_until_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 570–571why, one step back
570 response = httpx.Response(
571 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 572why, one step back
572 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 573why, one step back
573 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 574–579why, one step back
574
575 with pytest.raises(RuntimeError):576 response.elapsed577578579def test_unknown_status_code():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 580–582why, one step back
580 response = httpx.Response(
581 600,582 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 583–591why, one step back
583 assert response.status_code == 600
584 assert response.reason_phrase == ""585 assert response.text == ""586587588def test_json_with_specified_encoding():589 data = {"greeting": "hello", "recipient": "world"}590 content = json.dumps(data).encode("utf-16")591 headers = {"Content-Type": "application/json, charset=utf-16"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 592–596why, one step back
592 response = httpx.Response(
593 200,594 content=content,595 headers=headers,596 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 597–603why, one step back
597 assert response.json() == data
598599600def test_json_with_options():601 data = {"greeting": "hello", "recipient": "world", "amount": 1}602 content = json.dumps(data).encode("utf-16")603 headers = {"Content-Type": "application/json, charset=utf-16"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 604–608why, one step back
604 response = httpx.Response(
605 200,606 content=content,607 headers=headers,608 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

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

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 616–620why, one step back
616 response = httpx.Response(
617 200,618 content=content,619 headers=headers,620 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 621–630why, one step back
621 assert response.json() == data
622623624def test_json_without_specified_encoding_decode_error():625 data = {"greeting": "hello", "recipient": "world"}626 content = json.dumps(data).encode("utf-32-be")627 headers = {"Content-Type": "application/json"}628 # force incorrect guess from `guess_json_utf` to trigger error629 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):630 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 631–633why, one step back
631 200,
632 content=content,633 headers=headers,

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

line 634why, one step back
634 )

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 635why, one step back
635 with pytest.raises(json.decoder.JSONDecodeError):

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 636–638why, one step back
636 response.json()
637638

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 639–649why, one step back
639def test_json_without_specified_encoding_value_error():
640 data = {"greeting": "hello", "recipient": "world"}641 content = json.dumps(data).encode("utf-32-be")642 headers = {"Content-Type": "application/json"}643 # force incorrect guess from `guess_json_utf` to trigger error644 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):645 response = httpx.Response(200, content=content, headers=headers)646 with pytest.raises(ValueError):647 response.json()648649

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

lines 650–666why, one step back
650@pytest.mark.parametrize(
651 "headers, expected",652 [653 (654 {"Link": "<https://example.com>; rel='preload'"},655 {"preload": {"rel": "preload", "url": "https://example.com"}},656 ),657 (658 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},659 {660 "hub": {"url": "/hub", "rel": "hub"},661 "self": {"url": "/resource", "rel": "self"},662 },663 ),664 ],665)666def test_link_headers(headers, expected):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 667–671why, one step back
667 response = httpx.Response(
668 200,669 content=None,670 headers=headers,671 )

Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c

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

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 673–679why, one step back
673
674675@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))676def test_decode_error_with_request(header_value):677 headers = [(b"Content-Encoding", header_value)]678 body = b"test 123"679 compressed_body = brotli.compress(body)[3:]

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 680–686why, one step back
680 with pytest.raises(ValueError):
681 httpx.Response(682 200,683 headers=headers,684 content=compressed_body,685 )686

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 687why, one step back
687 with pytest.raises(httpx.DecodingError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 688–693why, one step back
688 httpx.Response(
689 200,690 headers=headers,691 content=compressed_body,692 request=httpx.Request("GET", "https://www.example.org/"),693 )

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 694–716why, one step back
694
695696@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))697def test_value_error_without_request(header_value):698 headers = [(b"Content-Encoding", header_value)]699 body = b"test 123"700 compressed_body = brotli.compress(body)[3:]701 with pytest.raises(ValueError):702 httpx.Response(200, headers=headers, content=compressed_body)703704705def test_response_with_unset_request():706 response = httpx.Response(200, content=b"Hello, world!")707708 assert response.status_code == 200709 assert response.reason_phrase == "OK"710 assert response.text == "Hello, world!"711 assert not response.is_error712713714def test_set_request_after_init():715 response = httpx.Response(200, content=b"Hello, world!")716

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 717why, one step back
717 response.request = httpx.Request("GET", "https://www.example.org")

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 718why, one step back
718

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 719–720why, one step back
719 assert response.request.method == "GET"
720 assert response.request.url == "https://www.example.org"

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 721–726why, one step back
721
722723def test_cannot_access_unset_request():724 response = httpx.Response(200, content=b"Hello, world!")725726 with pytest.raises(RuntimeError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 727why, one step back
727 response.request

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 728–734why, one step back
728
729730def test_generator_with_transfer_encoding_header():731 def content():732 yield b"test 123" # pragma: nocover733734 response = httpx.Response(200, content=content())

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 735why, one step back
735 assert response.headers == {"Transfer-Encoding": "chunked"}

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 736–743why, one step back
736
737738def test_generator_with_content_length_header():739 def content():740 yield b"test 123" # pragma: nocover741742 headers = {"Content-Length": "8"}743 response = httpx.Response(200, content=content(), headers=headers)

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 744why, one step back
744 assert response.headers == {"Content-Length": "8"}