test_responses.py
This file
Path
tests/models/test_responses.py
309 lines · 9.3 KiB · Python
Last changed by
Bump coverage (#705)Tom Christiebefore adoptionb8394f1
History of this fileTake it
The walk stopped after 200 commits: lines older than 8155352 point at it.
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 1why, one step back1import datetimeDocument digest auth (#348)Yeray Diaz Diazbefore adoption8155352
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 AsyncIteratorStreamDocument digest auth (#348)Yeray Diaz Diazbefore adoption8155352
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")Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 11why, one step back11Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 12why, one step back12Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 13–22why, 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!"2122response.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e
lines 23–24why, one step back23@pytest.mark.asyncio24async def test_response():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 25why, one step back25 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)response.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e
line 26why, one step back26Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 27–29why, one step back27 assert response.status_code == 20028 assert response.reason_phrase == "OK"29 assert response.text == "Hello, world!"Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 30why, one step back30 assert response.request is REQUESTresponse.elapsed now reflects entire request/response time. (#692)Tom Christiebefore adoption6a1ee0e
line 31why, one step back31 assert response.elapsed >= datetime.timedelta(0)Add Response.is_error (#574)Tom Christiebefore adoptionfdaa012
line 32why, one step back32 assert not response.is_errorDocument digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 33–35why, one step back333435def test_response_repr():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 36why, one step back36 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 37–45why, one step back37 assert repr(response) == "<Response [200 OK]>"383940def test_response_content_type_encoding():41 """42 Use the charset encoding in the Content-Type header if possible.43 """44 headers = {"Content-Type": "text-plain; charset=latin-1"}45 content = "Latin 1: ÿ".encode("latin-1")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 46why, one step back46 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 47–55why, one step back47 assert response.text == "Latin 1: ÿ"48 assert response.encoding == "latin-1"495051def test_response_autodetect_encoding():52 """53 Autodetect encoding if there is no charset info in a Content-Type header.54 """55 content = "おはようございます。".encode("EUC-JP")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 56why, one step back56 response = httpx.Response(200, content=content, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 57–66why, one step back57 assert response.text == "おはようございます。"58 assert response.encoding == "EUC-JP"596061def test_response_fallback_to_autodetect():62 """63 Fallback to autodetection if we get an invalid charset in the Content-Type header.64 """65 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}66 content = "おはようございます。".encode("EUC-JP")Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 67why, one step back67 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 68–78why, one step back68 assert response.text == "おはようございます。"69 assert response.encoding == "EUC-JP"707172def test_response_default_text_encoding():73 """74 A media type of 'text/*' with no charset should default to ISO-8859-1.75 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.176 """77 content = b"Hello, world!"78 headers = {"Content-Type": "text/plain"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 79why, one step back79 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 80–88why, one step back80 assert response.status_code == 20081 assert response.encoding == "iso-8859-1"82 assert response.text == "Hello, world!"838485def test_response_default_encoding():86 """87 Default to utf-8 if all else fails.88 """Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 89why, one step back89 response = httpx.Response(200, content=b"", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 90–98why, one step back90 assert response.text == ""91 assert response.encoding == "utf-8"929394def test_response_non_text_encoding():95 """96 Default to apparent encoding for non-text content-type headers.97 """98 headers = {"Content-Type": "image/png"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 99why, one step back99 response = httpx.Response(200, content=b"xyz", headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 100–108why, one step back100 assert response.text == "xyz"101 assert response.encoding == "ascii"102103104def test_response_set_explicit_encoding():105 headers = {106 "Content-Type": "text-plain; charset=utf-8"107 } # Deliberately incorrect charset108 response = httpx.Response(Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 109why, one step back109 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers, request=REQUEST,Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 110–116why, one step back110 )111 response.encoding = "latin-1"112 assert response.text == "Latin 1: ÿ"113 assert response.encoding == "latin-1"114115116def test_response_force_encoding():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 117–119why, one step back117 response = httpx.Response(118 200, content="Snowman: ☃".encode("utf-8"), request=REQUEST119 )Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 120–126why, one step back120 response.encoding = "iso-8859-1"121 assert response.status_code == 200122 assert response.reason_phrase == "OK"123 assert response.text == "Snowman: â\x98\x83"124 assert response.encoding == "iso-8859-1"125126Drop sync (#544)Tom Christiebefore adoption206c537
lines 127–128why, one step back127@pytest.mark.asyncio128async def test_read_response():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 129why, one step back129 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 130–135why, one step back130131 assert response.status_code == 200132 assert response.text == "Hello, world!"133 assert response.encoding == "ascii"134 assert response.is_closed135Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 136why, one step back136 content = await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 137–142why, one step back137138 assert content == b"Hello, world!"139 assert response.content == b"Hello, world!"140 assert response.is_closed141142Drop sync (#544)Tom Christiebefore adoption206c537
lines 143–144why, one step back143@pytest.mark.asyncio144async def test_raw_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 145why, one step back145 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 146–147why, one step back146147 raw = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 148why, one step back148 async for part in response.aiter_raw():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 149–152why, one step back149 raw += part150 assert raw == b"Hello, world!"151152Drop sync (#544)Tom Christiebefore adoption206c537
line 153why, one step back153@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 154why, one step back154async def test_bytes_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 155why, one step back155 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 156–157why, one step back156157 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 158why, one step back158 async for part in response.aiter_bytes():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 159–162why, one step back159 content += part160 assert content == b"Hello, world!"161162Drop sync (#544)Tom Christiebefore adoption206c537
line 163why, one step back163@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 164why, one step back164async def test_text_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 165why, one step back165 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 166why, one step back166Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 167why, one step back167 await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 168why, one step back168Drop sync (#544)Tom Christiebefore adoption206c537
line 169why, one step back169 content = ""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 170why, one step back170 async for part in response.aiter_text():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 171why, one step back171 content += partDrop sync (#544)Tom Christiebefore adoption206c537
line 172why, one step back172 assert content == "Hello, world!"Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 173–175why, one step back173174175@pytest.mark.asyncioAdd aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 176why, one step back176async def test_lines_interface():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 177why, one step back177 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 178why, one step back178Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 179why, one step back179 await response.aread()Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 180–181why, one step back180181 content = []Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 182why, one step back182 async for line in response.aiter_lines():Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 183–187why, one step back183 content.append(line)184 assert content == ["Hello,\n", "world!"]185186187@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 188why, one step back188async def test_stream_interface_after_read():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 189why, one step back189 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 190why, one step back190Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 191why, one step back191 await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 192–193why, one step back192193 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 194why, one step back194 async for part in response.aiter_bytes():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 195–199why, one step back195 content += part196 assert content == b"Hello, world!"197198199@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 200why, one step back200async def test_streaming_response():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 201why, one step back201 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 202why, one step back202 response = httpx.Response(200, stream=stream, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 203–206why, one step back203204 assert response.status_code == 200205 assert not response.is_closed206Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 207why, one step back207 content = await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 208–214why, one step back208209 assert content == b"Hello, world!"210 assert response.content == b"Hello, world!"211 assert response.is_closed212213214@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 215why, one step back215async def test_cannot_read_after_stream_consumed():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 216why, one step back216 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 217why, one step back217 response = httpx.Response(200, stream=stream, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 218–219why, one step back218219 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 220why, one step back220 async for part in response.aiter_bytes():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 221–223why, one step back221 content += part222223 with pytest.raises(httpx.StreamConsumed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 224why, one step back224 await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 225–227why, one step back225226227@pytest.mark.asyncioDrop sync (#544)Tom Christiebefore adoption206c537
line 228why, one step back228async def test_cannot_read_after_response_closed():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 229why, one step back229 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 230why, one step back230 response = httpx.Response(200, stream=stream, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 231why, one step back231Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 232why, one step back232 await response.aclose()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 233–234why, one step back233234 with pytest.raises(httpx.ResponseClosed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 235why, one step back235 await response.aread()Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 236–237why, one step back236237Bump coverage (#705)Tom Christiebefore adoptionb8394f1
lines 238–246why, one step back238@pytest.mark.asyncio239async def test_elapsed_not_available_until_closed():240 stream = AsyncIteratorStream(aiterator=async_streaming_body())241 response = httpx.Response(200, stream=stream, request=REQUEST)242243 with pytest.raises(RuntimeError):244 response.elapsed245246Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 247why, one step back247def test_unknown_status_code():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 248why, one step back248 response = httpx.Response(600, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 249–257why, one step back249 assert response.status_code == 600250 assert response.reason_phrase == ""251 assert response.text == ""252253254def test_json_with_specified_encoding():255 data = {"greeting": "hello", "recipient": "world"}256 content = json.dumps(data).encode("utf-16")257 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 258why, one step back258 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 259–265why, one step back259 assert response.json() == data260261262def test_json_with_options():263 data = {"greeting": "hello", "recipient": "world", "amount": 1}264 content = json.dumps(data).encode("utf-16")265 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 266why, one step back266 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 267–273why, one step back267 assert response.json(parse_int=str)["amount"] == "1"268269270def test_json_without_specified_encoding():271 data = {"greeting": "hello", "recipient": "world"}272 content = json.dumps(data).encode("utf-32-be")273 headers = {"Content-Type": "application/json"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 274why, one step back274 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 275–283why, one step back275 assert response.json() == data276277278def test_json_without_specified_encoding_decode_error():279 data = {"greeting": "hello", "recipient": "world"}280 content = json.dumps(data).encode("utf-32-be")281 headers = {"Content-Type": "application/json"}282 # force incorrect guess from `guess_json_utf` to trigger error283 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 284–286why, one step back284 response = httpx.Response(285 200, content=content, headers=headers, request=REQUEST286 )Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 287–307why, one step back287 with pytest.raises(json.JSONDecodeError):288 response.json()289290291@pytest.mark.parametrize(292 "headers, expected",293 [294 (295 {"Link": "<https://example.com>; rel='preload'"},296 {"preload": {"rel": "preload", "url": "https://example.com"}},297 ),298 (299 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},300 {301 "hub": {"url": "/hub", "rel": "hub"},302 "self": {"url": "/resource", "rel": "self"},303 },304 ),305 ],306)307def test_link_headers(headers, expected):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 308why, one step back308 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 309why, one step back309 assert response.links == expected