test_responses.py
This file
Path
tests/models/test_responses.py
328 lines · 9.2 KiB · Python
Last changed by
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
History of this fileTake it
The walk stopped after 200 commits: lines older than 58461d4 point at it.
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 1why, one step back1import datetimeEncoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 2–4why, one step back2import json3from unittest import mock4Update READMETom Christiebefore adoption58461d4
lines 5–6why, one step back5import pytest6Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 7why, one step back7import httpxUpdate READMETom Christiebefore adoption58461d4
lines 8–9why, one step back89Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 10why, one step back10def streaming_body():Update READMETom Christiebefore adoption58461d4
lines 11–14why, one step back11 yield b"Hello, "12 yield b"world!"1314Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 15–19why, one step back15async def async_streaming_body():16 yield b"Hello, "17 yield b"world!"1819Update READMETom Christiebefore adoption58461d4
line 20why, one step back20def test_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 21why, one step back21 response = httpx.Response(200, content=b"Hello, world!")Update READMETom Christiebefore adoption58461d4
lines 22–24why, one step back22 assert response.status_code == 20023 assert response.reason_phrase == "OK"24 assert response.text == "Hello, world!"Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 25why, one step back25 assert response.elapsed == datetime.timedelta(0)Update READMETom Christiebefore adoption58461d4
lines 26–28why, one step back262728def test_response_repr():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 29why, one step back29 response = httpx.Response(200, content=b"Hello, world!")Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 30why, one step back30 assert repr(response) == "<Response [200 OK]>"Update READMETom Christiebefore adoption58461d4
lines 31–38why, one step back313233def test_response_content_type_encoding():34 """35 Use the charset encoding in the Content-Type header if possible.36 """37 headers = {"Content-Type": "text-plain; charset=latin-1"}38 content = "Latin 1: ÿ".encode("latin-1")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 39why, one step back39 response = httpx.Response(200, content=content, headers=headers)Update READMETom Christiebefore adoption58461d4
lines 40–48why, one step back40 assert response.text == "Latin 1: ÿ"41 assert response.encoding == "latin-1"424344def test_response_autodetect_encoding():45 """46 Autodetect encoding if there is no charset info in a Content-Type header.47 """48 content = "おはようございます。".encode("EUC-JP")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 49why, one step back49 response = httpx.Response(200, content=content)Update READMETom Christiebefore adoption58461d4
lines 50–59why, one step back50 assert response.text == "おはようございます。"51 assert response.encoding == "EUC-JP"525354def test_response_fallback_to_autodetect():55 """56 Fallback to autodetection if we get an invalid charset in the Content-Type header.57 """58 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}59 content = "おはようございます。".encode("EUC-JP")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 60why, one step back60 response = httpx.Response(200, content=content, headers=headers)Update READMETom Christiebefore adoption58461d4
lines 61–71why, one step back61 assert response.text == "おはようございます。"62 assert response.encoding == "EUC-JP"636465def test_response_default_text_encoding():66 """67 A media type of 'text/*' with no charset should default to ISO-8859-1.68 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.169 """70 content = b"Hello, world!"71 headers = {"Content-Type": "text/plain"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 72why, one step back72 response = httpx.Response(200, content=content, headers=headers)Update READMETom Christiebefore adoption58461d4
lines 73–81why, one step back73 assert response.status_code == 20074 assert response.encoding == "iso-8859-1"75 assert response.text == "Hello, world!"767778def test_response_default_encoding():79 """80 Default to utf-8 if all else fails.81 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 82why, one step back82 response = httpx.Response(200, content=b"")Update READMETom Christiebefore adoption58461d4
lines 83–91why, one step back83 assert response.text == ""84 assert response.encoding == "utf-8"858687def test_response_non_text_encoding():88 """89 Default to apparent encoding for non-text content-type headers.90 """91 headers = {"Content-Type": "image/png"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 92why, one step back92 response = httpx.Response(200, content=b"xyz", headers=headers)Update READMETom Christiebefore adoption58461d4
lines 93–100why, one step back93 assert response.text == "xyz"94 assert response.encoding == "ascii"959697def test_response_set_explicit_encoding():98 headers = {99 "Content-Type": "text-plain; charset=utf-8"100 } # Deliberately incorrect charsetRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 101why, one step back101 response = httpx.Response(Update READMETom Christiebefore adoption58461d4
lines 102–109why, one step back102 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers103 )104 response.encoding = "latin-1"105 assert response.text == "Latin 1: ÿ"106 assert response.encoding == "latin-1"107108109def test_response_force_encoding():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 110why, one step back110 response = httpx.Response(200, content="Snowman: ☃".encode("utf-8"))Update READMETom Christiebefore adoption58461d4
lines 111–117why, one step back111 response.encoding = "iso-8859-1"112 assert response.status_code == 200113 assert response.reason_phrase == "OK"114 assert response.text == "Snowman: â\x98\x83"115 assert response.encoding == "iso-8859-1"116117Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 118why, one step back118def test_read_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 119why, one step back119 response = httpx.Response(200, content=b"Hello, world!")Update READMETom Christiebefore adoption58461d4
lines 120–125why, one step back120121 assert response.status_code == 200122 assert response.text == "Hello, world!"123 assert response.encoding == "ascii"124 assert response.is_closed125Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 126why, one step back126 content = response.read()Update READMETom Christiebefore adoption58461d4
lines 127–132why, one step back127128 assert content == b"Hello, world!"129 assert response.content == b"Hello, world!"130 assert response.is_closed131132Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 133why, one step back133def test_raw_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 134why, one step back134 response = httpx.Response(200, content=b"Hello, world!")Update READMETom Christiebefore adoption58461d4
lines 135–136why, one step back135136 raw = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 137why, one step back137 for part in response.raw():Update READMETom Christiebefore adoption58461d4
lines 138–141why, one step back138 raw += part139 assert raw == b"Hello, world!"140141Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 142why, one step back142def test_stream_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 143why, one step back143 response = httpx.Response(200, content=b"Hello, world!")Update READMETom Christiebefore adoption58461d4
lines 144–145why, one step back144145 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 146why, one step back146 for part in response.stream():Update READMETom Christiebefore adoption58461d4
lines 147–151why, one step back147 content += part148 assert content == b"Hello, world!"149150151@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 152why, one step back152async def test_async_stream_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 153why, one step back153 response = httpx.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 154–161why, one step back154155 content = b""156 async for part in response.stream():157 content += part158 assert content == b"Hello, world!"159160161def test_stream_interface_after_read():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 162why, one step back162 response = httpx.Response(200, content=b"Hello, world!")Update READMETom Christiebefore adoption58461d4
line 163why, one step back163Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 164–173why, one step back164 response.read()165166 content = b""167 for part in response.stream():168 content += part169 assert content == b"Hello, world!"170171172@pytest.mark.asyncio173async def test_async_stream_interface_after_read():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 174why, one step back174 response = httpx.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 175why, one step back175Update READMETom Christiebefore adoption58461d4
lines 176–183why, one step back176 await response.read()177178 content = b""179 async for part in response.stream():180 content += part181 assert content == b"Hello, world!"182183Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 184why, one step back184def test_streaming_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 185why, one step back185 response = httpx.Response(200, content=streaming_body())Update READMETom Christiebefore adoption58461d4
lines 186–189why, one step back186187 assert response.status_code == 200188 assert not response.is_closed189Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 190why, one step back190 content = response.read()Update READMETom Christiebefore adoption58461d4
lines 191–197why, one step back191192 assert content == b"Hello, world!"193 assert response.content == b"Hello, world!"194 assert response.is_closed195196197@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 198why, one step back198async def test_async_streaming_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 199why, one step back199 response = httpx.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 200–211why, one step back200201 assert response.status_code == 200202 assert not response.is_closed203204 content = await response.read()205206 assert content == b"Hello, world!"207 assert response.content == b"Hello, world!"208 assert response.is_closed209210211def test_cannot_read_after_stream_consumed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 212why, one step back212 response = httpx.Response(200, content=streaming_body())Update READMETom Christiebefore adoption58461d4
lines 213–214why, one step back213214 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 215–217why, one step back215 for part in response.stream():216 content += part217Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 218why, one step back218 with pytest.raises(httpx.StreamConsumed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 219–223why, one step back219 response.read()220221222@pytest.mark.asyncio223async def test_async_cannot_read_after_stream_consumed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 224why, one step back224 response = httpx.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 225–226why, one step back225226 content = b""Update READMETom Christiebefore adoption58461d4
lines 227–229why, one step back227 async for part in response.stream():228 content += part229Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 230why, one step back230 with pytest.raises(httpx.StreamConsumed):Update READMETom Christiebefore adoption58461d4
lines 231–233why, one step back231 await response.read()232233Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 234why, one step back234def test_cannot_read_after_response_closed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 235why, one step back235 response = httpx.Response(200, content=streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 236–238why, one step back236237 response.close()238Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 239why, one step back239 with pytest.raises(httpx.ResponseClosed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 240–244why, one step back240 response.read()241242243@pytest.mark.asyncio244async def test_async_cannot_read_after_response_closed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 245why, one step back245 response = httpx.AsyncResponse(200, content=async_streaming_body())Update READMETom Christiebefore adoption58461d4
lines 246–248why, one step back246247 await response.close()248Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 249why, one step back249 with pytest.raises(httpx.ResponseClosed):Update READMETom Christiebefore adoption58461d4
lines 250–253why, one step back250 await response.read()251252253def test_unknown_status_code():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 254why, one step back254 response = httpx.Response(600)Update READMETom Christiebefore adoption58461d4
lines 255–257why, one step back255 assert response.status_code == 600256 assert response.reason_phrase == ""257 assert response.text == ""Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 258–260why, one step back258259260def test_json_with_specified_encoding():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 261why, one step back261 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 262–263why, one step back262 content = json.dumps(data).encode("utf-16")263 headers = {"Content-Type": "application/json, charset=utf-16"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 264why, one step back264 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 265–268why, one step back265 assert response.json() == data266267268def test_json_with_options():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 269why, one step back269 data = {"greeting": "hello", "recipient": "world", "amount": 1}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 270–271why, one step back270 content = json.dumps(data).encode("utf-16")271 headers = {"Content-Type": "application/json, charset=utf-16"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 272why, one step back272 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 273–276why, one step back273 assert response.json(parse_int=str)["amount"] == "1"274275276def test_json_without_specified_encoding():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 277why, one step back277 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 278–279why, one step back278 content = json.dumps(data).encode("utf-32-be")279 headers = {"Content-Type": "application/json"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 280why, one step back280 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 281–284why, one step back281 assert response.json() == data282283284def test_json_without_specified_encoding_decode_error():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 285why, one step back285 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 286–288why, one step back286 content = json.dumps(data).encode("utf-32-be")287 headers = {"Content-Type": "application/json"}288 # force incorrect guess from `guess_json_utf` to trigger errorRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 289–290why, one step back289 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):290 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 291–292why, one step back291 with pytest.raises(json.JSONDecodeError):292 response.json()Implement Response.stream_text() (#183)Seth Michael Larsonbefore adoptionaf907e8
lines 293–294why, one step back293294Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 295–315why, one step back295@pytest.mark.parametrize(296 "headers, expected",297 [298 (299 {"Link": "<https://example.com>; rel='preload'"},300 {"preload": {"rel": "preload", "url": "https://example.com"}},301 ),302 (303 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},304 {305 "hub": {"url": "/hub", "rel": "hub"},306 "self": {"url": "/resource", "rel": "self"},307 },308 ),309 ],310)311def test_link_headers(headers, expected):312 response = httpx.Response(200, content=None, headers=headers)313 assert response.links == expected314315Implement Response.stream_text() (#183)Seth Michael Larsonbefore adoptionaf907e8
lines 316–328why, one step back316@pytest.mark.asyncio317async def test_stream_text():318 async def iterator():319 yield b"Hello, world!"320321 response = httpx.AsyncResponse(200, content=iterator().__aiter__())322323 await response.read()324325 content = ""326 async for part in response.stream_text():327 content += part328 assert content == "Hello, world!"