Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

298 lines · 9.0 KiB · Python

Last changed by

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

History of this file

Take it

The walk stopped after 200 commits: lines older than e9b2099 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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 2–7why, one step back
2import json
3from unittest import mock45import pytest67import httpx

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 8why, one step back
8from httpx.content_streams import AsyncIteratorStream

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

line 9why, one step back
9

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 10why, one step back
10REQUEST = httpx.Request("GET", "https://example.org")

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

line 11why, one step back
11

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 12why, one step back
12

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 13–23why, one step back
13def streaming_body():
14 yield b"Hello, "15 yield b"world!"161718async def async_streaming_body():19 yield b"Hello, "20 yield b"world!"212223def test_response():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 25–27why, one step back
25 assert response.status_code == 200
26 assert response.reason_phrase == "OK"27 assert response.text == "Hello, world!"

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 28why, one step back
28 assert response.request is REQUEST

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

line 29why, one step back
29 assert response.elapsed == datetime.timedelta(0)

Add Response.is_error (#574)Tom Christiebefore adoptionfdaa012

line 30why, one step back
30 assert not response.is_error

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 31–33why, one step back
31
3233def test_response_repr():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 35–43why, one step back
35 assert repr(response) == "<Response [200 OK]>"
363738def test_response_content_type_encoding():39 """40 Use the charset encoding in the Content-Type header if possible.41 """42 headers = {"Content-Type": "text-plain; charset=latin-1"}43 content = "Latin 1: ÿ".encode("latin-1")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 45–53why, one step back
45 assert response.text == "Latin 1: ÿ"
46 assert response.encoding == "latin-1"474849def test_response_autodetect_encoding():50 """51 Autodetect encoding if there is no charset info in a Content-Type header.52 """53 content = "おはようございます。".encode("EUC-JP")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 54why, one step back
54 response = httpx.Response(200, content=content, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 55–64why, one step back
55 assert response.text == "おはようございます。"
56 assert response.encoding == "EUC-JP"575859def test_response_fallback_to_autodetect():60 """61 Fallback to autodetection if we get an invalid charset in the Content-Type header.62 """63 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}64 content = "おはようございます。".encode("EUC-JP")

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 66–76why, one step back
66 assert response.text == "おはようございます。"
67 assert response.encoding == "EUC-JP"686970def test_response_default_text_encoding():71 """72 A media type of 'text/*' with no charset should default to ISO-8859-1.73 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.174 """75 content = b"Hello, world!"76 headers = {"Content-Type": "text/plain"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 78–86why, one step back
78 assert response.status_code == 200
79 assert response.encoding == "iso-8859-1"80 assert response.text == "Hello, world!"818283def test_response_default_encoding():84 """85 Default to utf-8 if all else fails.86 """

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 87why, one step back
87 response = httpx.Response(200, content=b"", request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 88–96why, one step back
88 assert response.text == ""
89 assert response.encoding == "utf-8"909192def test_response_non_text_encoding():93 """94 Default to apparent encoding for non-text content-type headers.95 """96 headers = {"Content-Type": "image/png"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 98–106why, one step back
98 assert response.text == "xyz"
99 assert response.encoding == "ascii"100101102def test_response_set_explicit_encoding():103 headers = {104 "Content-Type": "text-plain; charset=utf-8"105 } # Deliberately incorrect charset106 response = httpx.Response(

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 107why, one step back
107 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers, request=REQUEST,

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 108–114why, one step back
108 )
109 response.encoding = "latin-1"110 assert response.text == "Latin 1: ÿ"111 assert response.encoding == "latin-1"112113114def test_response_force_encoding():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 115–117why, one step back
115 response = httpx.Response(
116 200, content="Snowman: ☃".encode("utf-8"), request=REQUEST117 )

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 118–124why, one step back
118 response.encoding = "iso-8859-1"
119 assert response.status_code == 200120 assert response.reason_phrase == "OK"121 assert response.text == "Snowman: â\x98\x83"122 assert response.encoding == "iso-8859-1"123124

Drop sync (#544)Tom Christiebefore adoption206c537

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

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 128–133why, one step back
128
129 assert response.status_code == 200130 assert response.text == "Hello, world!"131 assert response.encoding == "ascii"132 assert response.is_closed133

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 135–140why, one step back
135
136 assert content == b"Hello, world!"137 assert response.content == b"Hello, world!"138 assert response.is_closed139140

Drop sync (#544)Tom Christiebefore adoption206c537

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

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

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

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 147–150why, one step back
147 raw += part
148 assert raw == b"Hello, world!"149150

Drop sync (#544)Tom Christiebefore adoption206c537

line 151why, one step back
151@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

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

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 154–155why, one step back
154
155 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 156why, one step back
156 async for part in response.aiter_bytes():

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 157–160why, one step back
157 content += part
158 assert content == b"Hello, world!"159160

Drop sync (#544)Tom Christiebefore adoption206c537

line 161why, one step back
161@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 162why, one step back
162async def test_text_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

line 164why, one step back
164

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

line 166why, one step back
166

Drop sync (#544)Tom Christiebefore adoption206c537

line 167why, one step back
167 content = ""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 168why, one step back
168 async for part in response.aiter_text():

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 171–173why, one step back
171
172173@pytest.mark.asyncio

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 174why, one step back
174async def test_lines_interface():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 175why, one step back
175 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 176–179why, one step back
176
177 await response.read()178179 content = []

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 180why, one step back
180 async for line in response.aiter_lines():

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 181–185why, one step back
181 content.append(line)
182 assert content == ["Hello,\n", "world!"]183184185@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 186why, one step back
186async def test_stream_interface_after_read():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 188–191why, one step back
188
189 await response.read()190191 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 192why, one step back
192 async for part in response.aiter_bytes():

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 193–197why, one step back
193 content += part
194 assert content == b"Hello, world!"195196197@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

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

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 199why, one step back
199 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 200why, one step back
200 response = httpx.Response(200, stream=stream, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 201–212why, one step back
201
202 assert response.status_code == 200203 assert not response.is_closed204205 content = await response.read()206207 assert content == b"Hello, world!"208 assert response.content == b"Hello, world!"209 assert response.is_closed210211212@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 213why, one step back
213async def test_cannot_read_after_stream_consumed():

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 214why, one step back
214 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 215why, one step back
215 response = httpx.Response(200, stream=stream, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 216–217why, one step back
216
217 content = b""

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 218why, one step back
218 async for part in response.aiter_bytes():

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 219–225why, one step back
219 content += part
220221 with pytest.raises(httpx.StreamConsumed):222 await response.read()223224225@pytest.mark.asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

line 226why, one step back
226async def test_cannot_read_after_response_closed():

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 227why, one step back
227 stream = AsyncIteratorStream(aiterator=async_streaming_body())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 228why, one step back
228 response = httpx.Response(200, stream=stream, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 229–236why, one step back
229
230 await response.close()231232 with pytest.raises(httpx.ResponseClosed):233 await response.read()234235236def test_unknown_status_code():

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 237why, one step back
237 response = httpx.Response(600, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 238–246why, one step back
238 assert response.status_code == 600
239 assert response.reason_phrase == ""240 assert response.text == ""241242243def test_json_with_specified_encoding():244 data = {"greeting": "hello", "recipient": "world"}245 content = json.dumps(data).encode("utf-16")246 headers = {"Content-Type": "application/json, charset=utf-16"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 248–254why, one step back
248 assert response.json() == data
249250251def test_json_with_options():252 data = {"greeting": "hello", "recipient": "world", "amount": 1}253 content = json.dumps(data).encode("utf-16")254 headers = {"Content-Type": "application/json, charset=utf-16"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 256–262why, one step back
256 assert response.json(parse_int=str)["amount"] == "1"
257258259def test_json_without_specified_encoding():260 data = {"greeting": "hello", "recipient": "world"}261 content = json.dumps(data).encode("utf-32-be")262 headers = {"Content-Type": "application/json"}

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

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

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 264–272why, one step back
264 assert response.json() == data
265266267def test_json_without_specified_encoding_decode_error():268 data = {"greeting": "hello", "recipient": "world"}269 content = json.dumps(data).encode("utf-32-be")270 headers = {"Content-Type": "application/json"}271 # force incorrect guess from `guess_json_utf` to trigger error272 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 273–275why, one step back
273 response = httpx.Response(
274 200, content=content, headers=headers, request=REQUEST275 )

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

lines 276–296why, one step back
276 with pytest.raises(json.JSONDecodeError):
277 response.json()278279280@pytest.mark.parametrize(281 "headers, expected",282 [283 (284 {"Link": "<https://example.com>; rel='preload'"},285 {"preload": {"rel": "preload", "url": "https://example.com"}},286 ),287 (288 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},289 {290 "hub": {"url": "/hub", "rel": "hub"},291 "self": {"url": "/resource", "rel": "self"},292 },293 ),294 ],295)296def test_link_headers(headers, expected):

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 297why, one step back
297 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)

Fix typos in comments (#294)Min ho Kimbefore adoptione9b2099

line 298why, one step back
298 assert response.links == expected