Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

290 lines · 8.2 KiB · Python

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

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

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6import httpx

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 7–8why, one step back
7
8

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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!")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 21–22why, one step back
21 assert response.status_code == 200
22 assert response.reason_phrase == "OK"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 23why, one step back
23 assert response.text == "Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 24–25why, one step back
24
25

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 31why, one step back
31def test_response_content_type_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 32–34why, one step back
32 """
33 Use the charset encoding in the Content-Type header if possible.34 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 35why, one step back
35 headers = {"Content-Type": "text-plain; charset=latin-1"}

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

line 36why, one step back
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)

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 38–42why, one step back
38 assert response.text == "Latin 1: ÿ"
39 assert response.encoding == "latin-1"404142def test_response_autodetect_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 43–46why, one step back
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)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 50–51why, one step back
50
51

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 52–57why, one step back
52def 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)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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():

Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb

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)

Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb

lines 71–75why, one step back
71 assert response.status_code == 200
72 assert response.encoding == "iso-8859-1"73 assert response.text == "Hello, world!"7475

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 76why, one step back
76def test_response_default_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 93–94why, one step back
93
94

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

line 95why, one step back
95def test_response_set_explicit_encoding():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 96–98why, one step back
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(

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 100–101why, one step back
100 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers
101 )

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 102why, one step back
102 response.encoding = "latin-1"

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 103–106why, one step back
103 assert response.text == "Latin 1: ÿ"
104 assert response.encoding == "latin-1"105106

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 107why, one step back
107def 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"))

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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!")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 118–119why, one step back
118
119 assert response.status_code == 200

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 120–121why, one step back
120 assert response.text == "Hello, world!"
121 assert response.encoding == "ascii"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 122–123why, one step back
122 assert response.is_closed
123

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 125why, one step back
125

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 128why, one step back
128 assert response.is_closed

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 129–130why, one step back
129
130

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!")

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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!")

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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!")

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 174–179why, 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!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 180–181why, one step back
180
181

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())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 189why, one step back
189

Response.contentTom Christiebefore adoption4c55113

lines 190–191why, one step back
190 assert content == b"Hello, world!"
191 assert response.content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 192–195why, one step back
192 assert response.is_closed
193194195@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())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 211why, one step back
211

Response.contentTom Christiebefore adoption4c55113

line 212why, one step back
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""

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 227why, one step back
227

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 255why, one step back
255 assert response.text == ""

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

lines 256–261why, one step back
256
257258def test_json_with_specified_encoding():259 data = dict(greeting="hello", recipient="world")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–269why, one step back
263 assert response.json() == data
264265266def test_json_with_options():267 data = dict(greeting="hello", recipient="world", amount=1)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–277why, one step back
271 assert response.json(parse_int=str)["amount"] == "1"
272273274def test_json_without_specified_encoding():275 data = dict(greeting="hello", recipient="world")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–286why, one step back
279 assert response.json() == data
280281282def test_json_without_specified_encoding_decode_error():283 data = dict(greeting="hello", recipient="world")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()