Skip to content
Myr

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 file

Take 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 back
1import datetime

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 2–4why, one step back
2import json
3from unittest import mock4

Update READMETom Christiebefore adoption58461d4

lines 5–6why, one step back
5import pytest
6

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 7why, one step back
7import httpx

Update READMETom Christiebefore adoption58461d4

lines 8–9why, one step back
8
9

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 10why, one step back
10def streaming_body():

Update READMETom Christiebefore adoption58461d4

lines 11–14why, one step back
11 yield b"Hello, "
12 yield b"world!"1314

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 15–19why, one step back
15async def async_streaming_body():
16 yield b"Hello, "17 yield b"world!"1819

Update READMETom Christiebefore adoption58461d4

line 20why, one step back
20def test_response():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 21why, one step back
21 response = httpx.Response(200, content=b"Hello, world!")

Update READMETom Christiebefore adoption58461d4

lines 22–24why, one step back
22 assert response.status_code == 200
23 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)

Update READMETom Christiebefore adoption58461d4

lines 26–28why, one step back
26
2728def test_response_repr():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 29why, one step back
29 response = httpx.Response(200, content=b"Hello, world!")

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

line 30why, one step back
30 assert repr(response) == "<Response [200 OK]>"

Update READMETom Christiebefore adoption58461d4

lines 31–38why, one step back
31
3233def 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 back
39 response = httpx.Response(200, content=content, headers=headers)

Update READMETom Christiebefore adoption58461d4

lines 40–48why, one step back
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")

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 49why, one step back
49 response = httpx.Response(200, content=content)

Update READMETom Christiebefore adoption58461d4

lines 50–59why, one step back
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")

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 60why, one step back
60 response = httpx.Response(200, content=content, headers=headers)

Update READMETom Christiebefore adoption58461d4

lines 61–71why, one step back
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"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 72why, one step back
72 response = httpx.Response(200, content=content, headers=headers)

Update READMETom Christiebefore adoption58461d4

lines 73–81why, one step back
73 assert response.status_code == 200
74 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 back
82 response = httpx.Response(200, content=b"")

Update READMETom Christiebefore adoption58461d4

lines 83–91why, one step back
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"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 92why, one step back
92 response = httpx.Response(200, content=b"xyz", headers=headers)

Update READMETom Christiebefore adoption58461d4

lines 93–100why, one step back
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 charset

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 101why, one step back
101 response = httpx.Response(

Update READMETom Christiebefore adoption58461d4

lines 102–109why, one step back
102 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers
103 )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 back
110 response = httpx.Response(200, content="Snowman: ☃".encode("utf-8"))

Update READMETom Christiebefore adoption58461d4

lines 111–117why, one step back
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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 118why, one step back
118def test_read_response():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 119why, one step back
119 response = httpx.Response(200, content=b"Hello, world!")

Update READMETom Christiebefore adoption58461d4

lines 120–125why, one step back
120
121 assert response.status_code == 200122 assert response.text == "Hello, world!"123 assert response.encoding == "ascii"124 assert response.is_closed125

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption58461d4

lines 127–132why, one step back
127
128 assert content == b"Hello, world!"129 assert response.content == b"Hello, world!"130 assert response.is_closed131132

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 133why, one step back
133def test_raw_interface():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 134why, one step back
134 response = httpx.Response(200, content=b"Hello, world!")

Update READMETom Christiebefore adoption58461d4

lines 135–136why, one step back
135
136 raw = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption58461d4

lines 138–141why, one step back
138 raw += part
139 assert raw == b"Hello, world!"140141

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 142why, one step back
142def test_stream_interface():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 143why, one step back
143 response = httpx.Response(200, content=b"Hello, world!")

Update READMETom Christiebefore adoption58461d4

lines 144–145why, one step back
144
145 content = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 146why, one step back
146 for part in response.stream():

Update READMETom Christiebefore adoption58461d4

lines 147–151why, one step back
147 content += part
148 assert content == b"Hello, world!"149150151@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 152why, one step back
152async def test_async_stream_interface():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 153why, one step back
153 response = httpx.AsyncResponse(200, content=b"Hello, world!")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 154–161why, one step back
154
155 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 back
162 response = httpx.Response(200, content=b"Hello, world!")

Update READMETom Christiebefore adoption58461d4

line 163why, one step back
163

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 164–173why, one step back
164 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 back
174 response = httpx.AsyncResponse(200, content=b"Hello, world!")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 175why, one step back
175

Update READMETom Christiebefore adoption58461d4

lines 176–183why, one step back
176 await response.read()
177178 content = b""179 async for part in response.stream():180 content += part181 assert content == b"Hello, world!"182183

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 184why, one step back
184def test_streaming_response():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 185why, one step back
185 response = httpx.Response(200, content=streaming_body())

Update READMETom Christiebefore adoption58461d4

lines 186–189why, one step back
186
187 assert response.status_code == 200188 assert not response.is_closed189

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption58461d4

lines 191–197why, one step back
191
192 assert content == b"Hello, world!"193 assert response.content == b"Hello, world!"194 assert response.is_closed195196197@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 198why, one step back
198async def test_async_streaming_response():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 199why, one step back
199 response = httpx.AsyncResponse(200, content=async_streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 200–211why, one step back
200
201 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 back
212 response = httpx.Response(200, content=streaming_body())

Update READMETom Christiebefore adoption58461d4

lines 213–214why, one step back
213
214 content = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 215–217why, one step back
215 for part in response.stream():
216 content += part217

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 218why, one step back
218 with pytest.raises(httpx.StreamConsumed):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 219–223why, one step back
219 response.read()
220221222@pytest.mark.asyncio223async def test_async_cannot_read_after_stream_consumed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 224why, one step back
224 response = httpx.AsyncResponse(200, content=async_streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 225–226why, one step back
225
226 content = b""

Update READMETom Christiebefore adoption58461d4

lines 227–229why, one step back
227 async for part in response.stream():
228 content += part229

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 230why, one step back
230 with pytest.raises(httpx.StreamConsumed):

Update READMETom Christiebefore adoption58461d4

lines 231–233why, one step back
231 await response.read()
232233

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 234why, one step back
234def test_cannot_read_after_response_closed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 235why, one step back
235 response = httpx.Response(200, content=streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 236–238why, one step back
236
237 response.close()238

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 239why, one step back
239 with pytest.raises(httpx.ResponseClosed):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 240–244why, one step back
240 response.read()
241242243@pytest.mark.asyncio244async def test_async_cannot_read_after_response_closed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 245why, one step back
245 response = httpx.AsyncResponse(200, content=async_streaming_body())

Update READMETom Christiebefore adoption58461d4

lines 246–248why, one step back
246
247 await response.close()248

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 249why, one step back
249 with pytest.raises(httpx.ResponseClosed):

Update READMETom Christiebefore adoption58461d4

lines 250–253why, one step back
250 await response.read()
251252253def test_unknown_status_code():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 254why, one step back
254 response = httpx.Response(600)

Update READMETom Christiebefore adoption58461d4

lines 255–257why, one step back
255 assert response.status_code == 600
256 assert response.reason_phrase == ""257 assert response.text == ""

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 258–260why, one step back
258
259260def test_json_with_specified_encoding():

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

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

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 262–263why, one step back
262 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 back
264 response = httpx.Response(200, content=content, headers=headers)

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 265–268why, one step back
265 assert response.json() == data
266267268def test_json_with_options():

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

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

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 270–271why, one step back
270 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 back
272 response = httpx.Response(200, content=content, headers=headers)

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 273–276why, one step back
273 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 back
277 data = {"greeting": "hello", "recipient": "world"}

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 278–279why, one step back
278 content = json.dumps(data).encode("utf-32-be")
279 headers = {"Content-Type": "application/json"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 280why, one step back
280 response = httpx.Response(200, content=content, headers=headers)

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 281–284why, one step back
281 assert response.json() == data
282283284def test_json_without_specified_encoding_decode_error():

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

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

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 286–288why, one step back
286 content = json.dumps(data).encode("utf-32-be")
287 headers = {"Content-Type": "application/json"}288 # force incorrect guess from `guess_json_utf` to trigger error

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

lines 289–290why, one step back
289 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 back
291 with pytest.raises(json.JSONDecodeError):
292 response.json()

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

lines 293–294why, one step back
293
294

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 295–315why, one step back
295@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 == expected314315

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

lines 316–328why, one step back
316@pytest.mark.asyncio
317async 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!"