test_responses.py
This file
Path
tests/models/test_responses.py
298 lines · 9.0 KiB · Python
Last changed by
Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
History of this fileTake it
The walk stopped after 200 commits: lines older than e9b2099 point at it.
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 1why, one step back1import datetimeFix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 2–7why, one step back2import json3from unittest import mock45import pytest67import httpxUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 8why, one step back8from httpx.content_streams import AsyncIteratorStreamFix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 9why, one step back9Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 10why, one step back10REQUEST = httpx.Request("GET", "https://example.org")Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 11why, one step back11Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 12why, one step back12Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 13–23why, one step back13def streaming_body():14 yield b"Hello, "15 yield b"world!"161718async def async_streaming_body():19 yield b"Hello, "20 yield b"world!"212223def test_response():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 24why, one step back24 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 25–27why, one step back25 assert response.status_code == 20026 assert response.reason_phrase == "OK"27 assert response.text == "Hello, world!"Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 28why, one step back28 assert response.request is REQUESTAdd `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 29why, one step back29 assert response.elapsed == datetime.timedelta(0)Add Response.is_error (#574)Tom Christiebefore adoptionfdaa012
line 30why, one step back30 assert not response.is_errorFix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 31–33why, one step back313233def test_response_repr():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 34why, one step back34 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 35–43why, one step back35 assert repr(response) == "<Response [200 OK]>"363738def test_response_content_type_encoding():39 """40 Use the charset encoding in the Content-Type header if possible.41 """42 headers = {"Content-Type": "text-plain; charset=latin-1"}43 content = "Latin 1: ÿ".encode("latin-1")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 44why, one step back44 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 45–53why, one step back45 assert response.text == "Latin 1: ÿ"46 assert response.encoding == "latin-1"474849def test_response_autodetect_encoding():50 """51 Autodetect encoding if there is no charset info in a Content-Type header.52 """53 content = "おはようございます。".encode("EUC-JP")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 54why, one step back54 response = httpx.Response(200, content=content, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 55–64why, one step back55 assert response.text == "おはようございます。"56 assert response.encoding == "EUC-JP"575859def test_response_fallback_to_autodetect():60 """61 Fallback to autodetection if we get an invalid charset in the Content-Type header.62 """63 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}64 content = "おはようございます。".encode("EUC-JP")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 65why, one step back65 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 66–76why, one step back66 assert response.text == "おはようございます。"67 assert response.encoding == "EUC-JP"686970def test_response_default_text_encoding():71 """72 A media type of 'text/*' with no charset should default to ISO-8859-1.73 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.174 """75 content = b"Hello, world!"76 headers = {"Content-Type": "text/plain"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 77why, one step back77 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 78–86why, one step back78 assert response.status_code == 20079 assert response.encoding == "iso-8859-1"80 assert response.text == "Hello, world!"818283def test_response_default_encoding():84 """85 Default to utf-8 if all else fails.86 """Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 87why, one step back87 response = httpx.Response(200, content=b"", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 88–96why, one step back88 assert response.text == ""89 assert response.encoding == "utf-8"909192def test_response_non_text_encoding():93 """94 Default to apparent encoding for non-text content-type headers.95 """96 headers = {"Content-Type": "image/png"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 97why, one step back97 response = httpx.Response(200, content=b"xyz", headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 98–106why, one step back98 assert response.text == "xyz"99 assert response.encoding == "ascii"100101102def test_response_set_explicit_encoding():103 headers = {104 "Content-Type": "text-plain; charset=utf-8"105 } # Deliberately incorrect charset106 response = httpx.Response(Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 107why, one step back107 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers, request=REQUEST,Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 108–114why, one step back108 )109 response.encoding = "latin-1"110 assert response.text == "Latin 1: ÿ"111 assert response.encoding == "latin-1"112113114def test_response_force_encoding():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 115–117why, one step back115 response = httpx.Response(116 200, content="Snowman: ☃".encode("utf-8"), request=REQUEST117 )Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 118–124why, one step back118 response.encoding = "iso-8859-1"119 assert response.status_code == 200120 assert response.reason_phrase == "OK"121 assert response.text == "Snowman: â\x98\x83"122 assert response.encoding == "iso-8859-1"123124Drop sync (#544)Tom Christiebefore adoption206c537
lines 125–126why, one step back125@pytest.mark.asyncio126async def test_read_response():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 127why, one step back127 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 128–133why, one step back128129 assert response.status_code == 200130 assert response.text == "Hello, world!"131 assert response.encoding == "ascii"132 assert response.is_closed133Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 134why, one step back134 content = await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 135–140why, one step back135136 assert content == b"Hello, world!"137 assert response.content == b"Hello, world!"138 assert response.is_closed139140Drop sync (#544)Tom Christiebefore adoption206c537
lines 141–142why, one step back141@pytest.mark.asyncio142async def test_raw_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 143why, one step back143 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 144–145why, one step back144145 raw = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 146why, one step back146 async for part in response.aiter_raw():Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 147–150why, one step back147 raw += part148 assert raw == b"Hello, world!"149150Drop sync (#544)Tom Christiebefore adoption206c537
line 151why, one step back151@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 152why, one step back152async def test_bytes_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 153why, one step back153 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 154–155why, one step back154155 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 156why, one step back156 async for part in response.aiter_bytes():Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 157–160why, one step back157 content += part158 assert content == b"Hello, world!"159160Drop sync (#544)Tom Christiebefore adoption206c537
line 161why, one step back161@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 162why, one step back162async def test_text_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 163why, one step back163 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 164why, one step back164Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 165why, one step back165 await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 166why, one step back166Drop sync (#544)Tom Christiebefore adoption206c537
line 167why, one step back167 content = ""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 168why, one step back168 async for part in response.aiter_text():Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 169why, one step back169 content += partDrop sync (#544)Tom Christiebefore adoption206c537
line 170why, one step back170 assert content == "Hello, world!"Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 171–173why, one step back171172173@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 174why, one step back174async def test_lines_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 175why, one step back175 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 176why, one step back176Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 177why, one step back177 await response.aread()Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 178–179why, one step back178179 content = []Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 180why, one step back180 async for line in response.aiter_lines():Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 181–185why, one step back181 content.append(line)182 assert content == ["Hello,\n", "world!"]183184185@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 186why, one step back186async def test_stream_interface_after_read():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 187why, one step back187 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 188why, one step back188Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 189why, one step back189 await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 190–191why, one step back190191 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 192why, one step back192 async for part in response.aiter_bytes():Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 193–197why, one step back193 content += part194 assert content == b"Hello, world!"195196197@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 198why, one step back198async def test_streaming_response():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 199why, one step back199 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 200why, one step back200 response = httpx.Response(200, stream=stream, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 201–204why, one step back201202 assert response.status_code == 200203 assert not response.is_closed204Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 205why, one step back205 content = await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 206–212why, one step back206207 assert content == b"Hello, world!"208 assert response.content == b"Hello, world!"209 assert response.is_closed210211212@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 213why, one step back213async def test_cannot_read_after_stream_consumed():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 214why, one step back214 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 215why, one step back215 response = httpx.Response(200, stream=stream, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 216–217why, one step back216217 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 218why, one step back218 async for part in response.aiter_bytes():Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 219–221why, one step back219 content += part220221 with pytest.raises(httpx.StreamConsumed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 222why, one step back222 await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 223–225why, one step back223224225@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 226why, one step back226async def test_cannot_read_after_response_closed():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 227why, one step back227 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 228why, one step back228 response = httpx.Response(200, stream=stream, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 229why, one step back229Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 230why, one step back230 await response.aclose()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 231–232why, one step back231232 with pytest.raises(httpx.ResponseClosed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 233why, one step back233 await response.aread()Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 234–236why, one step back234235236def test_unknown_status_code():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 237why, one step back237 response = httpx.Response(600, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 238–246why, one step back238 assert response.status_code == 600239 assert response.reason_phrase == ""240 assert response.text == ""241242243def test_json_with_specified_encoding():244 data = {"greeting": "hello", "recipient": "world"}245 content = json.dumps(data).encode("utf-16")246 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 247why, one step back247 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 248–254why, one step back248 assert response.json() == data249250251def test_json_with_options():252 data = {"greeting": "hello", "recipient": "world", "amount": 1}253 content = json.dumps(data).encode("utf-16")254 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 255why, one step back255 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 256–262why, one step back256 assert response.json(parse_int=str)["amount"] == "1"257258259def test_json_without_specified_encoding():260 data = {"greeting": "hello", "recipient": "world"}261 content = json.dumps(data).encode("utf-32-be")262 headers = {"Content-Type": "application/json"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 263why, one step back263 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 264–272why, one step back264 assert response.json() == data265266267def test_json_without_specified_encoding_decode_error():268 data = {"greeting": "hello", "recipient": "world"}269 content = json.dumps(data).encode("utf-32-be")270 headers = {"Content-Type": "application/json"}271 # force incorrect guess from `guess_json_utf` to trigger error272 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 273–275why, one step back273 response = httpx.Response(274 200, content=content, headers=headers, request=REQUEST275 )Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
lines 276–296why, one step back276 with pytest.raises(json.JSONDecodeError):277 response.json()278279280@pytest.mark.parametrize(281 "headers, expected",282 [283 (284 {"Link": "<https://example.com>; rel='preload'"},285 {"preload": {"rel": "preload", "url": "https://example.com"}},286 ),287 (288 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},289 {290 "hub": {"url": "/hub", "rel": "hub"},291 "self": {"url": "/resource", "rel": "self"},292 },293 ),294 ],295)296def test_link_headers(headers, expected):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 297why, one step back297 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099
line 298why, one step back298 assert response.links == expected