test_responses.py
This file
Path
tests/models/test_responses.py
398 lines · 11.6 KiB · Python
Last changed by
Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a96a2c point at it.
Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 1–7why, one step back1import datetime2import json3from unittest import mock45import pytest67import httpxSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
line 8why, one step back8from httpx._content_streams import AsyncIteratorStream, IteratorStreamAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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")Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 11why, one step back11Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 12why, one step back12Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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 back26Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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_errorAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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,Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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 )Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
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"125126Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 127–141why, one step back127def test_read():128 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)129130 assert response.status_code == 200131 assert response.text == "Hello, world!"132 assert response.encoding == "ascii"133 assert response.is_closed134135 content = response.read()136137 assert content == b"Hello, world!"138 assert response.content == b"Hello, world!"139 assert response.is_closed140141Drop sync (#544)Tom Christiebefore adoption206c537
line 142why, one step back142@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 143why, one step back143async def test_aread():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 144why, one step back144 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 145–150why, one step back145146 assert response.status_code == 200147 assert response.text == "Hello, world!"148 assert response.encoding == "ascii"149 assert response.is_closed150Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 151why, one step back151 content = await response.aread()Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 152–157why, one step back152153 assert content == b"Hello, world!"154 assert response.content == b"Hello, world!"155 assert response.is_closed156157Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 158–167why, one step back158def test_iter_raw():159 stream = IteratorStream(iterator=streaming_body())160 response = httpx.Response(200, stream=stream, request=REQUEST)161162 raw = b""163 for part in response.iter_raw():164 raw += part165 assert raw == b"Hello, world!"166167Drop sync (#544)Tom Christiebefore adoption206c537
line 168why, one step back168@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 169–171why, one step back169async def test_aiter_raw():170 stream = AsyncIteratorStream(aiterator=async_streaming_body())171 response = httpx.Response(200, stream=stream, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 172–173why, one step back172173 raw = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 174why, one step back174 async for part in response.aiter_raw():Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 175–178why, one step back175 raw += part176 assert raw == b"Hello, world!"177178Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 179–187why, one step back179def test_iter_bytes():180 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)181182 content = b""183 for part in response.iter_bytes():184 content += part185 assert content == b"Hello, world!"186187Drop sync (#544)Tom Christiebefore adoption206c537
line 188why, one step back188@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 189why, one step back189async def test_aiter_bytes():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 190why, one step back190 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 191–192why, one step back191192 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 193why, one step back193 async for part in response.aiter_bytes():Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 194–197why, one step back194 content += part195 assert content == b"Hello, world!"196197Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 198why, one step back198def test_iter_text():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 199why, one step back199 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 200why, one step back200Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 201–204why, one step back201 content = ""202 for part in response.iter_text():203 content += part204 assert content == "Hello, world!"Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 205why, one step back205Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 206–210why, one step back206207@pytest.mark.asyncio208async def test_aiter_text():209 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)210Drop sync (#544)Tom Christiebefore adoption206c537
line 211why, one step back211 content = ""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 212why, one step back212 async for part in response.aiter_text():Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 213why, one step back213 content += partDrop sync (#544)Tom Christiebefore adoption206c537
line 214why, one step back214 assert content == "Hello, world!"Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 215–216why, one step back215216Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 217why, one step back217def test_iter_lines():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 218why, one step back218 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 219why, one step back219Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 220–223why, one step back220 content = []221 for line in response.iter_lines():222 content.append(line)223 assert content == ["Hello,\n", "world!"]Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 224why, one step back224Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 225–229why, one step back225226@pytest.mark.asyncio227async def test_aiter_lines():228 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)229Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 230why, one step back230 content = []Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 231why, one step back231 async for line in response.aiter_lines():Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 232–235why, one step back232 content.append(line)233 assert content == ["Hello,\n", "world!"]234235Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 236–238why, one step back236def test_sync_streaming_response():237 stream = IteratorStream(iterator=streaming_body())238 response = httpx.Response(200, stream=stream, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 239why, one step back239Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 240–241why, one step back240 assert response.status_code == 200241 assert not response.is_closedAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 242why, one step back242Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 243–244why, one step back243 content = response.read()244Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 245why, one step back245 assert content == b"Hello, world!"Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 246–247why, one step back246 assert response.content == b"Hello, world!"247 assert response.is_closedAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 248–250why, one step back248249250@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 251why, one step back251async def test_async_streaming_response():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 252why, one step back252 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 253why, one step back253 response = httpx.Response(200, stream=stream, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 254–257why, one step back254255 assert response.status_code == 200256 assert not response.is_closed257Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 258why, one step back258 content = await response.aread()Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 259–264why, one step back259260 assert content == b"Hello, world!"261 assert response.content == b"Hello, world!"262 assert response.is_closed263264Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 265–276why, one step back265def test_cannot_read_after_stream_consumed():266 stream = IteratorStream(iterator=streaming_body())267 response = httpx.Response(200, stream=stream, request=REQUEST)268269 content = b""270 for part in response.iter_bytes():271 content += part272273 with pytest.raises(httpx.StreamConsumed):274 response.read()275276Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 277why, one step back277@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 278why, one step back278async def test_cannot_aread_after_stream_consumed():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 279why, one step back279 stream = AsyncIteratorStream(aiterator=async_streaming_body())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 280why, one step back280 response = httpx.Response(200, stream=stream, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 281–282why, one step back281282 content = b""Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04
line 283why, one step back283 async for part in response.aiter_bytes():Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 284–286why, one step back284 content += part285286 with pytest.raises(httpx.StreamConsumed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 287why, one step back287 await response.aread()Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 288–289why, one step back288289Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 290–306why, one step back290def test_cannot_read_after_response_closed():291 is_closed = False292293 def close_func():294 nonlocal is_closed295 is_closed = True296297 stream = IteratorStream(iterator=streaming_body(), close_func=close_func)298 response = httpx.Response(200, stream=stream, request=REQUEST)299300 response.close()301 assert is_closed302303 with pytest.raises(httpx.ResponseClosed):304 response.read()305306Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 307why, one step back307@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 308–317why, one step back308async def test_cannot_aread_after_response_closed():309 is_closed = False310311 async def close_func():312 nonlocal is_closed313 is_closed = True314315 stream = AsyncIteratorStream(316 aiterator=async_streaming_body(), close_func=close_func317 )Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 318why, one step back318 response = httpx.Response(200, stream=stream, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 319why, one step back319Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 320why, one step back320 await response.aclose()Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 321why, one step back321 assert is_closedAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 322–323why, one step back322323 with pytest.raises(httpx.ResponseClosed):Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 324why, one step back324 await response.aread()Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 325–326why, one step back325326Bump coverage (#705)Tom Christiebefore adoptionb8394f1
lines 327–335why, one step back327@pytest.mark.asyncio328async def test_elapsed_not_available_until_closed():329 stream = AsyncIteratorStream(aiterator=async_streaming_body())330 response = httpx.Response(200, stream=stream, request=REQUEST)331332 with pytest.raises(RuntimeError):333 response.elapsed334335Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 336why, one step back336def test_unknown_status_code():Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 337why, one step back337 response = httpx.Response(600, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 338–346why, one step back338 assert response.status_code == 600339 assert response.reason_phrase == ""340 assert response.text == ""341342343def test_json_with_specified_encoding():344 data = {"greeting": "hello", "recipient": "world"}345 content = json.dumps(data).encode("utf-16")346 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 347why, one step back347 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 348–354why, one step back348 assert response.json() == data349350351def test_json_with_options():352 data = {"greeting": "hello", "recipient": "world", "amount": 1}353 content = json.dumps(data).encode("utf-16")354 headers = {"Content-Type": "application/json, charset=utf-16"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 355why, one step back355 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 356–362why, one step back356 assert response.json(parse_int=str)["amount"] == "1"357358359def test_json_without_specified_encoding():360 data = {"greeting": "hello", "recipient": "world"}361 content = json.dumps(data).encode("utf-32-be")362 headers = {"Content-Type": "application/json"}Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 363why, one step back363 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 364–371why, one step back364 assert response.json() == data365366367def test_json_without_specified_encoding_decode_error():368 data = {"greeting": "hello", "recipient": "world"}369 content = json.dumps(data).encode("utf-32-be")370 headers = {"Content-Type": "application/json"}371 # force incorrect guess from `guess_json_utf` to trigger errorSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
line 372why, one step back372 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 373–375why, one step back373 response = httpx.Response(374 200, content=content, headers=headers, request=REQUEST375 )Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 376–396why, one step back376 with pytest.raises(json.JSONDecodeError):377 response.json()378379380@pytest.mark.parametrize(381 "headers, expected",382 [383 (384 {"Link": "<https://example.com>; rel='preload'"},385 {"preload": {"rel": "preload", "url": "https://example.com"}},386 ),387 (388 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},389 {390 "hub": {"url": "/hub", "rel": "hub"},391 "self": {"url": "/resource", "rel": "self"},392 },393 ),394 ],395)396def test_link_headers(headers, expected):Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 397why, one step back397 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
line 398why, one step back398 assert response.links == expected