Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

612 lines · 15.7 KiB · Python

Last changed by

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

History of this file

Take it

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 5–19why, one step back
5import pytest
67import httpx8910def streaming_body():11 yield b"Hello, "12 yield b"world!"131415async def async_streaming_body():16 yield b"Hello, "17 yield b"world!"1819

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

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

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

lines 21–25why, one step back
21 response = httpx.Response(
22 200,23 content=b"Hello, world!",24 request=httpx.Request("GET", "https://example.org"),25 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 32–34why, one step back
32 assert not response.is_error
3334

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

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

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

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

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

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

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

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

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

lines 40–42why, one step back
40 response.raise_for_status()
4142 # 4xx status codes are a client error.

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

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

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

lines 44–47why, one step back
44 with pytest.raises(httpx.HTTPStatusError):
45 response.raise_for_status()4647 # 5xx status codes are a server error.

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

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

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

lines 49–58why, one step back
49 with pytest.raises(httpx.HTTPStatusError):
50 response.raise_for_status()5152 # Calling .raise_for_status without setting a request instance is53 # not valid. Should raise a runtime error.54 response = httpx.Response(200)55 with pytest.raises(RuntimeError):56 response.raise_for_status()5758

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

lines 60–63why, one step back
60 response = httpx.Response(
61 200,62 content=b"Hello, world!",63 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 64–72why, one step back
64 assert repr(response) == "<Response [200 OK]>"
656667def test_response_content_type_encoding():68 """69 Use the charset encoding in the Content-Type header if possible.70 """71 headers = {"Content-Type": "text-plain; charset=latin-1"}72 content = "Latin 1: ÿ".encode("latin-1")

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

lines 73–77why, one step back
73 response = httpx.Response(
74 200,75 content=content,76 headers=headers,77 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 78–83why, one step back
78 assert response.text == "Latin 1: ÿ"
79 assert response.encoding == "latin-1"808182def test_response_autodetect_encoding():83 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

line 85why, one step back
85 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 87–90why, one step back
87 response = httpx.Response(
88 200,89 content=content,90 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 93–99why, one step back
93
9495def test_response_fallback_to_autodetect():96 """97 Fallback to autodetection if we get an invalid charset in the Content-Type header.98 """99 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 101–105why, one step back
101 response = httpx.Response(
102 200,103 content=content,104 headers=headers,105 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 108–109why, one step back
108
109

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

line 111why, one step back
111 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

lines 117–121why, one step back
117 response = httpx.Response(
118 200,119 content=content,120 headers=headers,121 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 124–126why, one step back
124 assert response.text == "Hello, world!"
125126

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

line 128why, one step back
128 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

line 131why, one step back
131 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

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

lines 134–135why, one step back
134 response = httpx.Response(
135 200,

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 136–137why, one step back
136 content=content,
137 headers=headers,

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

line 138why, one step back
138 )

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 141–142why, one step back
141
142

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 143–174why, one step back
143def test_response_no_charset_with_iso_8859_1_content():
144 """145 A response with ISO 8859-1 encoded content should decode correctly,146 even with no charset specified.147 """148 content = "Accented: Österreich".encode("iso-8859-1")149 headers = {"Content-Type": "text/plain"}150 response = httpx.Response(151 200,152 content=content,153 headers=headers,154 )155 assert response.text == "Accented: Österreich"156 assert response.encoding is None157158159def test_response_no_charset_with_cp_1252_content():160 """161 A response with Windows 1252 encoded content should decode correctly,162 even with no charset specified.163 """164 content = "Euro Currency: €".encode("cp1252")165 headers = {"Content-Type": "text/plain"}166 response = httpx.Response(167 200,168 content=content,169 headers=headers,170 )171 assert response.text == "Euro Currency: €"172 assert response.encoding is None173174

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 175–179why, one step back
175def test_response_non_text_encoding():
176 """177 Default to apparent encoding for non-text content-type headers.178 """179 headers = {"Content-Type": "image/png"}

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

lines 180–184why, one step back
180 response = httpx.Response(
181 200,182 content=b"xyz",183 headers=headers,184 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 187–193why, one step back
187
188189def test_response_set_explicit_encoding():190 headers = {191 "Content-Type": "text-plain; charset=utf-8"192 } # Deliberately incorrect charset193 response = httpx.Response(

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

lines 194–196why, one step back
194 200,
195 content="Latin 1: ÿ".encode("latin-1"),196 headers=headers,

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 197–204why, one step back
197 )
198 response.encoding = "latin-1"199 assert response.text == "Latin 1: ÿ"200 assert response.encoding == "latin-1"201202203def test_response_force_encoding():204 response = httpx.Response(

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

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 207–215why, one step back
207 )
208 response.encoding = "iso-8859-1"209 assert response.status_code == 200210 assert response.reason_phrase == "OK"211 assert response.text == "Snowman: â\x98\x83"212 assert response.encoding == "iso-8859-1"213214215def test_read():

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

lines 216–219why, one step back
216 response = httpx.Response(
217 200,218 content=b"Hello, world!",219 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 220–222why, one step back
220
221 assert response.status_code == 200222 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 224–234why, one step back
224 assert response.is_closed
225226 content = response.read()227228 assert content == b"Hello, world!"229 assert response.content == b"Hello, world!"230 assert response.is_closed231232233@pytest.mark.asyncio234async def test_aread():

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

lines 235–238why, one step back
235 response = httpx.Response(
236 200,237 content=b"Hello, world!",238 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 239–241why, one step back
239
240 assert response.status_code == 200241 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 243–252why, one step back
243 assert response.is_closed
244245 content = await response.aread()246247 assert content == b"Hello, world!"248 assert response.content == b"Hello, world!"249 assert response.is_closed250251252def test_iter_raw():

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

lines 253–254why, one step back
253 response = httpx.Response(
254 200,

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

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

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

line 256why, one step back
256 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 257–263why, one step back
257
258 raw = b""259 for part in response.iter_raw():260 raw += part261 assert raw == b"Hello, world!"262263

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 266–272why, one step back
266
267 num_downloaded = response.num_bytes_downloaded268 for part in response.iter_raw():269 assert len(part) == (response.num_bytes_downloaded - num_downloaded)270 num_downloaded = response.num_bytes_downloaded271272

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 276–280why, one step back
276
277 raw = b""278 async for part in response.aiter_raw():279 raw += part280 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 281–284why, one step back
281
282283@pytest.mark.asyncio284async def test_aiter_raw_increments_updates_counter():

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 286–290why, one step back
286
287 num_downloaded = response.num_bytes_downloaded288 async for part in response.aiter_raw():289 assert len(part) == (response.num_bytes_downloaded - num_downloaded)290 num_downloaded = response.num_bytes_downloaded

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 291–293why, one step back
291
292293def test_iter_bytes():

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

lines 294–297why, one step back
294 response = httpx.Response(
295 200,296 content=b"Hello, world!",297 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 298–306why, one step back
298
299 content = b""300 for part in response.iter_bytes():301 content += part302 assert content == b"Hello, world!"303304305@pytest.mark.asyncio306async def test_aiter_bytes():

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

lines 307–310why, one step back
307 response = httpx.Response(
308 200,309 content=b"Hello, world!",310 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 311–318why, one step back
311
312 content = b""313 async for part in response.aiter_bytes():314 content += part315 assert content == b"Hello, world!"316317318def test_iter_text():

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

lines 319–322why, one step back
319 response = httpx.Response(
320 200,321 content=b"Hello, world!",322 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 323–331why, one step back
323
324 content = ""325 for part in response.iter_text():326 content += part327 assert content == "Hello, world!"328329330@pytest.mark.asyncio331async def test_aiter_text():

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

lines 332–335why, one step back
332 response = httpx.Response(
333 200,334 content=b"Hello, world!",335 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 336–343why, one step back
336
337 content = ""338 async for part in response.aiter_text():339 content += part340 assert content == "Hello, world!"341342343def test_iter_lines():

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

lines 344–347why, one step back
344 response = httpx.Response(
345 200,346 content=b"Hello,\nworld!",347 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 348–356why, one step back
348
349 content = []350 for line in response.iter_lines():351 content.append(line)352 assert content == ["Hello,\n", "world!"]353354355@pytest.mark.asyncio356async def test_aiter_lines():

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

lines 357–360why, one step back
357 response = httpx.Response(
358 200,359 content=b"Hello,\nworld!",360 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 361–368why, one step back
361
362 content = []363 async for line in response.aiter_lines():364 content.append(line)365 assert content == ["Hello,\n", "world!"]366367368def test_sync_streaming_response():

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

lines 369–370why, one step back
369 response = httpx.Response(
370 200,

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

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

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

line 372why, one step back
372 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 373–385why, one step back
373
374 assert response.status_code == 200375 assert not response.is_closed376377 content = response.read()378379 assert content == b"Hello, world!"380 assert response.content == b"Hello, world!"381 assert response.is_closed382383384@pytest.mark.asyncio385async def test_async_streaming_response():

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

lines 386–387why, one step back
386 response = httpx.Response(
387 200,

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

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

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

line 389why, one step back
389 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 390–401why, one step back
390
391 assert response.status_code == 200392 assert not response.is_closed393394 content = await response.aread()395396 assert content == b"Hello, world!"397 assert response.content == b"Hello, world!"398 assert response.is_closed399400401def test_cannot_read_after_stream_consumed():

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

lines 402–403why, one step back
402 response = httpx.Response(
403 200,

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

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

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

line 405why, one step back
405 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 406–416why, one step back
406
407 content = b""408 for part in response.iter_bytes():409 content += part410411 with pytest.raises(httpx.StreamConsumed):412 response.read()413414415@pytest.mark.asyncio416async def test_cannot_aread_after_stream_consumed():

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

lines 417–418why, one step back
417 response = httpx.Response(
418 200,

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

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

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

line 420why, one step back
420 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 421–430why, one step back
421
422 content = b""423 async for part in response.aiter_bytes():424 content += part425426 with pytest.raises(httpx.StreamConsumed):427 await response.aread()428429430def test_cannot_read_after_response_closed():

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

lines 431–432why, one step back
431 response = httpx.Response(
432 200,

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

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

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

line 434why, one step back
434 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 435–442why, one step back
435
436 response.close()437 with pytest.raises(httpx.ResponseClosed):438 response.read()439440441@pytest.mark.asyncio442async def test_cannot_aread_after_response_closed():

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

lines 443–444why, one step back
443 response = httpx.Response(
444 200,

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

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

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

line 446why, one step back
446 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 447–454why, one step back
447
448 await response.aclose()449 with pytest.raises(httpx.ResponseClosed):450 await response.aread()451452453@pytest.mark.asyncio454async def test_elapsed_not_available_until_closed():

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

lines 455–456why, one step back
455 response = httpx.Response(
456 200,

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

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

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

line 458why, one step back
458 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 459–464why, one step back
459
460 with pytest.raises(RuntimeError):461 response.elapsed462463464def test_unknown_status_code():

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

lines 465–467why, one step back
465 response = httpx.Response(
466 600,467 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 468–476why, one step back
468 assert response.status_code == 600
469 assert response.reason_phrase == ""470 assert response.text == ""471472473def test_json_with_specified_encoding():474 data = {"greeting": "hello", "recipient": "world"}475 content = json.dumps(data).encode("utf-16")476 headers = {"Content-Type": "application/json, charset=utf-16"}

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

lines 477–481why, one step back
477 response = httpx.Response(
478 200,479 content=content,480 headers=headers,481 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 482–488why, one step back
482 assert response.json() == data
483484485def test_json_with_options():486 data = {"greeting": "hello", "recipient": "world", "amount": 1}487 content = json.dumps(data).encode("utf-16")488 headers = {"Content-Type": "application/json, charset=utf-16"}

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

lines 489–493why, one step back
489 response = httpx.Response(
490 200,491 content=content,492 headers=headers,493 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

lines 501–505why, one step back
501 response = httpx.Response(
502 200,503 content=content,504 headers=headers,505 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 506–515why, one step back
506 assert response.json() == data
507508509def test_json_without_specified_encoding_decode_error():510 data = {"greeting": "hello", "recipient": "world"}511 content = json.dumps(data).encode("utf-32-be")512 headers = {"Content-Type": "application/json"}513 # force incorrect guess from `guess_json_utf` to trigger error514 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):515 response = httpx.Response(

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

lines 516–518why, one step back
516 200,
517 content=content,518 headers=headers,

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

line 519why, one step back
519 )

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

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

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 521–523why, one step back
521 response.json()
522523

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

lines 524–534why, one step back
524def test_json_without_specified_encoding_value_error():
525 data = {"greeting": "hello", "recipient": "world"}526 content = json.dumps(data).encode("utf-32-be")527 headers = {"Content-Type": "application/json"}528 # force incorrect guess from `guess_json_utf` to trigger error529 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):530 response = httpx.Response(200, content=content, headers=headers)531 with pytest.raises(ValueError):532 response.json()533534

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

lines 535–551why, one step back
535@pytest.mark.parametrize(
536 "headers, expected",537 [538 (539 {"Link": "<https://example.com>; rel='preload'"},540 {"preload": {"rel": "preload", "url": "https://example.com"}},541 ),542 (543 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},544 {545 "hub": {"url": "/hub", "rel": "hub"},546 "self": {"url": "/resource", "rel": "self"},547 },548 ),549 ],550)551def test_link_headers(headers, expected):

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

lines 552–556why, one step back
552 response = httpx.Response(
553 200,554 content=None,555 headers=headers,556 )

When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e

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

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

lines 558–564why, one step back
558
559560@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))561def test_decode_error_with_request(header_value):562 headers = [(b"Content-Encoding", header_value)]563 body = b"test 123"564 compressed_body = brotli.compress(body)[3:]

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

lines 565–571why, one step back
565 with pytest.raises(ValueError):
566 httpx.Response(567 200,568 headers=headers,569 content=compressed_body,570 )571

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

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

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

lines 573–578why, one step back
573 httpx.Response(
574 200,575 headers=headers,576 content=compressed_body,577 request=httpx.Request("GET", "https://www.example.org/"),578 )

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

lines 579–601why, one step back
579
580581@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))582def test_value_error_without_request(header_value):583 headers = [(b"Content-Encoding", header_value)]584 body = b"test 123"585 compressed_body = brotli.compress(body)[3:]586 with pytest.raises(ValueError):587 httpx.Response(200, headers=headers, content=compressed_body)588589590def test_response_with_unset_request():591 response = httpx.Response(200, content=b"Hello, world!")592593 assert response.status_code == 200594 assert response.reason_phrase == "OK"595 assert response.text == "Hello, world!"596 assert not response.is_error597598599def test_set_request_after_init():600 response = httpx.Response(200, content=b"Hello, world!")601

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

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

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

line 603why, one step back
603

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

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

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

lines 606–611why, one step back
606
607608def test_cannot_access_unset_request():609 response = httpx.Response(200, content=b"Hello, world!")610611 with pytest.raises(RuntimeError):

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

line 612why, one step back
612 response.request