Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

326 lines · 9.1 KiB · Python

Last changed by

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

History of this file

Take it

The walk stopped after 200 commits: lines older than 891548e point at it.

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

lines 1–3why, one step back
1import json
2from unittest import mock3

Update READMETom Christiebefore adoption891548e

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6import httpx

Update READMETom Christiebefore adoption891548e

lines 7–8why, one step back
7
8

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption891548e

lines 10–13why, one step back
10 yield b"Hello, "
11 yield b"world!"1213

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 14–18why, one step back
14async def async_streaming_body():
15 yield b"Hello, "16 yield b"world!"1718

Update READMETom Christiebefore adoption891548e

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 21–25why, one step back
21 assert response.status_code == 200
22 assert response.reason_phrase == "OK"23 assert response.text == "Hello, world!"2425

Increase test coverageTom Christiebefore adoptione99ffbd

line 26why, one step back
26def test_response_repr():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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

Increase test coverageTom Christiebefore adoptione99ffbd

lines 29–30why, one step back
29
30

Update READMETom Christiebefore adoption891548e

lines 31–36why, one step back
31def test_response_content_type_encoding():
32 """33 Use the charset encoding in the Content-Type header if possible.34 """35 headers = {"Content-Type": "text-plain; charset=latin-1"}36 content = "Latin 1: ÿ".encode("latin-1")

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 59–62why, one step back
59 assert response.text == "おはようございます。"
60 assert response.encoding == "EUC-JP"6162

Increase test coverageTom Christiebefore adoptione99ffbd

line 63why, one step back
63def test_response_default_text_encoding():

Update READMETom Christiebefore adoption891548e

lines 64–69why, one step back
64 """
65 A media type of 'text/*' with no charset should default to ISO-8859-1.66 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.167 """68 content = b"Hello, world!"69 headers = {"Content-Type": "text/plain"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 71–79why, one step back
71 assert response.status_code == 200
72 assert response.encoding == "iso-8859-1"73 assert response.text == "Hello, world!"747576def test_response_default_encoding():77 """78 Default to utf-8 if all else fails.79 """

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 81–82why, one step back
81 assert response.text == ""
82 assert response.encoding == "utf-8"

Increase test coverageTom Christiebefore adoptione99ffbd

lines 83–89why, one step back
83
8485def test_response_non_text_encoding():86 """87 Default to apparent encoding for non-text content-type headers.88 """89 headers = {"Content-Type": "image/png"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Increase test coverageTom Christiebefore adoptione99ffbd

lines 91–92why, one step back
91 assert response.text == "xyz"
92 assert response.encoding == "ascii"

Update READMETom Christiebefore adoption891548e

lines 93–98why, one step back
93
9495def test_response_set_explicit_encoding():96 headers = {97 "Content-Type": "text-plain; charset=utf-8"98 } # Deliberately incorrect charset

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 100–107why, one step back
100 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers
101 )102 response.encoding = "latin-1"103 assert response.text == "Latin 1: ÿ"104 assert response.encoding == "latin-1"105106107def test_response_force_encoding():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 108why, one step back
108 response = httpx.Response(200, content="Snowman: ☃".encode("utf-8"))

Update READMETom Christiebefore adoption891548e

lines 109–115why, one step back
109 response.encoding = "iso-8859-1"
110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112 assert response.text == "Snowman: â\x98\x83"113 assert response.encoding == "iso-8859-1"114115

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 118–123why, one step back
118
119 assert response.status_code == 200120 assert response.text == "Hello, world!"121 assert response.encoding == "ascii"122 assert response.is_closed123

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption891548e

lines 125–130why, one step back
125
126 assert content == b"Hello, world!"127 assert response.content == b"Hello, world!"128 assert response.is_closed129130

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 133–134why, one step back
133
134 raw = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption891548e

lines 136–139why, one step back
136 raw += part
137 assert raw == b"Hello, world!"138139

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 142–143why, one step back
142
143 content = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption891548e

lines 145–149why, one step back
145 content += part
146 assert content == b"Hello, world!"147148149@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 152–159why, one step back
152
153 content = b""154 async for part in response.stream():155 content += part156 assert content == b"Hello, world!"157158159def test_stream_interface_after_read():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

line 161why, one step back
161

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 162–171why, one step back
162 response.read()
163164 content = b""165 for part in response.stream():166 content += part167 assert content == b"Hello, world!"168169170@pytest.mark.asyncio171async def test_async_stream_interface_after_read():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 173why, one step back
173

Update READMETom Christiebefore adoption891548e

lines 174–181why, one step back
174 await response.read()
175176 content = b""177 async for part in response.stream():178 content += part179 assert content == b"Hello, world!"180181

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 184–187why, one step back
184
185 assert response.status_code == 200186 assert not response.is_closed187

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Update READMETom Christiebefore adoption891548e

lines 189–195why, one step back
189
190 assert content == b"Hello, world!"191 assert response.content == b"Hello, world!"192 assert response.is_closed193194195@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 198–209why, one step back
198
199 assert response.status_code == 200200 assert not response.is_closed201202 content = await response.read()203204 assert content == b"Hello, world!"205 assert response.content == b"Hello, world!"206 assert response.is_closed207208209def test_cannot_read_after_stream_consumed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 211–212why, one step back
211
212 content = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 213–215why, one step back
213 for part in response.stream():
214 content += part215

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 217–221why, one step back
217 response.read()
218219220@pytest.mark.asyncio221async def test_async_cannot_read_after_stream_consumed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 223–224why, one step back
223
224 content = b""

Update READMETom Christiebefore adoption891548e

lines 225–227why, one step back
225 async for part in response.stream():
226 content += part227

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 229–231why, one step back
229 await response.read()
230231

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 234–236why, one step back
234
235 response.close()236

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 238–242why, one step back
238 response.read()
239240241@pytest.mark.asyncio242async def test_async_cannot_read_after_response_closed():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 244–246why, one step back
244
245 await response.close()246

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

lines 248–251why, one step back
248 await response.read()
249250251def test_unknown_status_code():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Update READMETom Christiebefore adoption891548e

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

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

lines 256–258why, one step back
256
257258def test_json_with_specified_encoding():

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

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

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

lines 260–261why, one step back
260 content = json.dumps(data).encode("utf-16")
261 headers = {"Content-Type": "application/json, charset=utf-16"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 263–266why, one step back
263 assert response.json() == data
264265266def test_json_with_options():

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

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

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

lines 268–269why, one step back
268 content = json.dumps(data).encode("utf-16")
269 headers = {"Content-Type": "application/json, charset=utf-16"}

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 271–274why, one step back
271 assert response.json(parse_int=str)["amount"] == "1"
272273274def test_json_without_specified_encoding():

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

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

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

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

lines 279–282why, one step back
279 assert response.json() == data
280281282def test_json_without_specified_encoding_decode_error():

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

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

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

lines 287–288why, one step back
287 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):
288 response = httpx.Response(200, content=content, headers=headers)

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

lines 289–290why, one step back
289 with pytest.raises(json.JSONDecodeError):
290 response.json()

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

lines 291–292why, one step back
291
292

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 293–313why, one step back
293@pytest.mark.parametrize(
294 "headers, expected",295 [296 (297 {"Link": "<https://example.com>; rel='preload'"},298 {"preload": {"rel": "preload", "url": "https://example.com"}},299 ),300 (301 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},302 {303 "hub": {"url": "/hub", "rel": "hub"},304 "self": {"url": "/resource", "rel": "self"},305 },306 ),307 ],308)309def test_link_headers(headers, expected):310 response = httpx.Response(200, content=None, headers=headers)311 assert response.links == expected312313

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

lines 314–326why, one step back
314@pytest.mark.asyncio
315async def test_stream_text():316 async def iterator():317 yield b"Hello, world!"318319 response = httpx.AsyncResponse(200, content=iterator().__aiter__())320321 await response.read()322323 content = ""324 async for part in response.stream_text():325 content += part326 assert content == "Hello, world!"