test_responses.py
This file
Path
tests/models/test_responses.py
894 lines · 24.1 KiB · Python
Last changed by
Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104
History of this fileTake it
The walk stopped after 200 commits: lines older than f67e925 point at it.
Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 1why, one step back1import jsonMake Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104
line 2why, one step back2import pickleRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 3–4why, one step back3from unittest import mock4Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 5why, one step back5import brotliRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 6–10why, one step back6import pytest78import httpx910Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 11–16why, one step back11class StreamingBody:12 def __iter__(self):13 yield b"Hello, "14 yield b"world!"1516Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 17–26why, one step back17def streaming_body():18 yield b"Hello, "19 yield b"world!"202122async def async_streaming_body():23 yield b"Hello, "24 yield b"world!"2526Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 27why, one step back27def test_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 28–32why, one step back28 response = httpx.Response(29 200,30 content=b"Hello, world!",31 request=httpx.Request("GET", "https://example.org"),32 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 33–36why, one step back3334 assert response.status_code == 20035 assert response.reason_phrase == "OK"36 assert response.text == "Hello, world!"Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 37–38why, one step back37 assert response.request.method == "GET"38 assert response.request.url == "https://example.org"Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 39–41why, one step back39 assert not response.is_error4041Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd
lines 42–47why, one step back42def test_response_content():43 response = httpx.Response(200, content="Hello, world!")4445 assert response.status_code == 20046 assert response.reason_phrase == "OK"47 assert response.text == "Hello, world!"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 48why, one step back48 assert response.headers == {"Content-Length": "13"}Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd
lines 49–50why, one step back4950Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 51–56why, one step back51def test_response_text():52 response = httpx.Response(200, text="Hello, world!")5354 assert response.status_code == 20055 assert response.reason_phrase == "OK"56 assert response.text == "Hello, world!"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 57–60why, one step back57 assert response.headers == {58 "Content-Length": "13",59 "Content-Type": "text/plain; charset=utf-8",60 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 61–68why, one step back616263def test_response_html():64 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")6566 assert response.status_code == 20067 assert response.reason_phrase == "OK"68 assert response.text == "<html><body>Hello, world!</html></body>"Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 69–72why, one step back69 assert response.headers == {70 "Content-Length": "39",71 "Content-Type": "text/html; charset=utf-8",72 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 73–80why, one step back737475def test_response_json():76 response = httpx.Response(200, json={"hello": "world"})7778 assert response.status_code == 20079 assert response.reason_phrase == "OK"80 assert response.json() == {"hello": "world"}Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 81–84why, one step back81 assert response.headers == {82 "Content-Length": "18",83 "Content-Type": "application/json",84 }Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 85–86why, one step back8586Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 87why, one step back87def test_raise_for_status():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 88–89why, one step back88 request = httpx.Request("GET", "https://example.org")89Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 90why, one step back90 # 2xx status codes are not an error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 91why, one step back91 response = httpx.Response(200, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 92–94why, one step back92 response.raise_for_status()9394 # 4xx status codes are a client error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 95why, one step back95 response = httpx.Response(403, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 96–99why, one step back96 with pytest.raises(httpx.HTTPStatusError):97 response.raise_for_status()9899 # 5xx status codes are a server error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 100why, one step back100 response = httpx.Response(500, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 101–110why, one step back101 with pytest.raises(httpx.HTTPStatusError):102 response.raise_for_status()103104 # Calling .raise_for_status without setting a request instance is105 # not valid. Should raise a runtime error.106 response = httpx.Response(200)107 with pytest.raises(RuntimeError):108 response.raise_for_status()109110Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 111why, one step back111def test_response_repr():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 112–115why, one step back112 response = httpx.Response(113 200,114 content=b"Hello, world!",115 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 116–124why, one step back116 assert repr(response) == "<Response [200 OK]>"117118119def test_response_content_type_encoding():120 """121 Use the charset encoding in the Content-Type header if possible.122 """123 headers = {"Content-Type": "text-plain; charset=latin-1"}124 content = "Latin 1: ÿ".encode("latin-1")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 125–129why, one step back125 response = httpx.Response(126 200,127 content=content,128 headers=headers,129 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 130–135why, one step back130 assert response.text == "Latin 1: ÿ"131 assert response.encoding == "latin-1"132133134def test_response_autodetect_encoding():135 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 136why, one step back136 Autodetect encoding if there is no Content-Type header.Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 137why, one step back137 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 138why, one step back138 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 139–142why, one step back139 response = httpx.Response(140 200,141 content=content,142 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 143why, one step back143 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 144why, one step back144 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 145–151why, one step back145146147def test_response_fallback_to_autodetect():148 """149 Fallback to autodetection if we get an invalid charset in the Content-Type header.150 """151 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 152why, one step back152 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 153–157why, one step back153 response = httpx.Response(154 200,155 content=content,156 headers=headers,157 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 158why, one step back158 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 159why, one step back159 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 160–161why, one step back160161Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 162why, one step back162def test_response_no_charset_with_ascii_content():Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 163why, one step back163 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 164–165why, one step back164 A response with ascii encoded content should decode correctly,165 even with no charset specified.Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 166–168why, one step back166 """167 content = b"Hello, world!"168 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 169–173why, one step back169 response = httpx.Response(170 200,171 content=content,172 headers=headers,173 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 174why, one step back174 assert response.status_code == 200Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 175why, one step back175 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 176–178why, one step back176 assert response.text == "Hello, world!"177178Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 179why, one step back179def test_response_no_charset_with_utf8_content():Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 180why, one step back180 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 181–182why, one step back181 A response with UTF-8 encoded content should decode correctly,182 even with no charset specified.Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 183why, one step back183 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 184–185why, one step back184 content = "Unicode Snowman: ☃".encode("utf-8")185 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 186–187why, one step back186 response = httpx.Response(187 200,Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 188–189why, one step back188 content=content,189 headers=headers,Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 190why, one step back190 )Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 191–192why, one step back191 assert response.text == "Unicode Snowman: ☃"192 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 193–194why, one step back193194Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 195–226why, one step back195def test_response_no_charset_with_iso_8859_1_content():196 """197 A response with ISO 8859-1 encoded content should decode correctly,198 even with no charset specified.199 """200 content = "Accented: Österreich".encode("iso-8859-1")201 headers = {"Content-Type": "text/plain"}202 response = httpx.Response(203 200,204 content=content,205 headers=headers,206 )207 assert response.text == "Accented: Österreich"208 assert response.encoding is None209210211def test_response_no_charset_with_cp_1252_content():212 """213 A response with Windows 1252 encoded content should decode correctly,214 even with no charset specified.215 """216 content = "Euro Currency: €".encode("cp1252")217 headers = {"Content-Type": "text/plain"}218 response = httpx.Response(219 200,220 content=content,221 headers=headers,222 )223 assert response.text == "Euro Currency: €"224 assert response.encoding is None225226Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 227–231why, one step back227def test_response_non_text_encoding():228 """229 Default to apparent encoding for non-text content-type headers.230 """231 headers = {"Content-Type": "image/png"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 232–236why, one step back232 response = httpx.Response(233 200,234 content=b"xyz",235 headers=headers,236 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 237why, one step back237 assert response.text == "xyz"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 238why, one step back238 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 239–245why, one step back239240241def test_response_set_explicit_encoding():242 headers = {243 "Content-Type": "text-plain; charset=utf-8"244 } # Deliberately incorrect charset245 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 246–248why, one step back246 200,247 content="Latin 1: ÿ".encode("latin-1"),248 headers=headers,Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 249–256why, one step back249 )250 response.encoding = "latin-1"251 assert response.text == "Latin 1: ÿ"252 assert response.encoding == "latin-1"253254255def test_response_force_encoding():256 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 257–258why, one step back257 200,258 content="Snowman: ☃".encode("utf-8"),Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 259–267why, one step back259 )260 response.encoding = "iso-8859-1"261 assert response.status_code == 200262 assert response.reason_phrase == "OK"263 assert response.text == "Snowman: â\x98\x83"264 assert response.encoding == "iso-8859-1"265266267def test_read():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 268–271why, one step back268 response = httpx.Response(269 200,270 content=b"Hello, world!",271 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 272–274why, one step back272273 assert response.status_code == 200274 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 275why, one step back275 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 276–284why, one step back276 assert response.is_closed277278 content = response.read()279280 assert content == b"Hello, world!"281 assert response.content == b"Hello, world!"282 assert response.is_closed283284Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 285–299why, one step back285def test_empty_read():286 response = httpx.Response(200)287288 assert response.status_code == 200289 assert response.text == ""290 assert response.encoding is None291 assert response.is_closed292293 content = response.read()294295 assert content == b""296 assert response.content == b""297 assert response.is_closed298299Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 300–301why, one step back300@pytest.mark.asyncio301async def test_aread():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 302–305why, one step back302 response = httpx.Response(303 200,304 content=b"Hello, world!",305 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 306–308why, one step back306307 assert response.status_code == 200308 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 309why, one step back309 assert response.encoding is NoneRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 310–318why, one step back310 assert response.is_closed311312 content = await response.aread()313314 assert content == b"Hello, world!"315 assert response.content == b"Hello, world!"316 assert response.is_closed317318Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 319–334why, one step back319@pytest.mark.asyncio320async def test_empty_aread():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 = await response.aread()329330 assert content == b""331 assert response.content == b""332 assert response.is_closed333334Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 335why, one step back335def test_iter_raw():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 336–337why, one step back336 response = httpx.Response(337 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 338why, one step back338 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 339why, one step back339 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 340–346why, one step back340341 raw = b""342 for part in response.iter_raw():343 raw += part344 assert raw == b"Hello, world!"345346Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 347–363why, one step back347def test_iter_raw_with_chunksize():348 response = httpx.Response(200, content=streaming_body())349350 parts = [part for part in response.iter_raw(chunk_size=5)]351 assert parts == [b"Hello", b", wor", b"ld!"]352353 response = httpx.Response(200, content=streaming_body())354355 parts = [part for part in response.iter_raw(chunk_size=13)]356 assert parts == [b"Hello, world!"]357358 response = httpx.Response(200, content=streaming_body())359360 parts = [part for part in response.iter_raw(chunk_size=20)]361 assert parts == [b"Hello, world!"]362363Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 364–385why, one step back364def test_iter_raw_on_iterable():365 response = httpx.Response(366 200,367 content=StreamingBody(),368 )369370 raw = b""371 for part in response.iter_raw():372 raw += part373 assert raw == b"Hello, world!"374375376def test_iter_raw_on_async():377 response = httpx.Response(378 200,379 content=async_streaming_body(),380 )381382 with pytest.raises(RuntimeError):383 [part for part in response.iter_raw()]384385Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328
lines 386–395why, one step back386def test_close_on_async():387 response = httpx.Response(388 200,389 content=async_streaming_body(),390 )391392 with pytest.raises(RuntimeError):393 response.close()394395Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 396why, one step back396def test_iter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 397why, one step back397 response = httpx.Response(200, content=streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 398–404why, one step back398399 num_downloaded = response.num_bytes_downloaded400 for part in response.iter_raw():401 assert len(part) == (response.num_bytes_downloaded - num_downloaded)402 num_downloaded = response.num_bytes_downloaded403404Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 405–406why, one step back405@pytest.mark.asyncio406async def test_aiter_raw():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 407why, one step back407 response = httpx.Response(200, content=async_streaming_body())Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 408–412why, one step back408409 raw = b""410 async for part in response.aiter_raw():411 raw += part412 assert raw == b"Hello, world!"Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 413–415why, one step back413414415@pytest.mark.asyncioSupport for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 416–433why, one step back416async def test_aiter_raw_with_chunksize():417 response = httpx.Response(200, content=async_streaming_body())418419 parts = [part async for part in response.aiter_raw(chunk_size=5)]420 assert parts == [b"Hello", b", wor", b"ld!"]421422 response = httpx.Response(200, content=async_streaming_body())423424 parts = [part async for part in response.aiter_raw(chunk_size=13)]425 assert parts == [b"Hello, world!"]426427 response = httpx.Response(200, content=async_streaming_body())428429 parts = [part async for part in response.aiter_raw(chunk_size=20)]430 assert parts == [b"Hello, world!"]431432433@pytest.mark.asyncioDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 434–441why, one step back434async def test_aiter_raw_on_sync():435 response = httpx.Response(436 200,437 content=streaming_body(),438 )439440 with pytest.raises(RuntimeError):441 [part async for part in response.aiter_raw()]Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328
lines 442–452why, one step back442443444@pytest.mark.asyncio445async def test_aclose_on_sync():446 response = httpx.Response(447 200,448 content=streaming_body(),449 )450451 with pytest.raises(RuntimeError):452 await response.aclose()Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 453–455why, one step back453454455@pytest.mark.asyncioAdd progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 456why, one step back456async def test_aiter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 457why, one step back457 response = httpx.Response(200, content=async_streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 458–462why, one step back458459 num_downloaded = response.num_bytes_downloaded460 async for part in response.aiter_raw():461 assert len(part) == (response.num_bytes_downloaded - num_downloaded)462 num_downloaded = response.num_bytes_downloadedRename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 463–465why, one step back463464465def test_iter_bytes():Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
line 466why, one step back466 response = httpx.Response(200, content=b"Hello, world!")Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 467–473why, one step back467468 content = b""469 for part in response.iter_bytes():470 content += part471 assert content == b"Hello, world!"472473Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 474–487why, one step back474def test_iter_bytes_with_chunk_size():475 response = httpx.Response(200, content=streaming_body())476 parts = [part for part in response.iter_bytes(chunk_size=5)]477 assert parts == [b"Hello", b", wor", b"ld!"]478479 response = httpx.Response(200, content=streaming_body())480 parts = [part for part in response.iter_bytes(chunk_size=13)]481 assert parts == [b"Hello, world!"]482483 response = httpx.Response(200, content=streaming_body())484 parts = [part for part in response.iter_bytes(chunk_size=20)]485 assert parts == [b"Hello, world!"]486487Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 488–489why, one step back488@pytest.mark.asyncio489async def test_aiter_bytes():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 490–493why, one step back490 response = httpx.Response(491 200,492 content=b"Hello, world!",493 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 494–500why, one step back494495 content = b""496 async for part in response.aiter_bytes():497 content += part498 assert content == b"Hello, world!"499500Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 501–515why, one step back501@pytest.mark.asyncio502async def test_aiter_bytes_with_chunk_size():503 response = httpx.Response(200, content=async_streaming_body())504 parts = [part async for part in response.aiter_bytes(chunk_size=5)]505 assert parts == [b"Hello", b", wor", b"ld!"]506507 response = httpx.Response(200, content=async_streaming_body())508 parts = [part async for part in response.aiter_bytes(chunk_size=13)]509 assert parts == [b"Hello, world!"]510511 response = httpx.Response(200, content=async_streaming_body())512 parts = [part async for part in response.aiter_bytes(chunk_size=20)]513 assert parts == [b"Hello, world!"]514515Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 516why, one step back516def test_iter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 517–520why, one step back517 response = httpx.Response(518 200,519 content=b"Hello, world!",520 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 521–527why, one step back521522 content = ""523 for part in response.iter_text():524 content += part525 assert content == "Hello, world!"526527Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 528–541why, one step back528def test_iter_text_with_chunk_size():529 response = httpx.Response(200, content=b"Hello, world!")530 parts = [part for part in response.iter_text(chunk_size=5)]531 assert parts == ["Hello", ", wor", "ld!"]532533 response = httpx.Response(200, content=b"Hello, world!")534 parts = [part for part in response.iter_text(chunk_size=13)]535 assert parts == ["Hello, world!"]536537 response = httpx.Response(200, content=b"Hello, world!")538 parts = [part for part in response.iter_text(chunk_size=20)]539 assert parts == ["Hello, world!"]540541Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 542–543why, one step back542@pytest.mark.asyncio543async def test_aiter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 544–547why, one step back544 response = httpx.Response(545 200,546 content=b"Hello, world!",547 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 548–552why, one step back548549 content = ""550 async for part in response.aiter_text():551 content += part552 assert content == "Hello, world!"Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4
lines 553–567why, one step back553554555@pytest.mark.asyncio556async def test_aiter_text_with_chunk_size():557 response = httpx.Response(200, content=b"Hello, world!")558 parts = [part async for part in response.aiter_text(chunk_size=5)]559 assert parts == ["Hello", ", wor", "ld!"]560561 response = httpx.Response(200, content=b"Hello, world!")562 parts = [part async for part in response.aiter_text(chunk_size=13)]563 assert parts == ["Hello, world!"]564565 response = httpx.Response(200, content=b"Hello, world!")566 parts = [part async for part in response.aiter_text(chunk_size=20)]567 assert parts == ["Hello, world!"]Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 568–570why, one step back568569570def test_iter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 571–574why, one step back571 response = httpx.Response(572 200,573 content=b"Hello,\nworld!",574 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 575–583why, one step back575576 content = []577 for line in response.iter_lines():578 content.append(line)579 assert content == ["Hello,\n", "world!"]580581582@pytest.mark.asyncio583async def test_aiter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 584–587why, one step back584 response = httpx.Response(585 200,586 content=b"Hello,\nworld!",587 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 588–595why, one step back588589 content = []590 async for line in response.aiter_lines():591 content.append(line)592 assert content == ["Hello,\n", "world!"]593594595def test_sync_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 596–597why, one step back596 response = httpx.Response(597 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 598why, one step back598 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 599why, one step back599 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 600–612why, one step back600601 assert response.status_code == 200602 assert not response.is_closed603604 content = response.read()605606 assert content == b"Hello, world!"607 assert response.content == b"Hello, world!"608 assert response.is_closed609610611@pytest.mark.asyncio612async def test_async_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 613–614why, one step back613 response = httpx.Response(614 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 615why, one step back615 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 616why, one step back616 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 617–628why, one step back617618 assert response.status_code == 200619 assert not response.is_closed620621 content = await response.aread()622623 assert content == b"Hello, world!"624 assert response.content == b"Hello, world!"625 assert response.is_closed626627628def test_cannot_read_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 629–630why, one step back629 response = httpx.Response(630 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 631why, one step back631 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 632why, one step back632 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 633–643why, one step back633634 content = b""635 for part in response.iter_bytes():636 content += part637638 with pytest.raises(httpx.StreamConsumed):639 response.read()640641642@pytest.mark.asyncio643async def test_cannot_aread_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 644–645why, one step back644 response = httpx.Response(645 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 646why, one step back646 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 647why, one step back647 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 648–657why, one step back648649 content = b""650 async for part in response.aiter_bytes():651 content += part652653 with pytest.raises(httpx.StreamConsumed):654 await response.aread()655656657def test_cannot_read_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 658–659why, one step back658 response = httpx.Response(659 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 660why, one step back660 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 661why, one step back661 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 662–663why, one step back662663 response.close()`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af
line 664why, one step back664 with pytest.raises(httpx.StreamClosed):Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 665–669why, one step back665 response.read()666667668@pytest.mark.asyncio669async def test_cannot_aread_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 670–671why, one step back670 response = httpx.Response(671 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 672why, one step back672 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 673why, one step back673 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 674–675why, one step back674675 await response.aclose()`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af
line 676why, one step back676 with pytest.raises(httpx.StreamClosed):Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 677–681why, one step back677 await response.aread()678679680@pytest.mark.asyncio681async def test_elapsed_not_available_until_closed():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 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 686–691why, one step back686687 with pytest.raises(RuntimeError):688 response.elapsed689690691def test_unknown_status_code():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 692–694why, one step back692 response = httpx.Response(693 600,694 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 695–703why, one step back695 assert response.status_code == 600696 assert response.reason_phrase == ""697 assert response.text == ""698699700def test_json_with_specified_encoding():701 data = {"greeting": "hello", "recipient": "world"}702 content = json.dumps(data).encode("utf-16")703 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 704–708why, one step back704 response = httpx.Response(705 200,706 content=content,707 headers=headers,708 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 709–715why, one step back709 assert response.json() == data710711712def test_json_with_options():713 data = {"greeting": "hello", "recipient": "world", "amount": 1}714 content = json.dumps(data).encode("utf-16")715 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 716–720why, one step back716 response = httpx.Response(717 200,718 content=content,719 headers=headers,720 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 721–727why, one step back721 assert response.json(parse_int=str)["amount"] == "1"722723724def test_json_without_specified_encoding():725 data = {"greeting": "hello", "recipient": "world"}726 content = json.dumps(data).encode("utf-32-be")727 headers = {"Content-Type": "application/json"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 728–732why, one step back728 response = httpx.Response(729 200,730 content=content,731 headers=headers,732 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 733–742why, one step back733 assert response.json() == data734735736def test_json_without_specified_encoding_decode_error():737 data = {"greeting": "hello", "recipient": "world"}738 content = json.dumps(data).encode("utf-32-be")739 headers = {"Content-Type": "application/json"}740 # force incorrect guess from `guess_json_utf` to trigger error741 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):742 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 743–745why, one step back743 200,744 content=content,745 headers=headers,Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 746why, one step back746 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 747why, one step back747 with pytest.raises(json.decoder.JSONDecodeError):Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 748–750why, one step back748 response.json()749750Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 751–757why, one step back751def test_json_without_specified_encoding_value_error():752 data = {"greeting": "hello", "recipient": "world"}753 content = json.dumps(data).encode("utf-32-be")754 headers = {"Content-Type": "application/json"}755 # force incorrect guess from `guess_json_utf` to trigger error756 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):757 response = httpx.Response(200, content=content, headers=headers)Transport API (#1522)Tom Christiebefore adoption1a6e254
line 758why, one step back758 with pytest.raises(json.decoder.JSONDecodeError):Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 759–761why, one step back759 response.json()760761Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
lines 762–778why, one step back762@pytest.mark.parametrize(763 "headers, expected",764 [765 (766 {"Link": "<https://example.com>; rel='preload'"},767 {"preload": {"rel": "preload", "url": "https://example.com"}},768 ),769 (770 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},771 {772 "hub": {"url": "/hub", "rel": "hub"},773 "self": {"url": "/resource", "rel": "self"},774 },775 ),776 ],777)778def test_link_headers(headers, expected):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 779–783why, one step back779 response = httpx.Response(780 200,781 content=None,782 headers=headers,783 )Rename PoolLimits to Limits (#1113)Florimond Mancabefore adoptionf67e925
line 784why, one step back784 assert response.links == expectedMake the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 785–791why, one step back785786787@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))788def test_decode_error_with_request(header_value):789 headers = [(b"Content-Encoding", header_value)]790 body = b"test 123"791 compressed_body = brotli.compress(body)[3:]Transport API (#1522)Tom Christiebefore adoption1a6e254
line 792why, one step back792 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 793–798why, one step back793 httpx.Response(794 200,795 headers=headers,796 content=compressed_body,797 )798Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 799why, one step back799 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 800–805why, one step back800 httpx.Response(801 200,802 headers=headers,803 content=compressed_body,804 request=httpx.Request("GET", "https://www.example.org/"),805 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 806–812why, one step back806807808@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))809def test_value_error_without_request(header_value):810 headers = [(b"Content-Encoding", header_value)]811 body = b"test 123"812 compressed_body = brotli.compress(body)[3:]Transport API (#1522)Tom Christiebefore adoption1a6e254
line 813why, one step back813 with pytest.raises(httpx.DecodingError):Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 814–828why, one step back814 httpx.Response(200, headers=headers, content=compressed_body)815816817def test_response_with_unset_request():818 response = httpx.Response(200, content=b"Hello, world!")819820 assert response.status_code == 200821 assert response.reason_phrase == "OK"822 assert response.text == "Hello, world!"823 assert not response.is_error824825826def test_set_request_after_init():827 response = httpx.Response(200, content=b"Hello, world!")828Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 829why, one step back829 response.request = httpx.Request("GET", "https://www.example.org")Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 830why, one step back830Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 831–832why, one step back831 assert response.request.method == "GET"832 assert response.request.url == "https://www.example.org"Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 833–838why, one step back833834835def test_cannot_access_unset_request():836 response = httpx.Response(200, content=b"Hello, world!")837838 with pytest.raises(RuntimeError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 839why, one step back839 response.requestDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 840–846why, one step back840841842def test_generator_with_transfer_encoding_header():843 def content():844 yield b"test 123" # pragma: nocover845846 response = httpx.Response(200, content=content())Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 847why, one step back847 assert response.headers == {"Transfer-Encoding": "chunked"}Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 848–855why, one step back848849850def test_generator_with_content_length_header():851 def content():852 yield b"test 123" # pragma: nocover853854 headers = {"Content-Length": "8"}855 response = httpx.Response(200, content=content(), headers=headers)Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
line 856why, one step back856 assert response.headers == {"Content-Length": "8"}Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104
lines 857–894why, one step back857858859def test_response_picklable():860 response = httpx.Response(861 200,862 content=b"Hello, world!",863 request=httpx.Request("GET", "https://example.org"),864 )865 pickle_response = pickle.loads(pickle.dumps(response))866 assert pickle_response.is_closed is True867 assert pickle_response.is_stream_consumed is True868 assert pickle_response.next_request is None869 assert pickle_response.stream is not None870 assert pickle_response.content == b"Hello, world!"871 assert pickle_response.status_code == 200872 assert pickle_response.request.url == response.request.url873 assert pickle_response.extensions == {}874 assert pickle_response.history == []875876877@pytest.mark.asyncio878async def test_response_async_streaming_picklable():879 response = httpx.Response(200, content=async_streaming_body())880 pickle_response = pickle.loads(pickle.dumps(response))881 with pytest.raises(httpx.ResponseNotRead):882 pickle_response.content883 with pytest.raises(httpx.StreamClosed):884 await pickle_response.aread()885 assert pickle_response.is_stream_consumed is False886 assert pickle_response.num_bytes_downloaded == 0887 assert pickle_response.headers == {"Transfer-Encoding": "chunked"}888889 response = httpx.Response(200, content=async_streaming_body())890 await response.aread()891 pickle_response = pickle.loads(pickle.dumps(response))892 assert pickle_response.is_stream_consumed is True893 assert pickle_response.content == b"Hello, world!"894 assert pickle_response.num_bytes_downloaded == 13