Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

274 lines · 8.0 KiB · Python

Last changed by

Drop sync (#544)Tom Christiebefore adoption206c537

History of this file

Take it

The walk stopped after 200 commits: lines older than 77cf336 point at it.

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 1why, one step back
1import datetime

Version 0.6.8Tom Christiebefore adoption77cf336

lines 2–24why, one step back
2import json
3from unittest import mock45import pytest67import httpx8910def streaming_body():11 yield b"Hello, "12 yield b"world!"131415async def async_streaming_body():16 yield b"Hello, "17 yield b"world!"181920def test_response():21 response = httpx.Response(200, content=b"Hello, world!")22 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 back
25 assert response.elapsed == datetime.timedelta(0)

Version 0.6.8Tom Christiebefore adoption77cf336

lines 26–117why, one step back
26
2728def test_response_repr():29 response = httpx.Response(200, content=b"Hello, world!")30 assert repr(response) == "<Response [200 OK]>"313233def 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")39 response = httpx.Response(200, content=content, headers=headers)40 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")49 response = httpx.Response(200, content=content)50 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")60 response = httpx.Response(200, content=content, headers=headers)61 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"}72 response = httpx.Response(200, content=content, headers=headers)73 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 """82 response = httpx.Response(200, content=b"")83 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"}92 response = httpx.Response(200, content=b"xyz", headers=headers)93 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 charset101 response = httpx.Response(102 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():110 response = httpx.Response(200, content="Snowman: ☃".encode("utf-8"))111 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"116117

Drop sync (#544)Tom Christiebefore adoption206c537

lines 118–119why, one step back
118@pytest.mark.asyncio
119async def test_read_response():

Version 0.6.8Tom Christiebefore adoption77cf336

lines 120–126why, one step back
120 response = httpx.Response(200, content=b"Hello, world!")
121122 assert response.status_code == 200123 assert response.text == "Hello, world!"124 assert response.encoding == "ascii"125 assert response.is_closed126

Drop sync (#544)Tom Christiebefore adoption206c537

line 127why, one step back
127 content = await response.read()

Version 0.6.8Tom Christiebefore adoption77cf336

lines 128–133why, one step back
128
129 assert content == b"Hello, world!"130 assert response.content == b"Hello, world!"131 assert response.is_closed132133

Drop sync (#544)Tom Christiebefore adoption206c537

lines 134–135why, one step back
134@pytest.mark.asyncio
135async def test_raw_interface():

Version 0.6.8Tom Christiebefore adoption77cf336

lines 136–138why, one step back
136 response = httpx.Response(200, content=b"Hello, world!")
137138 raw = b""

Drop sync (#544)Tom Christiebefore adoption206c537

line 139why, one step back
139 async for part in response.raw():

Version 0.6.8Tom Christiebefore adoption77cf336

lines 140–143why, one step back
140 raw += part
141 assert raw == b"Hello, world!"142143

Drop sync (#544)Tom Christiebefore adoption206c537

lines 144–145why, one step back
144@pytest.mark.asyncio
145async def test_stream_interface():

Version 0.6.8Tom Christiebefore adoption77cf336

lines 146–153why, one step back
146 response = httpx.Response(200, content=b"Hello, world!")
147148 content = b""149 async for part in response.stream():150 content += part151 assert content == b"Hello, world!"152153

Drop sync (#544)Tom Christiebefore adoption206c537

lines 154–155why, one step back
154@pytest.mark.asyncio
155async def test_stream_text():

Version 0.6.8Tom Christiebefore adoption77cf336

lines 156–157why, one step back
156 response = httpx.Response(200, content=b"Hello, world!")
157

Drop sync (#544)Tom Christiebefore adoption206c537

line 158why, one step back
158 await response.read()

Version 0.6.8Tom Christiebefore adoption77cf336

line 159why, one step back
159

Drop sync (#544)Tom Christiebefore adoption206c537

lines 160–161why, one step back
160 content = ""
161 async for part in response.stream_text():

Version 0.6.8Tom Christiebefore adoption77cf336

line 162why, one step back
162 content += part

Drop sync (#544)Tom Christiebefore adoption206c537

line 163why, one step back
163 assert content == "Hello, world!"

Version 0.6.8Tom Christiebefore adoption77cf336

lines 164–166why, one step back
164
165166@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 167–168why, one step back
167async def test_stream_interface_after_read():
168 response = httpx.Response(200, content=b"Hello, world!")

Version 0.6.8Tom Christiebefore adoption77cf336

lines 169–178why, one step back
169
170 await response.read()171172 content = b""173 async for part in response.stream():174 content += part175 assert content == b"Hello, world!"176177178@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 179–180why, one step back
179async def test_streaming_response():
180 response = httpx.Response(200, content=async_streaming_body())

Version 0.6.8Tom Christiebefore adoption77cf336

lines 181–192why, one step back
181
182 assert response.status_code == 200183 assert not response.is_closed184185 content = await response.read()186187 assert content == b"Hello, world!"188 assert response.content == b"Hello, world!"189 assert response.is_closed190191192@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 193–194why, one step back
193async def test_cannot_read_after_stream_consumed():
194 response = httpx.Response(200, content=async_streaming_body())

Version 0.6.8Tom Christiebefore adoption77cf336

lines 195–204why, one step back
195
196 content = b""197 async for part in response.stream():198 content += part199200 with pytest.raises(httpx.StreamConsumed):201 await response.read()202203204@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 205–206why, one step back
205async def test_cannot_read_after_response_closed():
206 response = httpx.Response(200, content=async_streaming_body())

Version 0.6.8Tom Christiebefore adoption77cf336

lines 207–221why, one step back
207
208 await response.close()209210 with pytest.raises(httpx.ResponseClosed):211 await response.read()212213214def test_unknown_status_code():215 response = httpx.Response(600)216 assert response.status_code == 600217 assert response.reason_phrase == ""218 assert response.text == ""219220221def test_json_with_specified_encoding():

Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b

line 222why, one step back
222 data = {"greeting": "hello", "recipient": "world"}

Version 0.6.8Tom Christiebefore adoption77cf336

lines 223–229why, one step back
223 content = json.dumps(data).encode("utf-16")
224 headers = {"Content-Type": "application/json, charset=utf-16"}225 response = httpx.Response(200, content=content, headers=headers)226 assert response.json() == data227228229def test_json_with_options():

Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b

line 230why, one step back
230 data = {"greeting": "hello", "recipient": "world", "amount": 1}

Version 0.6.8Tom Christiebefore adoption77cf336

lines 231–237why, one step back
231 content = json.dumps(data).encode("utf-16")
232 headers = {"Content-Type": "application/json, charset=utf-16"}233 response = httpx.Response(200, content=content, headers=headers)234 assert response.json(parse_int=str)["amount"] == "1"235236237def test_json_without_specified_encoding():

Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b

line 238why, one step back
238 data = {"greeting": "hello", "recipient": "world"}

Version 0.6.8Tom Christiebefore adoption77cf336

lines 239–245why, one step back
239 content = json.dumps(data).encode("utf-32-be")
240 headers = {"Content-Type": "application/json"}241 response = httpx.Response(200, content=content, headers=headers)242 assert response.json() == data243244245def test_json_without_specified_encoding_decode_error():

Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b

line 246why, one step back
246 data = {"greeting": "hello", "recipient": "world"}

Version 0.6.8Tom Christiebefore adoption77cf336

lines 247–253why, one step back
247 content = json.dumps(data).encode("utf-32-be")
248 headers = {"Content-Type": "application/json"}249 # force incorrect guess from `guess_json_utf` to trigger error250 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):251 response = httpx.Response(200, content=content, headers=headers)252 with pytest.raises(json.JSONDecodeError):253 response.json()

Implement Response.stream_text() (#183)Seth Michael Larsonbefore adoptionaf907e8

lines 254–255why, one step back
254
255

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 256–274why, one step back
256@pytest.mark.parametrize(
257 "headers, expected",258 [259 (260 {"Link": "<https://example.com>; rel='preload'"},261 {"preload": {"rel": "preload", "url": "https://example.com"}},262 ),263 (264 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},265 {266 "hub": {"url": "/hub", "rel": "hub"},267 "self": {"url": "/resource", "rel": "self"},268 },269 ),270 ],271)272def test_link_headers(headers, expected):273 response = httpx.Response(200, content=None, headers=headers)274 assert response.links == expected