test_responses.py
This file
Path
tests/models/test_responses.py
741 lines · 18.7 KiB · Python
Last changed by
Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
History of this fileTake it
The walk stopped after 200 commits: lines older than d568ecd point at it.
ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 1–3why, one step back1import json2from unittest import mock3Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 4why, one step back4import brotliASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 5–9why, one step back5import pytest67import httpx89Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 10–15why, one step back10class StreamingBody:11 def __iter__(self):12 yield b"Hello, "13 yield b"world!"1415ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
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 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
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"ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 38–40why, one step back38 assert not response.is_error3940Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941
lines 41–82why, one step back41def test_response_text():42 response = httpx.Response(200, text="Hello, world!")4344 assert response.status_code == 20045 assert response.reason_phrase == "OK"46 assert response.text == "Hello, world!"47 assert response.headers == httpx.Headers(48 {49 "Content-Length": "13",50 "Content-Type": "text/plain; charset=utf-8",51 }52 )535455def test_response_html():56 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")5758 assert response.status_code == 20059 assert response.reason_phrase == "OK"60 assert response.text == "<html><body>Hello, world!</html></body>"61 assert response.headers == httpx.Headers(62 {63 "Content-Length": "39",64 "Content-Type": "text/html; charset=utf-8",65 }66 )676869def test_response_json():70 response = httpx.Response(200, json={"hello": "world"})7172 assert response.status_code == 20073 assert response.reason_phrase == "OK"74 assert response.json() == {"hello": "world"}75 assert response.headers == httpx.Headers(76 {77 "Content-Length": "18",78 "Content-Type": "application/json",79 }80 )8182Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 83why, one step back83def test_raise_for_status():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 84–85why, one step back84 request = httpx.Request("GET", "https://example.org")85Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 86why, one step back86 # 2xx status codes are not an error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 87why, one step back87 response = httpx.Response(200, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 88–90why, one step back88 response.raise_for_status()8990 # 4xx status codes are a client error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 91why, one step back91 response = httpx.Response(403, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 92–95why, one step back92 with pytest.raises(httpx.HTTPStatusError):93 response.raise_for_status()9495 # 5xx status codes are a server error.Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 96why, one step back96 response = httpx.Response(500, request=request)Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 97–106why, one step back97 with pytest.raises(httpx.HTTPStatusError):98 response.raise_for_status()99100 # Calling .raise_for_status without setting a request instance is101 # not valid. Should raise a runtime error.102 response = httpx.Response(200)103 with pytest.raises(RuntimeError):104 response.raise_for_status()105106ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 107why, one step back107def test_response_repr():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 108–111why, one step back108 response = httpx.Response(109 200,110 content=b"Hello, world!",111 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 112–120why, one step back112 assert repr(response) == "<Response [200 OK]>"113114115def test_response_content_type_encoding():116 """117 Use the charset encoding in the Content-Type header if possible.118 """119 headers = {"Content-Type": "text-plain; charset=latin-1"}120 content = "Latin 1: ÿ".encode("latin-1")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 121–125why, one step back121 response = httpx.Response(122 200,123 content=content,124 headers=headers,125 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 126–131why, one step back126 assert response.text == "Latin 1: ÿ"127 assert response.encoding == "latin-1"128129130def test_response_autodetect_encoding():131 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 132why, one step back132 Autodetect encoding if there is no Content-Type header.ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 133why, one step back133 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 134why, one step back134 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 135–138why, one step back135 response = httpx.Response(136 200,137 content=content,138 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 139why, one step back139 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 140why, one step back140 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 141–147why, one step back141142143def test_response_fallback_to_autodetect():144 """145 Fallback to autodetection if we get an invalid charset in the Content-Type header.146 """147 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 148why, one step back148 content = "おはようございます。".encode("utf-8")Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 149–153why, one step back149 response = httpx.Response(150 200,151 content=content,152 headers=headers,153 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 154why, one step back154 assert response.text == "おはようございます。"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 155why, one step back155 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 156–157why, one step back156157Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 158why, one step back158def test_response_no_charset_with_ascii_content():ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 159why, one step back159 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 160–161why, one step back160 A response with ascii encoded content should decode correctly,161 even with no charset specified.ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 162–164why, one step back162 """163 content = b"Hello, world!"164 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 165–169why, one step back165 response = httpx.Response(166 200,167 content=content,168 headers=headers,169 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 170why, one step back170 assert response.status_code == 200Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 171why, one step back171 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 172–174why, one step back172 assert response.text == "Hello, world!"173174Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 175why, one step back175def test_response_no_charset_with_utf8_content():ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 176why, one step back176 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 177–178why, one step back177 A response with UTF-8 encoded content should decode correctly,178 even with no charset specified.ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 179why, one step back179 """Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 180–181why, one step back180 content = "Unicode Snowman: ☃".encode("utf-8")181 headers = {"Content-Type": "text/plain"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 182–183why, one step back182 response = httpx.Response(183 200,Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 184–185why, one step back184 content=content,185 headers=headers,Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 186why, one step back186 )Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 187–188why, one step back187 assert response.text == "Unicode Snowman: ☃"188 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 189–190why, one step back189190Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
lines 191–222why, one step back191def test_response_no_charset_with_iso_8859_1_content():192 """193 A response with ISO 8859-1 encoded content should decode correctly,194 even with no charset specified.195 """196 content = "Accented: Österreich".encode("iso-8859-1")197 headers = {"Content-Type": "text/plain"}198 response = httpx.Response(199 200,200 content=content,201 headers=headers,202 )203 assert response.text == "Accented: Österreich"204 assert response.encoding is None205206207def test_response_no_charset_with_cp_1252_content():208 """209 A response with Windows 1252 encoded content should decode correctly,210 even with no charset specified.211 """212 content = "Euro Currency: €".encode("cp1252")213 headers = {"Content-Type": "text/plain"}214 response = httpx.Response(215 200,216 content=content,217 headers=headers,218 )219 assert response.text == "Euro Currency: €"220 assert response.encoding is None221222ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 223–227why, one step back223def test_response_non_text_encoding():224 """225 Default to apparent encoding for non-text content-type headers.226 """227 headers = {"Content-Type": "image/png"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 228–232why, one step back228 response = httpx.Response(229 200,230 content=b"xyz",231 headers=headers,232 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 233why, one step back233 assert response.text == "xyz"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 234why, one step back234 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 235–241why, one step back235236237def test_response_set_explicit_encoding():238 headers = {239 "Content-Type": "text-plain; charset=utf-8"240 } # Deliberately incorrect charset241 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 242–244why, one step back242 200,243 content="Latin 1: ÿ".encode("latin-1"),244 headers=headers,ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 245–252why, one step back245 )246 response.encoding = "latin-1"247 assert response.text == "Latin 1: ÿ"248 assert response.encoding == "latin-1"249250251def test_response_force_encoding():252 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 253–254why, one step back253 200,254 content="Snowman: ☃".encode("utf-8"),ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 255–263why, one step back255 )256 response.encoding = "iso-8859-1"257 assert response.status_code == 200258 assert response.reason_phrase == "OK"259 assert response.text == "Snowman: â\x98\x83"260 assert response.encoding == "iso-8859-1"261262263def test_read():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 264–267why, one step back264 response = httpx.Response(265 200,266 content=b"Hello, world!",267 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 268–270why, one step back268269 assert response.status_code == 200270 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 271why, one step back271 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 272–280why, one step back272 assert response.is_closed273274 content = response.read()275276 assert content == b"Hello, world!"277 assert response.content == b"Hello, world!"278 assert response.is_closed279280Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 281–295why, one step back281def test_empty_read():282 response = httpx.Response(200)283284 assert response.status_code == 200285 assert response.text == ""286 assert response.encoding is None287 assert response.is_closed288289 content = response.read()290291 assert content == b""292 assert response.content == b""293 assert response.is_closed294295ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 296–297why, one step back296@pytest.mark.asyncio297async def test_aread():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 298–301why, one step back298 response = httpx.Response(299 200,300 content=b"Hello, world!",301 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 302–304why, one step back302303 assert response.status_code == 200304 assert response.text == "Hello, world!"Drop chardet (#1269)Tom Christiebefore adoptiond0fe113
line 305why, one step back305 assert response.encoding is NoneASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 306–314why, one step back306 assert response.is_closed307308 content = await response.aread()309310 assert content == b"Hello, world!"311 assert response.content == b"Hello, world!"312 assert response.is_closed313314Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 315–330why, one step back315@pytest.mark.asyncio316async def test_empty_aread():317 response = httpx.Response(200)318319 assert response.status_code == 200320 assert response.text == ""321 assert response.encoding is None322 assert response.is_closed323324 content = await response.aread()325326 assert content == b""327 assert response.content == b""328 assert response.is_closed329330ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 331why, one step back331def test_iter_raw():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 332–333why, one step back332 response = httpx.Response(333 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 334why, one step back334 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 335why, one step back335 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 336–342why, one step back336337 raw = b""338 for part in response.iter_raw():339 raw += part340 assert raw == b"Hello, world!"341342Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 343–364why, one step back343def test_iter_raw_on_iterable():344 response = httpx.Response(345 200,346 content=StreamingBody(),347 )348349 raw = b""350 for part in response.iter_raw():351 raw += part352 assert raw == b"Hello, world!"353354355def test_iter_raw_on_async():356 response = httpx.Response(357 200,358 content=async_streaming_body(),359 )360361 with pytest.raises(RuntimeError):362 [part for part in response.iter_raw()]363364Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 365why, one step back365def test_iter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 366why, one step back366 response = httpx.Response(200, content=streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 367–373why, one step back367368 num_downloaded = response.num_bytes_downloaded369 for part in response.iter_raw():370 assert len(part) == (response.num_bytes_downloaded - num_downloaded)371 num_downloaded = response.num_bytes_downloaded372373ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 374–375why, one step back374@pytest.mark.asyncio375async def test_aiter_raw():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 376why, one step back376 response = httpx.Response(200, content=async_streaming_body())ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 377–381why, one step back377378 raw = b""379 async for part in response.aiter_raw():380 raw += part381 assert raw == b"Hello, world!"Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 382–384why, one step back382383384@pytest.mark.asyncioDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 385–395why, one step back385async def test_aiter_raw_on_sync():386 response = httpx.Response(387 200,388 content=streaming_body(),389 )390391 with pytest.raises(RuntimeError):392 [part async for part in response.aiter_raw()]393394395@pytest.mark.asyncioAdd progress to streaming download (#1268)cdelerbefore adoptioned16eb3
line 396why, one step back396async def test_aiter_raw_increments_updates_counter():Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 397why, one step back397 response = httpx.Response(200, content=async_streaming_body())Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3
lines 398–402why, one step back398399 num_downloaded = response.num_bytes_downloaded400 async for part in response.aiter_raw():401 assert len(part) == (response.num_bytes_downloaded - num_downloaded)402 num_downloaded = response.num_bytes_downloadedASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 403–405why, one step back403404405def test_iter_bytes():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 406–409why, one step back406 response = httpx.Response(407 200,408 content=b"Hello, world!",409 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 410–418why, one step back410411 content = b""412 for part in response.iter_bytes():413 content += part414 assert content == b"Hello, world!"415416417@pytest.mark.asyncio418async def test_aiter_bytes():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 419–422why, one step back419 response = httpx.Response(420 200,421 content=b"Hello, world!",422 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 423–430why, one step back423424 content = b""425 async for part in response.aiter_bytes():426 content += part427 assert content == b"Hello, world!"428429430def test_iter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 431–434why, one step back431 response = httpx.Response(432 200,433 content=b"Hello, world!",434 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 435–443why, one step back435436 content = ""437 for part in response.iter_text():438 content += part439 assert content == "Hello, world!"440441442@pytest.mark.asyncio443async def test_aiter_text():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 444–447why, one step back444 response = httpx.Response(445 200,446 content=b"Hello, world!",447 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 448–455why, one step back448449 content = ""450 async for part in response.aiter_text():451 content += part452 assert content == "Hello, world!"453454455def test_iter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 456–459why, one step back456 response = httpx.Response(457 200,458 content=b"Hello,\nworld!",459 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 460–468why, one step back460461 content = []462 for line in response.iter_lines():463 content.append(line)464 assert content == ["Hello,\n", "world!"]465466467@pytest.mark.asyncio468async def test_aiter_lines():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 469–472why, one step back469 response = httpx.Response(470 200,471 content=b"Hello,\nworld!",472 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 473–480why, one step back473474 content = []475 async for line in response.aiter_lines():476 content.append(line)477 assert content == ["Hello,\n", "world!"]478479480def test_sync_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 481–482why, one step back481 response = httpx.Response(482 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 483why, one step back483 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 484why, one step back484 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 485–497why, one step back485486 assert response.status_code == 200487 assert not response.is_closed488489 content = response.read()490491 assert content == b"Hello, world!"492 assert response.content == b"Hello, world!"493 assert response.is_closed494495496@pytest.mark.asyncio497async def test_async_streaming_response():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 498–499why, one step back498 response = httpx.Response(499 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 500why, one step back500 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 501why, one step back501 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 502–513why, one step back502503 assert response.status_code == 200504 assert not response.is_closed505506 content = await response.aread()507508 assert content == b"Hello, world!"509 assert response.content == b"Hello, world!"510 assert response.is_closed511512513def test_cannot_read_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 514–515why, one step back514 response = httpx.Response(515 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 516why, one step back516 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 517why, one step back517 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 518–528why, one step back518519 content = b""520 for part in response.iter_bytes():521 content += part522523 with pytest.raises(httpx.StreamConsumed):524 response.read()525526527@pytest.mark.asyncio528async def test_cannot_aread_after_stream_consumed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 529–530why, one step back529 response = httpx.Response(530 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 531why, one step back531 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 532why, one step back532 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 533–542why, one step back533534 content = b""535 async for part in response.aiter_bytes():536 content += part537538 with pytest.raises(httpx.StreamConsumed):539 await response.aread()540541542def test_cannot_read_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 543–544why, one step back543 response = httpx.Response(544 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 545why, one step back545 content=streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 546why, one step back546 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 547–554why, one step back547548 response.close()549 with pytest.raises(httpx.ResponseClosed):550 response.read()551552553@pytest.mark.asyncio554async def test_cannot_aread_after_response_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 555–556why, one step back555 response = httpx.Response(556 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 557why, one step back557 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 558why, one step back558 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 559–566why, one step back559560 await response.aclose()561 with pytest.raises(httpx.ResponseClosed):562 await response.aread()563564565@pytest.mark.asyncio566async def test_elapsed_not_available_until_closed():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 567–568why, one step back567 response = httpx.Response(568 200,Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 569why, one step back569 content=async_streaming_body(),Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 570why, one step back570 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 571–576why, one step back571572 with pytest.raises(RuntimeError):573 response.elapsed574575576def test_unknown_status_code():Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 577–579why, one step back577 response = httpx.Response(578 600,579 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 580–588why, one step back580 assert response.status_code == 600581 assert response.reason_phrase == ""582 assert response.text == ""583584585def test_json_with_specified_encoding():586 data = {"greeting": "hello", "recipient": "world"}587 content = json.dumps(data).encode("utf-16")588 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 589–593why, one step back589 response = httpx.Response(590 200,591 content=content,592 headers=headers,593 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 594–600why, one step back594 assert response.json() == data595596597def test_json_with_options():598 data = {"greeting": "hello", "recipient": "world", "amount": 1}599 content = json.dumps(data).encode("utf-16")600 headers = {"Content-Type": "application/json, charset=utf-16"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 601–605why, one step back601 response = httpx.Response(602 200,603 content=content,604 headers=headers,605 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 606–612why, one step back606 assert response.json(parse_int=str)["amount"] == "1"607608609def test_json_without_specified_encoding():610 data = {"greeting": "hello", "recipient": "world"}611 content = json.dumps(data).encode("utf-32-be")612 headers = {"Content-Type": "application/json"}Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 613–617why, one step back613 response = httpx.Response(614 200,615 content=content,616 headers=headers,617 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 618–627why, one step back618 assert response.json() == data619620621def test_json_without_specified_encoding_decode_error():622 data = {"greeting": "hello", "recipient": "world"}623 content = json.dumps(data).encode("utf-32-be")624 headers = {"Content-Type": "application/json"}625 # force incorrect guess from `guess_json_utf` to trigger error626 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):627 response = httpx.Response(Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 628–630why, one step back628 200,629 content=content,630 headers=headers,ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 631why, one step back631 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 632why, one step back632 with pytest.raises(json.decoder.JSONDecodeError):ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 633–635why, one step back633 response.json()634635Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 636–646why, one step back636def test_json_without_specified_encoding_value_error():637 data = {"greeting": "hello", "recipient": "world"}638 content = json.dumps(data).encode("utf-32-be")639 headers = {"Content-Type": "application/json"}640 # force incorrect guess from `guess_json_utf` to trigger error641 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):642 response = httpx.Response(200, content=content, headers=headers)643 with pytest.raises(ValueError):644 response.json()645646ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
lines 647–663why, one step back647@pytest.mark.parametrize(648 "headers, expected",649 [650 (651 {"Link": "<https://example.com>; rel='preload'"},652 {"preload": {"rel": "preload", "url": "https://example.com"}},653 ),654 (655 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},656 {657 "hub": {"url": "/hub", "rel": "hub"},658 "self": {"url": "/resource", "rel": "self"},659 },660 ),661 ],662)663def test_link_headers(headers, expected):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 664–668why, one step back664 response = httpx.Response(665 200,666 content=None,667 headers=headers,668 )ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 669why, one step back669 assert response.links == expectedMake the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 670–676why, one step back670671672@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))673def test_decode_error_with_request(header_value):674 headers = [(b"Content-Encoding", header_value)]675 body = b"test 123"676 compressed_body = brotli.compress(body)[3:]Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 677–683why, one step back677 with pytest.raises(ValueError):678 httpx.Response(679 200,680 headers=headers,681 content=compressed_body,682 )683Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 684why, one step back684 with pytest.raises(httpx.DecodingError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 685–690why, one step back685 httpx.Response(686 200,687 headers=headers,688 content=compressed_body,689 request=httpx.Request("GET", "https://www.example.org/"),690 )Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 691–713why, one step back691692693@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))694def test_value_error_without_request(header_value):695 headers = [(b"Content-Encoding", header_value)]696 body = b"test 123"697 compressed_body = brotli.compress(body)[3:]698 with pytest.raises(ValueError):699 httpx.Response(200, headers=headers, content=compressed_body)700701702def test_response_with_unset_request():703 response = httpx.Response(200, content=b"Hello, world!")704705 assert response.status_code == 200706 assert response.reason_phrase == "OK"707 assert response.text == "Hello, world!"708 assert not response.is_error709710711def test_set_request_after_init():712 response = httpx.Response(200, content=b"Hello, world!")713Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 714why, one step back714 response.request = httpx.Request("GET", "https://www.example.org")Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
line 715why, one step back715Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
lines 716–717why, one step back716 assert response.request.method == "GET"717 assert response.request.url == "https://www.example.org"Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528
lines 718–723why, one step back718719720def test_cannot_access_unset_request():721 response = httpx.Response(200, content=b"Hello, world!")722723 with pytest.raises(RuntimeError):Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4
line 724why, one step back724 response.requestDrop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 725–741why, one step back725726727def test_generator_with_transfer_encoding_header():728 def content():729 yield b"test 123" # pragma: nocover730731 response = httpx.Response(200, content=content())732 assert response.headers == httpx.Headers({"Transfer-Encoding": "chunked"})733734735def test_generator_with_content_length_header():736 def content():737 yield b"test 123" # pragma: nocover738739 headers = {"Content-Length": "8"}740 response = httpx.Response(200, content=content(), headers=headers)741 assert response.headers == httpx.Headers({"Content-Length": "8"})