Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

741 lines · 18.7 KiB · Python

Last changed by

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

History of this file

Take it

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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"

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 41–82why, one step back
41def test_response_text():
42 response = httpx.Response(200, text="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 "Content-Type": "text/plain; charset=utf-8",51 }52 )535455def test_response_html():56 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")5758 assert response.status_code == 20059 assert response.reason_phrase == "OK"60 assert response.text == "<html><body>Hello, world!</html></body>"61 assert response.headers == httpx.Headers(62 {63 "Content-Length": "39",64 "Content-Type": "text/html; charset=utf-8",65 }66 )676869def test_response_json():70 response = httpx.Response(200, json={"hello": "world"})7172 assert response.status_code == 20073 assert response.reason_phrase == "OK"74 assert response.json() == {"hello": "world"}75 assert response.headers == httpx.Headers(76 {77 "Content-Length": "18",78 "Content-Type": "application/json",79 }80 )8182

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

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

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

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

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

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

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

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

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

lines 88–90why, one step back
88 response.raise_for_status()
8990 # 4xx status codes are a client error.

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

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

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

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

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

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

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

lines 97–106why, one step back
97 with pytest.raises(httpx.HTTPStatusError):
98 response.raise_for_status()99100 # Calling .raise_for_status without setting a request instance is101 # not valid. Should raise a runtime error.102 response = httpx.Response(200)103 with pytest.raises(RuntimeError):104 response.raise_for_status()105106

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 108–111why, one step back
108 response = httpx.Response(
109 200,110 content=b"Hello, world!",111 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 121–125why, one step back
121 response = httpx.Response(
122 200,123 content=content,124 headers=headers,125 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 126–131why, one step back
126 assert response.text == "Latin 1: ÿ"
127 assert response.encoding == "latin-1"128129130def test_response_autodetect_encoding():131 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

line 133why, one step back
133 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 156–157why, one step back
156
157

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

line 159why, one step back
159 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 165–169why, one step back
165 response = httpx.Response(
166 200,167 content=content,168 headers=headers,169 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 172–174why, one step back
172 assert response.text == "Hello, world!"
173174

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

line 176why, one step back
176 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

line 179why, one step back
179 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 182–183why, one step back
182 response = httpx.Response(
183 200,

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 184–185why, one step back
184 content=content,
185 headers=headers,

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

line 186why, one step back
186 )

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 189–190why, one step back
189
190

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 228–232why, one step back
228 response = httpx.Response(
229 200,230 content=b"xyz",231 headers=headers,232 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 235–241why, one step back
235
236237def test_response_set_explicit_encoding():238 headers = {239 "Content-Type": "text-plain; charset=utf-8"240 } # Deliberately incorrect charset241 response = httpx.Response(

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

lines 242–244why, one step back
242 200,
243 content="Latin 1: ÿ".encode("latin-1"),244 headers=headers,

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 245–252why, one step back
245 )
246 response.encoding = "latin-1"247 assert response.text == "Latin 1: ÿ"248 assert response.encoding == "latin-1"249250251def test_response_force_encoding():252 response = httpx.Response(

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 255–263why, one step back
255 )
256 response.encoding = "iso-8859-1"257 assert response.status_code == 200258 assert response.reason_phrase == "OK"259 assert response.text == "Snowman: â\x98\x83"260 assert response.encoding == "iso-8859-1"261262263def test_read():

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

lines 264–267why, one step back
264 response = httpx.Response(
265 200,266 content=b"Hello, world!",267 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 268–270why, one step back
268
269 assert response.status_code == 200270 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 272–280why, one step back
272 assert response.is_closed
273274 content = response.read()275276 assert content == b"Hello, world!"277 assert response.content == b"Hello, world!"278 assert response.is_closed279280

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 281–295why, one step back
281def test_empty_read():
282 response = httpx.Response(200)283284 assert response.status_code == 200285 assert response.text == ""286 assert response.encoding is None287 assert response.is_closed288289 content = response.read()290291 assert content == b""292 assert response.content == b""293 assert response.is_closed294295

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 298–301why, one step back
298 response = httpx.Response(
299 200,300 content=b"Hello, world!",301 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 302–304why, one step back
302
303 assert response.status_code == 200304 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 306–314why, one step back
306 assert response.is_closed
307308 content = await response.aread()309310 assert content == b"Hello, world!"311 assert response.content == b"Hello, world!"312 assert response.is_closed313314

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 315–330why, one step back
315@pytest.mark.asyncio
316async def test_empty_aread():317 response = httpx.Response(200)318319 assert response.status_code == 200320 assert response.text == ""321 assert response.encoding is None322 assert response.is_closed323324 content = await response.aread()325326 assert content == b""327 assert response.content == b""328 assert response.is_closed329330

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 332–333why, one step back
332 response = httpx.Response(
333 200,

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

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

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

line 335why, one step back
335 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 336–342why, one step back
336
337 raw = b""338 for part in response.iter_raw():339 raw += part340 assert raw == b"Hello, world!"341342

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 343–364why, one step back
343def test_iter_raw_on_iterable():
344 response = httpx.Response(345 200,346 content=StreamingBody(),347 )348349 raw = b""350 for part in response.iter_raw():351 raw += part352 assert raw == b"Hello, world!"353354355def test_iter_raw_on_async():356 response = httpx.Response(357 200,358 content=async_streaming_body(),359 )360361 with pytest.raises(RuntimeError):362 [part for part in response.iter_raw()]363364

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 367–373why, one step back
367
368 num_downloaded = response.num_bytes_downloaded369 for part in response.iter_raw():370 assert len(part) == (response.num_bytes_downloaded - num_downloaded)371 num_downloaded = response.num_bytes_downloaded372373

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 377–381why, one step back
377
378 raw = b""379 async for part in response.aiter_raw():380 raw += part381 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 382–384why, one step back
382
383384@pytest.mark.asyncio

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 385–395why, one step back
385async def test_aiter_raw_on_sync():
386 response = httpx.Response(387 200,388 content=streaming_body(),389 )390391 with pytest.raises(RuntimeError):392 [part async for part in response.aiter_raw()]393394395@pytest.mark.asyncio

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 403–405why, one step back
403
404405def test_iter_bytes():

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

lines 406–409why, one step back
406 response = httpx.Response(
407 200,408 content=b"Hello, world!",409 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 410–418why, one step back
410
411 content = b""412 for part in response.iter_bytes():413 content += part414 assert content == b"Hello, world!"415416417@pytest.mark.asyncio418async def test_aiter_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 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 423–430why, one step back
423
424 content = b""425 async for part in response.aiter_bytes():426 content += part427 assert content == b"Hello, world!"428429430def test_iter_text():

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 435–443why, one step back
435
436 content = ""437 for part in response.iter_text():438 content += part439 assert content == "Hello, world!"440441442@pytest.mark.asyncio443async def test_aiter_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 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 448–455why, one step back
448
449 content = ""450 async for part in response.aiter_text():451 content += part452 assert content == "Hello, world!"453454455def test_iter_lines():

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 460–468why, one step back
460
461 content = []462 for line in response.iter_lines():463 content.append(line)464 assert content == ["Hello,\n", "world!"]465466467@pytest.mark.asyncio468async def test_aiter_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 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 481–482why, one step back
481 response = httpx.Response(
482 200,

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

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

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

line 484why, one step back
484 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 485–497why, one step back
485
486 assert response.status_code == 200487 assert not response.is_closed488489 content = response.read()490491 assert content == b"Hello, world!"492 assert response.content == b"Hello, world!"493 assert response.is_closed494495496@pytest.mark.asyncio497async def test_async_streaming_response():

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

lines 498–499why, one step back
498 response = httpx.Response(
499 200,

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

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

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

line 501why, one step back
501 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 502–513why, one step back
502
503 assert response.status_code == 200504 assert not response.is_closed505506 content = await response.aread()507508 assert content == b"Hello, world!"509 assert response.content == b"Hello, world!"510 assert response.is_closed511512513def test_cannot_read_after_stream_consumed():

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

lines 514–515why, one step back
514 response = httpx.Response(
515 200,

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

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

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

line 517why, one step back
517 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 518–528why, one step back
518
519 content = b""520 for part in response.iter_bytes():521 content += part522523 with pytest.raises(httpx.StreamConsumed):524 response.read()525526527@pytest.mark.asyncio528async def test_cannot_aread_after_stream_consumed():

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

lines 529–530why, one step back
529 response = httpx.Response(
530 200,

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

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

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

line 532why, one step back
532 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 533–542why, one step back
533
534 content = b""535 async for part in response.aiter_bytes():536 content += part537538 with pytest.raises(httpx.StreamConsumed):539 await response.aread()540541542def test_cannot_read_after_response_closed():

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

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

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

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

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

line 546why, one step back
546 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 547–554why, one step back
547
548 response.close()549 with pytest.raises(httpx.ResponseClosed):550 response.read()551552553@pytest.mark.asyncio554async def test_cannot_aread_after_response_closed():

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

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

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

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

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

line 558why, one step back
558 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 559–566why, one step back
559
560 await response.aclose()561 with pytest.raises(httpx.ResponseClosed):562 await response.aread()563564565@pytest.mark.asyncio566async def test_elapsed_not_available_until_closed():

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

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

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

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

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

line 570why, one step back
570 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 571–576why, one step back
571
572 with pytest.raises(RuntimeError):573 response.elapsed574575576def test_unknown_status_code():

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

lines 577–579why, one step back
577 response = httpx.Response(
578 600,579 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 580–588why, one step back
580 assert response.status_code == 600
581 assert response.reason_phrase == ""582 assert response.text == ""583584585def test_json_with_specified_encoding():586 data = {"greeting": "hello", "recipient": "world"}587 content = json.dumps(data).encode("utf-16")588 headers = {"Content-Type": "application/json, charset=utf-16"}

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

lines 589–593why, one step back
589 response = httpx.Response(
590 200,591 content=content,592 headers=headers,593 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 594–600why, one step back
594 assert response.json() == data
595596597def test_json_with_options():598 data = {"greeting": "hello", "recipient": "world", "amount": 1}599 content = json.dumps(data).encode("utf-16")600 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 628–630why, one step back
628 200,
629 content=content,630 headers=headers,

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

line 631why, one step back
631 )

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

lines 633–635why, one step back
633 response.json()
634635

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

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

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

lines 664–668why, one step back
664 response = httpx.Response(
665 200,666 content=None,667 headers=headers,668 )

ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd

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

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

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

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

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

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

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

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

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

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

lines 691–713why, one step back
691
692693@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))694def test_value_error_without_request(header_value):695 headers = [(b"Content-Encoding", header_value)]696 body = b"test 123"697 compressed_body = brotli.compress(body)[3:]698 with pytest.raises(ValueError):699 httpx.Response(200, headers=headers, content=compressed_body)700701702def test_response_with_unset_request():703 response = httpx.Response(200, content=b"Hello, world!")704705 assert response.status_code == 200706 assert response.reason_phrase == "OK"707 assert response.text == "Hello, world!"708 assert not response.is_error709710711def test_set_request_after_init():712 response = httpx.Response(200, content=b"Hello, world!")713

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

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

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

line 715why, one step back
715

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

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

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

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

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

line 724why, one step back
724 response.request

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 725–741why, one step back
725
726727def test_generator_with_transfer_encoding_header():728 def content():729 yield b"test 123" # pragma: nocover730731 response = httpx.Response(200, content=content())732 assert response.headers == httpx.Headers({"Transfer-Encoding": "chunked"})733734735def test_generator_with_content_length_header():736 def content():737 yield b"test 123" # pragma: nocover738739 headers = {"Content-Length": "8"}740 response = httpx.Response(200, content=content(), headers=headers)741 assert response.headers == httpx.Headers({"Content-Length": "8"})