test_responses.py
This file
Path
tests/models/test_responses.py
944 lines · 25.7 KiB · Python
Last changed by
is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
History of this fileTake it
The walk stopped after 200 commits: lines older than bd8165a point at it.
Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 1why, one step back1import jsonMake Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104
line 2why, one step back2import pickleCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 3–6why, one step back34import pytest56import httpxUse either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312
line 7why, one step back7from httpx._compat import brotliCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 8–9why, one step back89Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 10–15why, one step back10class StreamingBody:11 def __iter__(self):12 yield b"Hello, "13 yield b"world!"1415Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 16–25why, one step back16def streaming_body():17 yield b"Hello, "18 yield b"world!"192021async def async_streaming_body():22 yield b"Hello, "23 yield b"world!"2425Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 26why, one step back26def test_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 27–31why, one step back27 response = httpx.Response(28 200,29 content=b"Hello, world!",30 request=httpx.Request("GET", "https://example.org"),31 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 32–35why, one step back3233 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 back36 assert response.request.method == "GET"37 assert response.request.url == "https://example.org"Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 38–40why, one step back38 assert not response.is_error3940Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd
lines 41–46why, one step back41def test_response_content():42 response = httpx.Response(200, content="Hello, world!")4344 assert response.status_code == 20045 assert response.reason_phrase == "OK"46 assert response.text == "Hello, world!"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 47why, one step back47 assert response.headers == {"Content-Length": "13"}Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd
lines 48–49why, one step back4849Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 50–55why, one step back50def test_response_text():51 response = httpx.Response(200, text="Hello, world!")5253 assert response.status_code == 20054 assert response.reason_phrase == "OK"55 assert response.text == "Hello, world!"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 56–59why, one step back56 assert response.headers == {57 "Content-Length": "13",58 "Content-Type": "text/plain; charset=utf-8",59 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 60–67why, one step back606162def test_response_html():63 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")6465 assert response.status_code == 20066 assert response.reason_phrase == "OK"67 assert response.text == "<html><body>Hello, world!</html></body>"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 68–71why, one step back68 assert response.headers == {69 "Content-Length": "39",70 "Content-Type": "text/html; charset=utf-8",71 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 72–79why, one step back727374def test_response_json():75 response = httpx.Response(200, json={"hello": "world"})7677 assert response.status_code == 20078 assert response.reason_phrase == "OK"79 assert response.json() == {"hello": "world"}Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 80–83why, one step back80 assert response.headers == {81 "Content-Length": "18",82 "Content-Type": "application/json",83 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 84–85why, one step back8485Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 86why, one step back86def test_raise_for_status():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 87–88why, one step back87 request = httpx.Request("GET", "https://example.org")88Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 89why, one step back89 # 2xx status codes are not an error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 90why, one step back90 response = httpx.Response(200, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 91–92why, one step back91 response.raise_for_status()92is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
lines 93–114why, one step back93 # 1xx status codes are informational responses.94 response = httpx.Response(101, request=request)95 assert response.is_informational96 with pytest.raises(httpx.HTTPStatusError) as exc_info:97 response.raise_for_status()98 assert str(exc_info.value) == (99 "Informational response '101 Switching Protocols' for url 'https://example.org'\n"100 "For more information check: https://httpstatuses.com/101"101 )102103 # 3xx status codes are redirections.104 headers = {"location": "https://other.org"}105 response = httpx.Response(303, headers=headers, request=request)106 assert response.is_redirect107 with pytest.raises(httpx.HTTPStatusError) as exc_info:108 response.raise_for_status()109 assert str(exc_info.value) == (110 "Redirect response '303 See Other' for url 'https://example.org'\n"111 "Redirect location: 'https://other.org'\n"112 "For more information check: https://httpstatuses.com/303"113 )114Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 115why, one step back115 # 4xx status codes are a client error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 116why, one step back116 response = httpx.Response(403, request=request)is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
lines 117–119why, one step back117 assert response.is_client_error118 assert response.is_error119 with pytest.raises(httpx.HTTPStatusError) as exc_info:Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 120why, one step back120 response.raise_for_status()is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
lines 121–124why, one step back121 assert str(exc_info.value) == (122 "Client error '403 Forbidden' for url 'https://example.org'\n"123 "For more information check: https://httpstatuses.com/403"124 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 125–126why, one step back125126 # 5xx status codes are a server error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 127why, one step back127 response = httpx.Response(500, request=request)is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
lines 128–130why, one step back128 assert response.is_server_error129 assert response.is_error130 with pytest.raises(httpx.HTTPStatusError) as exc_info:Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 131why, one step back131 response.raise_for_status()is_informational / is_success / is_redirect / is_client_error / is_server_error (#1854)Tom Christiebefore adoptiona761e17
lines 132–135why, one step back132 assert str(exc_info.value) == (133 "Server error '500 Internal Server Error' for url 'https://example.org'\n"134 "For more information check: https://httpstatuses.com/500"135 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 136–143why, one step back136137 # Calling .raise_for_status without setting a request instance is138 # not valid. Should raise a runtime error.139 response = httpx.Response(200)140 with pytest.raises(RuntimeError):141 response.raise_for_status()142143Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 144why, one step back144def test_response_repr():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 145–148why, one step back145 response = httpx.Response(146 200,147 content=b"Hello, world!",148 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 149–157why, one step back149 assert repr(response) == "<Response [200 OK]>"150151152def test_response_content_type_encoding():153 """154 Use the charset encoding in the Content-Type header if possible.155 """156 headers = {"Content-Type": "text-plain; charset=latin-1"}157 content = "Latin 1: ÿ".encode("latin-1")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 158–162why, one step back158 response = httpx.Response(159 200,160 content=content,161 headers=headers,162 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 163–168why, one step back163 assert response.text == "Latin 1: ÿ"164 assert response.encoding == "latin-1"165166167def test_response_autodetect_encoding():168 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 169why, one step back169 Autodetect encoding if there is no Content-Type header.Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 170why, one step back170 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 171why, one step back171 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 172–175why, one step back172 response = httpx.Response(173 200,174 content=content,175 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 176why, one step back176 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 177why, one step back177 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 178–184why, one step back178179180def test_response_fallback_to_autodetect():181 """182 Fallback to autodetection if we get an invalid charset in the Content-Type header.183 """184 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 185why, one step back185 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 186–190why, one step back186 response = httpx.Response(187 200,188 content=content,189 headers=headers,190 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 191why, one step back191 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 192why, one step back192 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 193–194why, one step back193194Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 195why, one step back195def test_response_no_charset_with_ascii_content():Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 196why, one step back196 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 197–198why, one step back197 A response with ascii encoded content should decode correctly,198 even with no charset specified.Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 199–201why, one step back199 """200 content = b"Hello, world!"201 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 202–206why, one step back202 response = httpx.Response(203 200,204 content=content,205 headers=headers,206 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 207why, one step back207 assert response.status_code == 200Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 208why, one step back208 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 209–211why, one step back209 assert response.text == "Hello, world!"210211Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 212why, one step back212def test_response_no_charset_with_utf8_content():Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 213why, one step back213 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 214–215why, one step back214 A response with UTF-8 encoded content should decode correctly,215 even with no charset specified.Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 216why, one step back216 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 217–218why, one step back217 content = "Unicode Snowman: ☃".encode("utf-8")218 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 219–220why, one step back219 response = httpx.Response(220 200,Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 221–222why, one step back221 content=content,222 headers=headers,Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 223why, one step back223 )Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 224–225why, one step back224 assert response.text == "Unicode Snowman: ☃"225 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 226–227why, one step back226227Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 228–232why, one step back228def test_response_no_charset_with_iso_8859_1_content():229 """230 A response with ISO 8859-1 encoded content should decode correctly,231 even with no charset specified.232 """Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
line 233why, one step back233 content = "Accented: Österreich abcdefghijklmnopqrstuzwxyz".encode("iso-8859-1")Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 234–239why, one step back234 headers = {"Content-Type": "text/plain"}235 response = httpx.Response(236 200,237 content=content,238 headers=headers,239 )Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
lines 240–242why, one step back240 assert response.text == "Accented: Österreich abcdefghijklmnopqrstuzwxyz"241 assert response.charset_encoding is None242 assert response.apparent_encoding is not NoneDrop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 243–249why, one step back243244245def test_response_no_charset_with_cp_1252_content():246 """247 A response with Windows 1252 encoded content should decode correctly,248 even with no charset specified.249 """Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
line 250why, one step back250 content = "Euro Currency: € abcdefghijklmnopqrstuzwxyz".encode("cp1252")Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 251–256why, one step back251 headers = {"Content-Type": "text/plain"}252 response = httpx.Response(253 200,254 content=content,255 headers=headers,256 )Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
lines 257–259why, one step back257 assert response.text == "Euro Currency: € abcdefghijklmnopqrstuzwxyz"258 assert response.charset_encoding is None259 assert response.apparent_encoding is not NoneDrop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 260–261why, one step back260261Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 262–266why, one step back262def test_response_non_text_encoding():263 """264 Default to apparent encoding for non-text content-type headers.265 """266 headers = {"Content-Type": "image/png"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 267–271why, one step back267 response = httpx.Response(268 200,269 content=b"xyz",270 headers=headers,271 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 272why, one step back272 assert response.text == "xyz"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 273why, one step back273 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 274–280why, one step back274275276def test_response_set_explicit_encoding():277 headers = {278 "Content-Type": "text-plain; charset=utf-8"279 } # Deliberately incorrect charset280 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 281–283why, one step back281 200,282 content="Latin 1: ÿ".encode("latin-1"),283 headers=headers,Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 284–291why, one step back284 )285 response.encoding = "latin-1"286 assert response.text == "Latin 1: ÿ"287 assert response.encoding == "latin-1"288289290def test_response_force_encoding():291 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 292–293why, one step back292 200,293 content="Snowman: ☃".encode("utf-8"),Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 294–302why, one step back294 )295 response.encoding = "iso-8859-1"296 assert response.status_code == 200297 assert response.reason_phrase == "OK"298 assert response.text == "Snowman: â\x98\x83"299 assert response.encoding == "iso-8859-1"300301302def test_read():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 303–306why, one step back303 response = httpx.Response(304 200,305 content=b"Hello, world!",306 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 307–309why, one step back307308 assert response.status_code == 200309 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 310why, one step back310 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 311–319why, one step back311 assert response.is_closed312313 content = response.read()314315 assert content == b"Hello, world!"316 assert response.content == b"Hello, world!"317 assert response.is_closed318319Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 320–334why, one step back320def test_empty_read():321 response = httpx.Response(200)322323 assert response.status_code == 200324 assert response.text == ""325 assert response.encoding is None326 assert response.is_closed327328 content = response.read()329330 assert content == b""331 assert response.content == b""332 assert response.is_closed333334Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 335–336why, one step back335@pytest.mark.asyncio336async def test_aread():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 337–340why, one step back337 response = httpx.Response(338 200,339 content=b"Hello, world!",340 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 341–343why, one step back341342 assert response.status_code == 200343 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 344why, one step back344 assert response.encoding is NoneCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 345–353why, one step back345 assert response.is_closed346347 content = await response.aread()348349 assert content == b"Hello, world!"350 assert response.content == b"Hello, world!"351 assert response.is_closed352353Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 354–369why, one step back354@pytest.mark.asyncio355async def test_empty_aread():356 response = httpx.Response(200)357358 assert response.status_code == 200359 assert response.text == ""360 assert response.encoding is None361 assert response.is_closed362363 content = await response.aread()364365 assert content == b""366 assert response.content == b""367 assert response.is_closed368369Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 370why, one step back370def test_iter_raw():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 371–372why, one step back371 response = httpx.Response(372 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 373why, one step back373 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 374why, one step back374 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 375–381why, one step back375376 raw = b""377 for part in response.iter_raw():378 raw += part379 assert raw == b"Hello, world!"380381Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 382–398why, one step back382def test_iter_raw_with_chunksize():383 response = httpx.Response(200, content=streaming_body())384385 parts = [part for part in response.iter_raw(chunk_size=5)]386 assert parts == [b"Hello", b", wor", b"ld!"]387388 response = httpx.Response(200, content=streaming_body())389390 parts = [part for part in response.iter_raw(chunk_size=13)]391 assert parts == [b"Hello, world!"]392393 response = httpx.Response(200, content=streaming_body())394395 parts = [part for part in response.iter_raw(chunk_size=20)]396 assert parts == [b"Hello, world!"]397398Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 399–420why, one step back399def test_iter_raw_on_iterable():400 response = httpx.Response(401 200,402 content=StreamingBody(),403 )404405 raw = b""406 for part in response.iter_raw():407 raw += part408 assert raw == b"Hello, world!"409410411def test_iter_raw_on_async():412 response = httpx.Response(413 200,414 content=async_streaming_body(),415 )416417 with pytest.raises(RuntimeError):418 [part for part in response.iter_raw()]419420Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328
lines 421–430why, one step back421def test_close_on_async():422 response = httpx.Response(423 200,424 content=async_streaming_body(),425 )426427 with pytest.raises(RuntimeError):428 response.close()429430Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 431why, one step back431def test_iter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 432why, one step back432 response = httpx.Response(200, content=streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 433–439why, one step back433434 num_downloaded = response.num_bytes_downloaded435 for part in response.iter_raw():436 assert len(part) == (response.num_bytes_downloaded - num_downloaded)437 num_downloaded = response.num_bytes_downloaded438439Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 440–441why, one step back440@pytest.mark.asyncio441async def test_aiter_raw():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 442why, one step back442 response = httpx.Response(200, content=async_streaming_body())Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 443–447why, one step back443444 raw = b""445 async for part in response.aiter_raw():446 raw += part447 assert raw == b"Hello, world!"Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 448–450why, one step back448449450@pytest.mark.asyncioSupport for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 451–468why, one step back451async def test_aiter_raw_with_chunksize():452 response = httpx.Response(200, content=async_streaming_body())453454 parts = [part async for part in response.aiter_raw(chunk_size=5)]455 assert parts == [b"Hello", b", wor", b"ld!"]456457 response = httpx.Response(200, content=async_streaming_body())458459 parts = [part async for part in response.aiter_raw(chunk_size=13)]460 assert parts == [b"Hello, world!"]461462 response = httpx.Response(200, content=async_streaming_body())463464 parts = [part async for part in response.aiter_raw(chunk_size=20)]465 assert parts == [b"Hello, world!"]466467468@pytest.mark.asyncioDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 469–476why, one step back469async def test_aiter_raw_on_sync():470 response = httpx.Response(471 200,472 content=streaming_body(),473 )474475 with pytest.raises(RuntimeError):476 [part async for part in response.aiter_raw()]Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328
lines 477–487why, one step back477478479@pytest.mark.asyncio480async def test_aclose_on_sync():481 response = httpx.Response(482 200,483 content=streaming_body(),484 )485486 with pytest.raises(RuntimeError):487 await response.aclose()Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 488–490why, one step back488489490@pytest.mark.asyncioAdd progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 491why, one step back491async def test_aiter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 492why, one step back492 response = httpx.Response(200, content=async_streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 493–497why, one step back493494 num_downloaded = response.num_bytes_downloaded495 async for part in response.aiter_raw():496 assert len(part) == (response.num_bytes_downloaded - num_downloaded)497 num_downloaded = response.num_bytes_downloadedCleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 498–500why, one step back498499500def test_iter_bytes():Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
line 501why, one step back501 response = httpx.Response(200, content=b"Hello, world!")Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 502–508why, one step back502503 content = b""504 for part in response.iter_bytes():505 content += part506 assert content == b"Hello, world!"507508Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 509–522why, one step back509def test_iter_bytes_with_chunk_size():510 response = httpx.Response(200, content=streaming_body())511 parts = [part for part in response.iter_bytes(chunk_size=5)]512 assert parts == [b"Hello", b", wor", b"ld!"]513514 response = httpx.Response(200, content=streaming_body())515 parts = [part for part in response.iter_bytes(chunk_size=13)]516 assert parts == [b"Hello, world!"]517518 response = httpx.Response(200, content=streaming_body())519 parts = [part for part in response.iter_bytes(chunk_size=20)]520 assert parts == [b"Hello, world!"]521522Fix iter_bytes with empty content (#1827)Tom Christiebefore adoption10b60d4
lines 523–528why, one step back523def test_iter_bytes_with_empty_response():524 response = httpx.Response(200, content=b"")525 parts = [part for part in response.iter_bytes()]526 assert parts == []527528Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 529–530why, one step back529@pytest.mark.asyncio530async def test_aiter_bytes():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 531–534why, one step back531 response = httpx.Response(532 200,533 content=b"Hello, world!",534 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 535–541why, one step back535536 content = b""537 async for part in response.aiter_bytes():538 content += part539 assert content == b"Hello, world!"540541Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 542–556why, one step back542@pytest.mark.asyncio543async def test_aiter_bytes_with_chunk_size():544 response = httpx.Response(200, content=async_streaming_body())545 parts = [part async for part in response.aiter_bytes(chunk_size=5)]546 assert parts == [b"Hello", b", wor", b"ld!"]547548 response = httpx.Response(200, content=async_streaming_body())549 parts = [part async for part in response.aiter_bytes(chunk_size=13)]550 assert parts == [b"Hello, world!"]551552 response = httpx.Response(200, content=async_streaming_body())553 parts = [part async for part in response.aiter_bytes(chunk_size=20)]554 assert parts == [b"Hello, world!"]555556Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 557why, one step back557def test_iter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 558–561why, one step back558 response = httpx.Response(559 200,560 content=b"Hello, world!",561 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 562–568why, one step back562563 content = ""564 for part in response.iter_text():565 content += part566 assert content == "Hello, world!"567568Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 569–582why, one step back569def test_iter_text_with_chunk_size():570 response = httpx.Response(200, content=b"Hello, world!")571 parts = [part for part in response.iter_text(chunk_size=5)]572 assert parts == ["Hello", ", wor", "ld!"]573574 response = httpx.Response(200, content=b"Hello, world!")575 parts = [part for part in response.iter_text(chunk_size=13)]576 assert parts == ["Hello, world!"]577578 response = httpx.Response(200, content=b"Hello, world!")579 parts = [part for part in response.iter_text(chunk_size=20)]580 assert parts == ["Hello, world!"]581582Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 583–584why, one step back583@pytest.mark.asyncio584async def test_aiter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 585–588why, one step back585 response = httpx.Response(586 200,587 content=b"Hello, world!",588 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 589–593why, one step back589590 content = ""591 async for part in response.aiter_text():592 content += part593 assert content == "Hello, world!"Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 594–608why, one step back594595596@pytest.mark.asyncio597async def test_aiter_text_with_chunk_size():598 response = httpx.Response(200, content=b"Hello, world!")599 parts = [part async for part in response.aiter_text(chunk_size=5)]600 assert parts == ["Hello", ", wor", "ld!"]601602 response = httpx.Response(200, content=b"Hello, world!")603 parts = [part async for part in response.aiter_text(chunk_size=13)]604 assert parts == ["Hello, world!"]605606 response = httpx.Response(200, content=b"Hello, world!")607 parts = [part async for part in response.aiter_text(chunk_size=20)]608 assert parts == ["Hello, world!"]Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 609–611why, one step back609610611def test_iter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 612–615why, one step back612 response = httpx.Response(613 200,614 content=b"Hello,\nworld!",615 )Replace for loops with comprehensions (#1759)Almazbefore adoptionb839478
line 616why, one step back616 content = [line for line in response.iter_lines()]Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 617–621why, one step back617 assert content == ["Hello,\n", "world!"]618619620@pytest.mark.asyncio621async def test_aiter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 622–625why, one step back622 response = httpx.Response(623 200,624 content=b"Hello,\nworld!",625 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 626–633why, one step back626627 content = []628 async for line in response.aiter_lines():629 content.append(line)630 assert content == ["Hello,\n", "world!"]631632633def test_sync_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 634–635why, one step back634 response = httpx.Response(635 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 636why, one step back636 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 637why, one step back637 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 638–650why, one step back638639 assert response.status_code == 200640 assert not response.is_closed641642 content = response.read()643644 assert content == b"Hello, world!"645 assert response.content == b"Hello, world!"646 assert response.is_closed647648649@pytest.mark.asyncio650async def test_async_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 651–652why, one step back651 response = httpx.Response(652 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 653why, one step back653 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 654why, one step back654 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 655–666why, one step back655656 assert response.status_code == 200657 assert not response.is_closed658659 content = await response.aread()660661 assert content == b"Hello, world!"662 assert response.content == b"Hello, world!"663 assert response.is_closed664665666def test_cannot_read_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 667–668why, one step back667 response = httpx.Response(668 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 669why, one step back669 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 670why, one step back670 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 671–681why, one step back671672 content = b""673 for part in response.iter_bytes():674 content += part675676 with pytest.raises(httpx.StreamConsumed):677 response.read()678679680@pytest.mark.asyncio681async def test_cannot_aread_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 682–683why, one step back682 response = httpx.Response(683 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 684why, one step back684 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 685why, one step back685 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 686–695why, one step back686687 content = b""688 async for part in response.aiter_bytes():689 content += part690691 with pytest.raises(httpx.StreamConsumed):692 await response.aread()693694695def test_cannot_read_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 696–697why, one step back696 response = httpx.Response(697 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 698why, one step back698 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 699why, one step back699 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 700–701why, one step back700701 response.close()`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af
line 702why, one step back702 with pytest.raises(httpx.StreamClosed):Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 703–707why, one step back703 response.read()704705706@pytest.mark.asyncio707async def test_cannot_aread_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 708–709why, one step back708 response = httpx.Response(709 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 710why, one step back710 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 711why, one step back711 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 712–713why, one step back712713 await response.aclose()`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af
line 714why, one step back714 with pytest.raises(httpx.StreamClosed):Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 715–719why, one step back715 await response.aread()716717718@pytest.mark.asyncio719async def test_elapsed_not_available_until_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 720–721why, one step back720 response = httpx.Response(721 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 722why, one step back722 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 723why, one step back723 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 724–729why, one step back724725 with pytest.raises(RuntimeError):726 response.elapsed727728729def test_unknown_status_code():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 730–732why, one step back730 response = httpx.Response(731 600,732 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 733–741why, one step back733 assert response.status_code == 600734 assert response.reason_phrase == ""735 assert response.text == ""736737738def test_json_with_specified_encoding():739 data = {"greeting": "hello", "recipient": "world"}740 content = json.dumps(data).encode("utf-16")741 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 742–746why, one step back742 response = httpx.Response(743 200,744 content=content,745 headers=headers,746 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 747–753why, one step back747 assert response.json() == data748749750def test_json_with_options():751 data = {"greeting": "hello", "recipient": "world", "amount": 1}752 content = json.dumps(data).encode("utf-16")753 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 754–758why, one step back754 response = httpx.Response(755 200,756 content=content,757 headers=headers,758 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 759–761why, one step back759 assert response.json(parse_int=str)["amount"] == "1"760761Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
lines 762–775why, one step back762@pytest.mark.parametrize(763 "encoding",764 [765 "utf-8",766 "utf-8-sig",767 "utf-16",768 "utf-16-be",769 "utf-16-le",770 "utf-32",771 "utf-32-be",772 "utf-32-le",773 ],774)775def test_json_without_specified_charset(encoding):Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 776why, one step back776 data = {"greeting": "hello", "recipient": "world"}Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
line 777why, one step back777 content = json.dumps(data).encode(encoding)Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 778why, one step back778 headers = {"Content-Type": "application/json"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 779–783why, one step back779 response = httpx.Response(780 200,781 content=content,782 headers=headers,783 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 784–786why, one step back784 assert response.json() == data785786Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
lines 787–800why, one step back787@pytest.mark.parametrize(788 "encoding",789 [790 "utf-8",791 "utf-8-sig",792 "utf-16",793 "utf-16-be",794 "utf-16-le",795 "utf-32",796 "utf-32-be",797 "utf-32-le",798 ],799)800def test_json_with_specified_charset(encoding):Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 801why, one step back801 data = {"greeting": "hello", "recipient": "world"}Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a
lines 802–809why, one step back802 content = json.dumps(data).encode(encoding)803 headers = {"Content-Type": f"application/json; charset={encoding}"}804 response = httpx.Response(805 200,806 content=content,807 headers=headers,808 )809 assert response.json() == dataMake the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 810–811why, one step back810811Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
lines 812–828why, one step back812@pytest.mark.parametrize(813 "headers, expected",814 [815 (816 {"Link": "<https://example.com>; rel='preload'"},817 {"preload": {"rel": "preload", "url": "https://example.com"}},818 ),819 (820 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},821 {822 "hub": {"url": "/hub", "rel": "hub"},823 "self": {"url": "/resource", "rel": "self"},824 },825 ),826 ],827)828def test_link_headers(headers, expected):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 829–833why, one step back829 response = httpx.Response(830 200,831 content=None,832 headers=headers,833 )Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a
line 834why, one step back834 assert response.links == expectedMake the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 835–840why, one step back835836837@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))838def test_decode_error_with_request(header_value):839 headers = [(b"Content-Encoding", header_value)]840 body = b"test 123"Use either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312
line 841why, one step back841 compressed_body = brotli.compress(body)[3:]Transport API (#1522)Tom Christiebefore adoption1a6e254
line 842why, one step back842 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 843–848why, one step back843 httpx.Response(844 200,845 headers=headers,846 content=compressed_body,847 )848Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 849why, one step back849 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 850–855why, one step back850 httpx.Response(851 200,852 headers=headers,853 content=compressed_body,854 request=httpx.Request("GET", "https://www.example.org/"),855 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 856–861why, one step back856857858@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))859def test_value_error_without_request(header_value):860 headers = [(b"Content-Encoding", header_value)]861 body = b"test 123"Use either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312
line 862why, one step back862 compressed_body = brotli.compress(body)[3:]Transport API (#1522)Tom Christiebefore adoption1a6e254
line 863why, one step back863 with pytest.raises(httpx.DecodingError):Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 864–878why, one step back864 httpx.Response(200, headers=headers, content=compressed_body)865866867def test_response_with_unset_request():868 response = httpx.Response(200, content=b"Hello, world!")869870 assert response.status_code == 200871 assert response.reason_phrase == "OK"872 assert response.text == "Hello, world!"873 assert not response.is_error874875876def test_set_request_after_init():877 response = httpx.Response(200, content=b"Hello, world!")878Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 879why, one step back879 response.request = httpx.Request("GET", "https://www.example.org")Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 880why, one step back880Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 881–882why, one step back881 assert response.request.method == "GET"882 assert response.request.url == "https://www.example.org"Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 883–888why, one step back883884885def test_cannot_access_unset_request():886 response = httpx.Response(200, content=b"Hello, world!")887888 with pytest.raises(RuntimeError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 889why, one step back889 response.requestDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 890–896why, one step back890891892def test_generator_with_transfer_encoding_header():893 def content():894 yield b"test 123" # pragma: nocover895896 response = httpx.Response(200, content=content())Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 897why, one step back897 assert response.headers == {"Transfer-Encoding": "chunked"}Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 898–905why, one step back898899900def test_generator_with_content_length_header():901 def content():902 yield b"test 123" # pragma: nocover903904 headers = {"Content-Length": "8"}905 response = httpx.Response(200, content=content(), headers=headers)Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 906why, one step back906 assert response.headers == {"Content-Length": "8"}Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104
lines 907–944why, one step back907908909def test_response_picklable():910 response = httpx.Response(911 200,912 content=b"Hello, world!",913 request=httpx.Request("GET", "https://example.org"),914 )915 pickle_response = pickle.loads(pickle.dumps(response))916 assert pickle_response.is_closed is True917 assert pickle_response.is_stream_consumed is True918 assert pickle_response.next_request is None919 assert pickle_response.stream is not None920 assert pickle_response.content == b"Hello, world!"921 assert pickle_response.status_code == 200922 assert pickle_response.request.url == response.request.url923 assert pickle_response.extensions == {}924 assert pickle_response.history == []925926927@pytest.mark.asyncio928async def test_response_async_streaming_picklable():929 response = httpx.Response(200, content=async_streaming_body())930 pickle_response = pickle.loads(pickle.dumps(response))931 with pytest.raises(httpx.ResponseNotRead):932 pickle_response.content933 with pytest.raises(httpx.StreamClosed):934 await pickle_response.aread()935 assert pickle_response.is_stream_consumed is False936 assert pickle_response.num_bytes_downloaded == 0937 assert pickle_response.headers == {"Transfer-Encoding": "chunked"}938939 response = httpx.Response(200, content=async_streaming_body())940 await response.aread()941 pickle_response = pickle.loads(pickle.dumps(response))942 assert pickle_response.is_stream_consumed is True943 assert pickle_response.content == b"Hello, world!"944 assert pickle_response.num_bytes_downloaded == 13