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 fileTake 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 back1import json2from unittest import mock3Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 4why, one step back4import brotliWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 5–19why, one step back5import pytest67import httpx8910def streaming_body():11 yield b"Hello, "12 yield b"world!"131415async def async_streaming_body():16 yield b"Hello, "17 yield b"world!"1819Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 20why, one step back20def test_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 21–25why, one step back21 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 back2627 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 back30 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 back32 assert not response.is_error3334Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 35why, one step back35def test_raise_for_status():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 36–37why, one step back36 request = httpx.Request("GET", "https://example.org")37Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 38why, one step back38 # 2xx status codes are not an error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 39why, one step back39 response = httpx.Response(200, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 40–42why, one step back40 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 back43 response = httpx.Response(403, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 44–47why, one step back44 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 back48 response = httpx.Response(500, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 49–58why, one step back49 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()5758When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
line 59why, one step back59def test_response_repr():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 60–63why, one step back60 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 back64 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 back73 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 back78 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 back84 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 back85 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 86why, one step back86 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 87–90why, one step back87 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 back91 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 92why, one step back92 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 93–99why, one step back939495def 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 back100 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 101–105why, one step back101 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 back106 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 107why, one step back107 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 108–109why, one step back108109Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 110why, one step back110def 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 back111 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 112–113why, one step back112 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 back114 """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 back117 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 back122 assert response.status_code == 200Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 123why, one step back123 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 124–126why, one step back124 assert response.text == "Hello, world!"125126Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 127why, one step back127def 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 back128 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 129–130why, one step back129 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 back131 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 132–133why, one step back132 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 back134 response = httpx.Response(135 200,Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 136–137why, one step back136 content=content,137 headers=headers,Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 138why, one step back138 )Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 139–140why, one step back139 assert response.text == "Unicode Snowman: ☃"140 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 141–142why, one step back141142Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 143–174why, one step back143def 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 None173174When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 175–179why, one step back175def 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 back180 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 back185 assert response.text == "xyz"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 186why, one step back186 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 187–193why, one step back187188189def 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 back194 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 back197 )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 back205 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 back207 )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 back216 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 back220221 assert response.status_code == 200222 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 223why, one step back223 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 224–234why, one step back224 assert response.is_closed225226 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 back235 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 back239240 assert response.status_code == 200241 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 242why, one step back242 assert response.encoding is NoneWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 243–252why, one step back243 assert response.is_closed244245 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 back253 response = httpx.Response(254 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 255why, one step back255 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 256why, one step back256 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 257–263why, one step back257258 raw = b""259 for part in response.iter_raw():260 raw += part261 assert raw == b"Hello, world!"262263Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 264why, one step back264def test_iter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 265why, one step back265 response = httpx.Response(200, content=streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 266–272why, one step back266267 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_downloaded271272When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 273–274why, one step back273@pytest.mark.asyncio274async def test_aiter_raw():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 275why, one step back275 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 back276277 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 back281282283@pytest.mark.asyncio284async def test_aiter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 285why, one step back285 response = httpx.Response(200, content=async_streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 286–290why, one step back286287 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_downloadedWhen setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 291–293why, one step back291292293def test_iter_bytes():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 294–297why, one step back294 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 back298299 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 back307 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 back311312 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 back319 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 back323324 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 back332 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 back336337 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 back344 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 back348349 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 back357 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 back361362 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 back369 response = httpx.Response(370 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 371why, one step back371 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 372why, one step back372 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 373–385why, one step back373374 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 back386 response = httpx.Response(387 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 388why, one step back388 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 389why, one step back389 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 390–401why, one step back390391 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 back402 response = httpx.Response(403 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 404why, one step back404 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 405why, one step back405 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 406–416why, one step back406407 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 back417 response = httpx.Response(418 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 419why, one step back419 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 420why, one step back420 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 421–430why, one step back421422 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 back431 response = httpx.Response(432 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 433why, one step back433 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 434why, one step back434 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 435–442why, one step back435436 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 back443 response = httpx.Response(444 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 445why, one step back445 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 446why, one step back446 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 447–454why, one step back447448 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 back455 response = httpx.Response(456 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 457why, one step back457 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 458why, one step back458 )When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 459–464why, one step back459460 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 back465 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 back468 assert response.status_code == 600469 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 back477 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 back482 assert response.json() == data483484485def 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 back489 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 back494 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 back501 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 back506 assert response.json() == data507508509def 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 back516 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 back519 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 520why, one step back520 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 back521 response.json()522523Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 524–534why, one step back524def 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()533534When setting headers, fallback to `utf-8` if no encoding is specified yet. (#820)Piotr Staroszczykbefore adoption50d337e
lines 535–551why, one step back535@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 back552 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 back557 assert response.links == expectedMake the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 558–564why, one step back558559560@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 back565 with pytest.raises(ValueError):566 httpx.Response(567 200,568 headers=headers,569 content=compressed_body,570 )571Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 572why, one step back572 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 573–578why, one step back573 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 back579580581@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!")601Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 602why, one step back602 response.request = httpx.Request("GET", "https://www.example.org")Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 603why, one step back603Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 604–605why, one step back604 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 back606607608def 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 back612 response.request