Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

754 lines · 19.1 KiB · Python

Last changed by

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

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–53why, 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!"47 assert response.headers == httpx.Headers(48 {49 "Content-Length": "13",50 }51 )5253

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

lines 54–95why, one step back
54def test_response_text():
55 response = httpx.Response(200, text="Hello, world!")5657 assert response.status_code == 20058 assert response.reason_phrase == "OK"59 assert response.text == "Hello, world!"60 assert response.headers == httpx.Headers(61 {62 "Content-Length": "13",63 "Content-Type": "text/plain; charset=utf-8",64 }65 )666768def test_response_html():69 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")7071 assert response.status_code == 20072 assert response.reason_phrase == "OK"73 assert response.text == "<html><body>Hello, world!</html></body>"74 assert response.headers == httpx.Headers(75 {76 "Content-Length": "39",77 "Content-Type": "text/html; charset=utf-8",78 }79 )808182def test_response_json():83 response = httpx.Response(200, json={"hello": "world"})8485 assert response.status_code == 20086 assert response.reason_phrase == "OK"87 assert response.json() == {"hello": "world"}88 assert response.headers == httpx.Headers(89 {90 "Content-Length": "18",91 "Content-Type": "application/json",92 }93 )9495

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

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

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

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

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

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

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

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

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

lines 101–103why, one step back
101 response.raise_for_status()
102103 # 4xx status codes are a client error.

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

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

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

lines 105–108why, one step back
105 with pytest.raises(httpx.HTTPStatusError):
106 response.raise_for_status()107108 # 5xx status codes are a server error.

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

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

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

lines 110–119why, one step back
110 with pytest.raises(httpx.HTTPStatusError):
111 response.raise_for_status()112113 # Calling .raise_for_status without setting a request instance is114 # not valid. Should raise a runtime error.115 response = httpx.Response(200)116 with pytest.raises(RuntimeError):117 response.raise_for_status()118119

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

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

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

lines 121–124why, one step back
121 response = httpx.Response(
122 200,123 content=b"Hello, world!",124 )

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

lines 125–133why, one step back
125 assert repr(response) == "<Response [200 OK]>"
126127128def test_response_content_type_encoding():129 """130 Use the charset encoding in the Content-Type header if possible.131 """132 headers = {"Content-Type": "text-plain; charset=latin-1"}133 content = "Latin 1: ÿ".encode("latin-1")

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

lines 134–138why, one step back
134 response = httpx.Response(
135 200,136 content=content,137 headers=headers,138 )

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

lines 139–144why, one step back
139 assert response.text == "Latin 1: ÿ"
140 assert response.encoding == "latin-1"141142143def test_response_autodetect_encoding():144 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

line 146why, one step back
146 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 148–151why, one step back
148 response = httpx.Response(
149 200,150 content=content,151 )

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

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 154–160why, one step back
154
155156def test_response_fallback_to_autodetect():157 """158 Fallback to autodetection if we get an invalid charset in the Content-Type header.159 """160 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 162–166why, one step back
162 response = httpx.Response(
163 200,164 content=content,165 headers=headers,166 )

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

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 169–170why, one step back
169
170

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

line 172why, one step back
172 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

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

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

lines 178–182why, one step back
178 response = httpx.Response(
179 200,180 content=content,181 headers=headers,182 )

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

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 185–187why, one step back
185 assert response.text == "Hello, world!"
186187

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

line 189why, one step back
189 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

line 192why, one step back
192 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 195–196why, one step back
195 response = httpx.Response(
196 200,

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 197–198why, one step back
197 content=content,
198 headers=headers,

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

line 199why, one step back
199 )

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 202–203why, one step back
202
203

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 204–235why, one step back
204def test_response_no_charset_with_iso_8859_1_content():
205 """206 A response with ISO 8859-1 encoded content should decode correctly,207 even with no charset specified.208 """209 content = "Accented: Österreich".encode("iso-8859-1")210 headers = {"Content-Type": "text/plain"}211 response = httpx.Response(212 200,213 content=content,214 headers=headers,215 )216 assert response.text == "Accented: Österreich"217 assert response.encoding is None218219220def test_response_no_charset_with_cp_1252_content():221 """222 A response with Windows 1252 encoded content should decode correctly,223 even with no charset specified.224 """225 content = "Euro Currency: €".encode("cp1252")226 headers = {"Content-Type": "text/plain"}227 response = httpx.Response(228 200,229 content=content,230 headers=headers,231 )232 assert response.text == "Euro Currency: €"233 assert response.encoding is None234235

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

lines 236–240why, one step back
236def test_response_non_text_encoding():
237 """238 Default to apparent encoding for non-text content-type headers.239 """240 headers = {"Content-Type": "image/png"}

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

lines 241–245why, one step back
241 response = httpx.Response(
242 200,243 content=b"xyz",244 headers=headers,245 )

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

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 248–254why, one step back
248
249250def test_response_set_explicit_encoding():251 headers = {252 "Content-Type": "text-plain; charset=utf-8"253 } # Deliberately incorrect charset254 response = httpx.Response(

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

lines 255–257why, one step back
255 200,
256 content="Latin 1: ÿ".encode("latin-1"),257 headers=headers,

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

lines 258–265why, one step back
258 )
259 response.encoding = "latin-1"260 assert response.text == "Latin 1: ÿ"261 assert response.encoding == "latin-1"262263264def test_response_force_encoding():265 response = httpx.Response(

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

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

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

lines 268–276why, one step back
268 )
269 response.encoding = "iso-8859-1"270 assert response.status_code == 200271 assert response.reason_phrase == "OK"272 assert response.text == "Snowman: â\x98\x83"273 assert response.encoding == "iso-8859-1"274275276def test_read():

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

lines 277–280why, one step back
277 response = httpx.Response(
278 200,279 content=b"Hello, world!",280 )

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

lines 281–283why, one step back
281
282 assert response.status_code == 200283 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 285–293why, one step back
285 assert response.is_closed
286287 content = response.read()288289 assert content == b"Hello, world!"290 assert response.content == b"Hello, world!"291 assert response.is_closed292293

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 294–308why, one step back
294def test_empty_read():
295 response = httpx.Response(200)296297 assert response.status_code == 200298 assert response.text == ""299 assert response.encoding is None300 assert response.is_closed301302 content = response.read()303304 assert content == b""305 assert response.content == b""306 assert response.is_closed307308

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

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

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

lines 311–314why, one step back
311 response = httpx.Response(
312 200,313 content=b"Hello, world!",314 )

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

lines 315–317why, one step back
315
316 assert response.status_code == 200317 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 319–327why, one step back
319 assert response.is_closed
320321 content = await response.aread()322323 assert content == b"Hello, world!"324 assert response.content == b"Hello, world!"325 assert response.is_closed326327

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 328–343why, one step back
328@pytest.mark.asyncio
329async def test_empty_aread():330 response = httpx.Response(200)331332 assert response.status_code == 200333 assert response.text == ""334 assert response.encoding is None335 assert response.is_closed336337 content = await response.aread()338339 assert content == b""340 assert response.content == b""341 assert response.is_closed342343

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

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

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

lines 345–346why, one step back
345 response = httpx.Response(
346 200,

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

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

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

line 348why, one step back
348 )

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

lines 349–355why, one step back
349
350 raw = b""351 for part in response.iter_raw():352 raw += part353 assert raw == b"Hello, world!"354355

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 356–377why, one step back
356def test_iter_raw_on_iterable():
357 response = httpx.Response(358 200,359 content=StreamingBody(),360 )361362 raw = b""363 for part in response.iter_raw():364 raw += part365 assert raw == b"Hello, world!"366367368def test_iter_raw_on_async():369 response = httpx.Response(370 200,371 content=async_streaming_body(),372 )373374 with pytest.raises(RuntimeError):375 [part for part in response.iter_raw()]376377

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 380–386why, one step back
380
381 num_downloaded = response.num_bytes_downloaded382 for part in response.iter_raw():383 assert len(part) == (response.num_bytes_downloaded - num_downloaded)384 num_downloaded = response.num_bytes_downloaded385386

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

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

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

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

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

lines 390–394why, one step back
390
391 raw = b""392 async for part in response.aiter_raw():393 raw += part394 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 395–397why, one step back
395
396397@pytest.mark.asyncio

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 398–408why, one step back
398async def test_aiter_raw_on_sync():
399 response = httpx.Response(400 200,401 content=streaming_body(),402 )403404 with pytest.raises(RuntimeError):405 [part async for part in response.aiter_raw()]406407408@pytest.mark.asyncio

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 411–415why, one step back
411
412 num_downloaded = response.num_bytes_downloaded413 async for part in response.aiter_raw():414 assert len(part) == (response.num_bytes_downloaded - num_downloaded)415 num_downloaded = response.num_bytes_downloaded

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

lines 416–418why, one step back
416
417418def test_iter_bytes():

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

lines 419–422why, one step back
419 response = httpx.Response(
420 200,421 content=b"Hello, world!",422 )

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

lines 423–431why, one step back
423
424 content = b""425 for part in response.iter_bytes():426 content += part427 assert content == b"Hello, world!"428429430@pytest.mark.asyncio431async def test_aiter_bytes():

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

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

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

lines 436–443why, one step back
436
437 content = b""438 async for part in response.aiter_bytes():439 content += part440 assert content == b"Hello, world!"441442443def test_iter_text():

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

lines 444–447why, one step back
444 response = httpx.Response(
445 200,446 content=b"Hello, world!",447 )

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

lines 448–456why, one step back
448
449 content = ""450 for part in response.iter_text():451 content += part452 assert content == "Hello, world!"453454455@pytest.mark.asyncio456async def test_aiter_text():

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

lines 457–460why, one step back
457 response = httpx.Response(
458 200,459 content=b"Hello, world!",460 )

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

lines 461–468why, one step back
461
462 content = ""463 async for part in response.aiter_text():464 content += part465 assert content == "Hello, world!"466467468def test_iter_lines():

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

lines 469–472why, one step back
469 response = httpx.Response(
470 200,471 content=b"Hello,\nworld!",472 )

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

lines 473–481why, one step back
473
474 content = []475 for line in response.iter_lines():476 content.append(line)477 assert content == ["Hello,\n", "world!"]478479480@pytest.mark.asyncio481async def test_aiter_lines():

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

lines 482–485why, one step back
482 response = httpx.Response(
483 200,484 content=b"Hello,\nworld!",485 )

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

lines 486–493why, one step back
486
487 content = []488 async for line in response.aiter_lines():489 content.append(line)490 assert content == ["Hello,\n", "world!"]491492493def test_sync_streaming_response():

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

lines 494–495why, one step back
494 response = httpx.Response(
495 200,

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

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

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

line 497why, one step back
497 )

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

lines 498–510why, one step back
498
499 assert response.status_code == 200500 assert not response.is_closed501502 content = response.read()503504 assert content == b"Hello, world!"505 assert response.content == b"Hello, world!"506 assert response.is_closed507508509@pytest.mark.asyncio510async def test_async_streaming_response():

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

lines 511–512why, one step back
511 response = httpx.Response(
512 200,

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

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

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

line 514why, one step back
514 )

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

lines 515–526why, one step back
515
516 assert response.status_code == 200517 assert not response.is_closed518519 content = await response.aread()520521 assert content == b"Hello, world!"522 assert response.content == b"Hello, world!"523 assert response.is_closed524525526def test_cannot_read_after_stream_consumed():

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

lines 527–528why, one step back
527 response = httpx.Response(
528 200,

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

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

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

line 530why, one step back
530 )

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

lines 531–541why, one step back
531
532 content = b""533 for part in response.iter_bytes():534 content += part535536 with pytest.raises(httpx.StreamConsumed):537 response.read()538539540@pytest.mark.asyncio541async def test_cannot_aread_after_stream_consumed():

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

lines 542–543why, one step back
542 response = httpx.Response(
543 200,

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

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

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

line 545why, one step back
545 )

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

lines 546–555why, one step back
546
547 content = b""548 async for part in response.aiter_bytes():549 content += part550551 with pytest.raises(httpx.StreamConsumed):552 await response.aread()553554555def test_cannot_read_after_response_closed():

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

lines 556–557why, one step back
556 response = httpx.Response(
557 200,

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

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

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

line 559why, one step back
559 )

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

lines 560–567why, one step back
560
561 response.close()562 with pytest.raises(httpx.ResponseClosed):563 response.read()564565566@pytest.mark.asyncio567async def test_cannot_aread_after_response_closed():

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

lines 568–569why, one step back
568 response = httpx.Response(
569 200,

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

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

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

line 571why, one step back
571 )

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

lines 572–579why, one step back
572
573 await response.aclose()574 with pytest.raises(httpx.ResponseClosed):575 await response.aread()576577578@pytest.mark.asyncio579async def test_elapsed_not_available_until_closed():

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

lines 580–581why, one step back
580 response = httpx.Response(
581 200,

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

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

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

line 583why, one step back
583 )

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

lines 584–589why, one step back
584
585 with pytest.raises(RuntimeError):586 response.elapsed587588589def test_unknown_status_code():

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

lines 590–592why, one step back
590 response = httpx.Response(
591 600,592 )

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

lines 593–601why, one step back
593 assert response.status_code == 600
594 assert response.reason_phrase == ""595 assert response.text == ""596597598def test_json_with_specified_encoding():599 data = {"greeting": "hello", "recipient": "world"}600 content = json.dumps(data).encode("utf-16")601 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

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

lines 607–613why, one step back
607 assert response.json() == data
608609610def test_json_with_options():611 data = {"greeting": "hello", "recipient": "world", "amount": 1}612 content = json.dumps(data).encode("utf-16")613 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

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

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

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

lines 626–630why, one step back
626 response = httpx.Response(
627 200,628 content=content,629 headers=headers,630 )

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

lines 631–640why, one step back
631 assert response.json() == data
632633634def test_json_without_specified_encoding_decode_error():635 data = {"greeting": "hello", "recipient": "world"}636 content = json.dumps(data).encode("utf-32-be")637 headers = {"Content-Type": "application/json"}638 # force incorrect guess from `guess_json_utf` to trigger error639 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):640 response = httpx.Response(

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

lines 641–643why, one step back
641 200,
642 content=content,643 headers=headers,

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

line 644why, one step back
644 )

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

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

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

lines 646–648why, one step back
646 response.json()
647648

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

lines 649–659why, one step back
649def test_json_without_specified_encoding_value_error():
650 data = {"greeting": "hello", "recipient": "world"}651 content = json.dumps(data).encode("utf-32-be")652 headers = {"Content-Type": "application/json"}653 # force incorrect guess from `guess_json_utf` to trigger error654 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):655 response = httpx.Response(200, content=content, headers=headers)656 with pytest.raises(ValueError):657 response.json()658659

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

lines 660–676why, one step back
660@pytest.mark.parametrize(
661 "headers, expected",662 [663 (664 {"Link": "<https://example.com>; rel='preload'"},665 {"preload": {"rel": "preload", "url": "https://example.com"}},666 ),667 (668 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},669 {670 "hub": {"url": "/hub", "rel": "hub"},671 "self": {"url": "/resource", "rel": "self"},672 },673 ),674 ],675)676def test_link_headers(headers, expected):

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

lines 677–681why, one step back
677 response = httpx.Response(
678 200,679 content=None,680 headers=headers,681 )

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

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

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

lines 683–689why, one step back
683
684685@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))686def test_decode_error_with_request(header_value):687 headers = [(b"Content-Encoding", header_value)]688 body = b"test 123"689 compressed_body = brotli.compress(body)[3:]

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

lines 690–696why, one step back
690 with pytest.raises(ValueError):
691 httpx.Response(692 200,693 headers=headers,694 content=compressed_body,695 )696

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

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

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

lines 698–703why, one step back
698 httpx.Response(
699 200,700 headers=headers,701 content=compressed_body,702 request=httpx.Request("GET", "https://www.example.org/"),703 )

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

lines 704–726why, one step back
704
705706@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))707def test_value_error_without_request(header_value):708 headers = [(b"Content-Encoding", header_value)]709 body = b"test 123"710 compressed_body = brotli.compress(body)[3:]711 with pytest.raises(ValueError):712 httpx.Response(200, headers=headers, content=compressed_body)713714715def test_response_with_unset_request():716 response = httpx.Response(200, content=b"Hello, world!")717718 assert response.status_code == 200719 assert response.reason_phrase == "OK"720 assert response.text == "Hello, world!"721 assert not response.is_error722723724def test_set_request_after_init():725 response = httpx.Response(200, content=b"Hello, world!")726

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

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

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

line 728why, one step back
728

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

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

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

lines 731–736why, one step back
731
732733def test_cannot_access_unset_request():734 response = httpx.Response(200, content=b"Hello, world!")735736 with pytest.raises(RuntimeError):

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

line 737why, one step back
737 response.request

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 738–754why, one step back
738
739740def test_generator_with_transfer_encoding_header():741 def content():742 yield b"test 123" # pragma: nocover743744 response = httpx.Response(200, content=content())745 assert response.headers == httpx.Headers({"Transfer-Encoding": "chunked"})746747748def test_generator_with_content_length_header():749 def content():750 yield b"test 123" # pragma: nocover751752 headers = {"Content-Length": "8"}753 response = httpx.Response(200, content=content(), headers=headers)754 assert response.headers == httpx.Headers({"Content-Length": "8"})