Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

834 lines · 22.2 KiB · Python

Last changed by

Transport API (#1522)Tom Christiebefore adoption1a6e254

History of this file

Take it

The walk stopped after 200 commits: lines older than 27b0dbc point at it.

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 4why, one step back
4import brotli

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 5–9why, one step back
5import pytest
67import httpx89

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 10–15why, one step back
10class StreamingBody:
11 def __iter__(self):12 yield b"Hello, "13 yield b"world!"1415

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 16–25why, one step back
16def streaming_body():
17 yield b"Hello, "18 yield b"world!"192021async def async_streaming_body():22 yield b"Hello, "23 yield b"world!"2425

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 27–31why, one step back
27 response = httpx.Response(
28 200,29 content=b"Hello, world!",30 request=httpx.Request("GET", "https://example.org"),31 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 32–35why, one step back
32
33 assert response.status_code == 20034 assert response.reason_phrase == "OK"35 assert response.text == "Hello, world!"

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 36–37why, one step back
36 assert response.request.method == "GET"
37 assert response.request.url == "https://example.org"

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 38–40why, one step back
38 assert not response.is_error
3940

Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd

lines 41–46why, one step back
41def test_response_content():
42 response = httpx.Response(200, content="Hello, world!")4344 assert response.status_code == 20045 assert response.reason_phrase == "OK"46 assert response.text == "Hello, world!"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 47why, one step back
47 assert response.headers == {"Content-Length": "13"}

Fix automatic .read() when Response instances are created with content=<str> (#1324)Tom Christiebefore adoption666cbbd

lines 48–49why, one step back
48
49

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 50–55why, one step back
50def test_response_text():
51 response = httpx.Response(200, text="Hello, world!")5253 assert response.status_code == 20054 assert response.reason_phrase == "OK"55 assert response.text == "Hello, world!"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 56–59why, one step back
56 assert response.headers == {
57 "Content-Length": "13",58 "Content-Type": "text/plain; charset=utf-8",59 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 60–67why, one step back
60
6162def test_response_html():63 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")6465 assert response.status_code == 20066 assert response.reason_phrase == "OK"67 assert response.text == "<html><body>Hello, world!</html></body>"

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 68–71why, one step back
68 assert response.headers == {
69 "Content-Length": "39",70 "Content-Type": "text/html; charset=utf-8",71 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 72–79why, one step back
72
7374def test_response_json():75 response = httpx.Response(200, json={"hello": "world"})7677 assert response.status_code == 20078 assert response.reason_phrase == "OK"79 assert response.json() == {"hello": "world"}

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 80–83why, one step back
80 assert response.headers == {
81 "Content-Length": "18",82 "Content-Type": "application/json",83 }

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

lines 84–85why, one step back
84
85

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 86why, one step back
86def test_raise_for_status():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 87–88why, one step back
87 request = httpx.Request("GET", "https://example.org")
88

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 89why, one step back
89 # 2xx status codes are not an error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

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

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 91–93why, one step back
91 response.raise_for_status()
9293 # 4xx status codes are a client error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 94why, one step back
94 response = httpx.Response(403, request=request)

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 95–98why, one step back
95 with pytest.raises(httpx.HTTPStatusError):
96 response.raise_for_status()9798 # 5xx status codes are a server error.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

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

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 100–109why, one step back
100 with pytest.raises(httpx.HTTPStatusError):
101 response.raise_for_status()102103 # Calling .raise_for_status without setting a request instance is104 # not valid. Should raise a runtime error.105 response = httpx.Response(200)106 with pytest.raises(RuntimeError):107 response.raise_for_status()108109

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 111–114why, one step back
111 response = httpx.Response(
112 200,113 content=b"Hello, world!",114 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 115–123why, one step back
115 assert repr(response) == "<Response [200 OK]>"
116117118def test_response_content_type_encoding():119 """120 Use the charset encoding in the Content-Type header if possible.121 """122 headers = {"Content-Type": "text-plain; charset=latin-1"}123 content = "Latin 1: ÿ".encode("latin-1")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 124–128why, one step back
124 response = httpx.Response(
125 200,126 content=content,127 headers=headers,128 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 129–134why, one step back
129 assert response.text == "Latin 1: ÿ"
130 assert response.encoding == "latin-1"131132133def test_response_autodetect_encoding():134 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 135why, one step back
135 Autodetect encoding if there is no Content-Type header.

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 136why, one step back
136 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 137why, one step back
137 content = "おはようございます。".encode("utf-8")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 138–141why, one step back
138 response = httpx.Response(
139 200,140 content=content,141 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 142why, one step back
142 assert response.text == "おはようございます。"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 143why, one step back
143 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 144–150why, one step back
144
145146def test_response_fallback_to_autodetect():147 """148 Fallback to autodetection if we get an invalid charset in the Content-Type header.149 """150 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 151why, one step back
151 content = "おはようございます。".encode("utf-8")

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 152–156why, one step back
152 response = httpx.Response(
153 200,154 content=content,155 headers=headers,156 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 157why, one step back
157 assert response.text == "おはようございます。"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 158why, one step back
158 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 159–160why, one step back
159
160

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 161why, one step back
161def test_response_no_charset_with_ascii_content():

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 162why, one step back
162 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 163–164why, one step back
163 A response with ascii encoded content should decode correctly,
164 even with no charset specified.

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 165–167why, one step back
165 """
166 content = b"Hello, world!"167 headers = {"Content-Type": "text/plain"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 168–172why, one step back
168 response = httpx.Response(
169 200,170 content=content,171 headers=headers,172 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 173why, one step back
173 assert response.status_code == 200

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 174why, one step back
174 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 175–177why, one step back
175 assert response.text == "Hello, world!"
176177

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 178why, one step back
178def test_response_no_charset_with_utf8_content():

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 179why, one step back
179 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 180–181why, one step back
180 A response with UTF-8 encoded content should decode correctly,
181 even with no charset specified.

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 182why, one step back
182 """

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 183–184why, one step back
183 content = "Unicode Snowman: ☃".encode("utf-8")
184 headers = {"Content-Type": "text/plain"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 185–186why, one step back
185 response = httpx.Response(
186 200,

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 187–188why, one step back
187 content=content,
188 headers=headers,

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 189why, one step back
189 )

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 190–191why, one step back
190 assert response.text == "Unicode Snowman: ☃"
191 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 192–193why, one step back
192
193

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 194–225why, one step back
194def test_response_no_charset_with_iso_8859_1_content():
195 """196 A response with ISO 8859-1 encoded content should decode correctly,197 even with no charset specified.198 """199 content = "Accented: Österreich".encode("iso-8859-1")200 headers = {"Content-Type": "text/plain"}201 response = httpx.Response(202 200,203 content=content,204 headers=headers,205 )206 assert response.text == "Accented: Österreich"207 assert response.encoding is None208209210def test_response_no_charset_with_cp_1252_content():211 """212 A response with Windows 1252 encoded content should decode correctly,213 even with no charset specified.214 """215 content = "Euro Currency: €".encode("cp1252")216 headers = {"Content-Type": "text/plain"}217 response = httpx.Response(218 200,219 content=content,220 headers=headers,221 )222 assert response.text == "Euro Currency: €"223 assert response.encoding is None224225

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 226–230why, one step back
226def test_response_non_text_encoding():
227 """228 Default to apparent encoding for non-text content-type headers.229 """230 headers = {"Content-Type": "image/png"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 231–235why, one step back
231 response = httpx.Response(
232 200,233 content=b"xyz",234 headers=headers,235 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 237why, one step back
237 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 238–244why, one step back
238
239240def test_response_set_explicit_encoding():241 headers = {242 "Content-Type": "text-plain; charset=utf-8"243 } # Deliberately incorrect charset244 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 248–255why, one step back
248 )
249 response.encoding = "latin-1"250 assert response.text == "Latin 1: ÿ"251 assert response.encoding == "latin-1"252253254def test_response_force_encoding():255 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 256–257why, one step back
256 200,
257 content="Snowman: ☃".encode("utf-8"),

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 258–266why, one step back
258 )
259 response.encoding = "iso-8859-1"260 assert response.status_code == 200261 assert response.reason_phrase == "OK"262 assert response.text == "Snowman: â\x98\x83"263 assert response.encoding == "iso-8859-1"264265266def test_read():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 267–270why, one step back
267 response = httpx.Response(
268 200,269 content=b"Hello, world!",270 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 271–273why, one step back
271
272 assert response.status_code == 200273 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 274why, one step back
274 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 275–283why, one step back
275 assert response.is_closed
276277 content = response.read()278279 assert content == b"Hello, world!"280 assert response.content == b"Hello, world!"281 assert response.is_closed282283

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 284–298why, one step back
284def test_empty_read():
285 response = httpx.Response(200)286287 assert response.status_code == 200288 assert response.text == ""289 assert response.encoding is None290 assert response.is_closed291292 content = response.read()293294 assert content == b""295 assert response.content == b""296 assert response.is_closed297298

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 299–300why, one step back
299@pytest.mark.asyncio
300async def test_aread():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 301–304why, one step back
301 response = httpx.Response(
302 200,303 content=b"Hello, world!",304 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 305–307why, one step back
305
306 assert response.status_code == 200307 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

line 308why, one step back
308 assert response.encoding is None

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 309–317why, one step back
309 assert response.is_closed
310311 content = await response.aread()312313 assert content == b"Hello, world!"314 assert response.content == b"Hello, world!"315 assert response.is_closed316317

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 318–333why, one step back
318@pytest.mark.asyncio
319async def test_empty_aread():320 response = httpx.Response(200)321322 assert response.status_code == 200323 assert response.text == ""324 assert response.encoding is None325 assert response.is_closed326327 content = await response.aread()328329 assert content == b""330 assert response.content == b""331 assert response.is_closed332333

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 334why, one step back
334def test_iter_raw():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 335–336why, one step back
335 response = httpx.Response(
336 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 337why, one step back
337 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 338why, one step back
338 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 339–345why, one step back
339
340 raw = b""341 for part in response.iter_raw():342 raw += part343 assert raw == b"Hello, world!"344345

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 346–362why, one step back
346def test_iter_raw_with_chunksize():
347 response = httpx.Response(200, content=streaming_body())348349 parts = [part for part in response.iter_raw(chunk_size=5)]350 assert parts == [b"Hello", b", wor", b"ld!"]351352 response = httpx.Response(200, content=streaming_body())353354 parts = [part for part in response.iter_raw(chunk_size=13)]355 assert parts == [b"Hello, world!"]356357 response = httpx.Response(200, content=streaming_body())358359 parts = [part for part in response.iter_raw(chunk_size=20)]360 assert parts == [b"Hello, world!"]361362

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 363–384why, one step back
363def test_iter_raw_on_iterable():
364 response = httpx.Response(365 200,366 content=StreamingBody(),367 )368369 raw = b""370 for part in response.iter_raw():371 raw += part372 assert raw == b"Hello, world!"373374375def test_iter_raw_on_async():376 response = httpx.Response(377 200,378 content=async_streaming_body(),379 )380381 with pytest.raises(RuntimeError):382 [part for part in response.iter_raw()]383384

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

line 385why, one step back
385def test_iter_raw_increments_updates_counter():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 387–393why, one step back
387
388 num_downloaded = response.num_bytes_downloaded389 for part in response.iter_raw():390 assert len(part) == (response.num_bytes_downloaded - num_downloaded)391 num_downloaded = response.num_bytes_downloaded392393

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 394–395why, one step back
394@pytest.mark.asyncio
395async def test_aiter_raw():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 397–401why, one step back
397
398 raw = b""399 async for part in response.aiter_raw():400 raw += part401 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 402–404why, one step back
402
403404@pytest.mark.asyncio

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 405–422why, one step back
405async def test_aiter_raw_with_chunksize():
406 response = httpx.Response(200, content=async_streaming_body())407408 parts = [part async for part in response.aiter_raw(chunk_size=5)]409 assert parts == [b"Hello", b", wor", b"ld!"]410411 response = httpx.Response(200, content=async_streaming_body())412413 parts = [part async for part in response.aiter_raw(chunk_size=13)]414 assert parts == [b"Hello, world!"]415416 response = httpx.Response(200, content=async_streaming_body())417418 parts = [part async for part in response.aiter_raw(chunk_size=20)]419 assert parts == [b"Hello, world!"]420421422@pytest.mark.asyncio

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 423–433why, one step back
423async def test_aiter_raw_on_sync():
424 response = httpx.Response(425 200,426 content=streaming_body(),427 )428429 with pytest.raises(RuntimeError):430 [part async for part in response.aiter_raw()]431432433@pytest.mark.asyncio

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

line 434why, one step back
434async def test_aiter_raw_increments_updates_counter():

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 436–440why, one step back
436
437 num_downloaded = response.num_bytes_downloaded438 async for part in response.aiter_raw():439 assert len(part) == (response.num_bytes_downloaded - num_downloaded)440 num_downloaded = response.num_bytes_downloaded

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 441–443why, one step back
441
442443def test_iter_bytes():

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 445–451why, one step back
445
446 content = b""447 for part in response.iter_bytes():448 content += part449 assert content == b"Hello, world!"450451

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 452–465why, one step back
452def test_iter_bytes_with_chunk_size():
453 response = httpx.Response(200, content=streaming_body())454 parts = [part for part in response.iter_bytes(chunk_size=5)]455 assert parts == [b"Hello", b", wor", b"ld!"]456457 response = httpx.Response(200, content=streaming_body())458 parts = [part for part in response.iter_bytes(chunk_size=13)]459 assert parts == [b"Hello, world!"]460461 response = httpx.Response(200, content=streaming_body())462 parts = [part for part in response.iter_bytes(chunk_size=20)]463 assert parts == [b"Hello, world!"]464465

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 466–467why, one step back
466@pytest.mark.asyncio
467async def test_aiter_bytes():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 468–471why, one step back
468 response = httpx.Response(
469 200,470 content=b"Hello, world!",471 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 472–478why, one step back
472
473 content = b""474 async for part in response.aiter_bytes():475 content += part476 assert content == b"Hello, world!"477478

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 479–493why, one step back
479@pytest.mark.asyncio
480async def test_aiter_bytes_with_chunk_size():481 response = httpx.Response(200, content=async_streaming_body())482 parts = [part async for part in response.aiter_bytes(chunk_size=5)]483 assert parts == [b"Hello", b", wor", b"ld!"]484485 response = httpx.Response(200, content=async_streaming_body())486 parts = [part async for part in response.aiter_bytes(chunk_size=13)]487 assert parts == [b"Hello, world!"]488489 response = httpx.Response(200, content=async_streaming_body())490 parts = [part async for part in response.aiter_bytes(chunk_size=20)]491 assert parts == [b"Hello, world!"]492493

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 494why, one step back
494def test_iter_text():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 495–498why, one step back
495 response = httpx.Response(
496 200,497 content=b"Hello, world!",498 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 499–505why, one step back
499
500 content = ""501 for part in response.iter_text():502 content += part503 assert content == "Hello, world!"504505

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 506–519why, one step back
506def test_iter_text_with_chunk_size():
507 response = httpx.Response(200, content=b"Hello, world!")508 parts = [part for part in response.iter_text(chunk_size=5)]509 assert parts == ["Hello", ", wor", "ld!"]510511 response = httpx.Response(200, content=b"Hello, world!")512 parts = [part for part in response.iter_text(chunk_size=13)]513 assert parts == ["Hello, world!"]514515 response = httpx.Response(200, content=b"Hello, world!")516 parts = [part for part in response.iter_text(chunk_size=20)]517 assert parts == ["Hello, world!"]518519

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 520–521why, one step back
520@pytest.mark.asyncio
521async def test_aiter_text():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 522–525why, one step back
522 response = httpx.Response(
523 200,524 content=b"Hello, world!",525 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 526–530why, one step back
526
527 content = ""528 async for part in response.aiter_text():529 content += part530 assert content == "Hello, world!"

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 531–545why, one step back
531
532533@pytest.mark.asyncio534async def test_aiter_text_with_chunk_size():535 response = httpx.Response(200, content=b"Hello, world!")536 parts = [part async for part in response.aiter_text(chunk_size=5)]537 assert parts == ["Hello", ", wor", "ld!"]538539 response = httpx.Response(200, content=b"Hello, world!")540 parts = [part async for part in response.aiter_text(chunk_size=13)]541 assert parts == ["Hello, world!"]542543 response = httpx.Response(200, content=b"Hello, world!")544 parts = [part async for part in response.aiter_text(chunk_size=20)]545 assert parts == ["Hello, world!"]

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 546–548why, one step back
546
547548def test_iter_lines():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 549–552why, one step back
549 response = httpx.Response(
550 200,551 content=b"Hello,\nworld!",552 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 553–561why, one step back
553
554 content = []555 for line in response.iter_lines():556 content.append(line)557 assert content == ["Hello,\n", "world!"]558559560@pytest.mark.asyncio561async def test_aiter_lines():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 562–565why, one step back
562 response = httpx.Response(
563 200,564 content=b"Hello,\nworld!",565 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 566–573why, one step back
566
567 content = []568 async for line in response.aiter_lines():569 content.append(line)570 assert content == ["Hello,\n", "world!"]571572573def test_sync_streaming_response():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 574–575why, one step back
574 response = httpx.Response(
575 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 576why, one step back
576 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 577why, one step back
577 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 578–590why, one step back
578
579 assert response.status_code == 200580 assert not response.is_closed581582 content = response.read()583584 assert content == b"Hello, world!"585 assert response.content == b"Hello, world!"586 assert response.is_closed587588589@pytest.mark.asyncio590async def test_async_streaming_response():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 591–592why, one step back
591 response = httpx.Response(
592 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 593why, one step back
593 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 594why, one step back
594 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 595–606why, one step back
595
596 assert response.status_code == 200597 assert not response.is_closed598599 content = await response.aread()600601 assert content == b"Hello, world!"602 assert response.content == b"Hello, world!"603 assert response.is_closed604605606def test_cannot_read_after_stream_consumed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 607–608why, one step back
607 response = httpx.Response(
608 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 609why, one step back
609 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 610why, one step back
610 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 611–621why, one step back
611
612 content = b""613 for part in response.iter_bytes():614 content += part615616 with pytest.raises(httpx.StreamConsumed):617 response.read()618619620@pytest.mark.asyncio621async def test_cannot_aread_after_stream_consumed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 622–623why, one step back
622 response = httpx.Response(
623 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 624why, one step back
624 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 625why, one step back
625 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 626–635why, one step back
626
627 content = b""628 async for part in response.aiter_bytes():629 content += part630631 with pytest.raises(httpx.StreamConsumed):632 await response.aread()633634635def test_cannot_read_after_response_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 636–637why, one step back
636 response = httpx.Response(
637 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 638why, one step back
638 content=streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 639why, one step back
639 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 640–647why, one step back
640
641 response.close()642 with pytest.raises(httpx.ResponseClosed):643 response.read()644645646@pytest.mark.asyncio647async def test_cannot_aread_after_response_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 648–649why, one step back
648 response = httpx.Response(
649 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 650why, one step back
650 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 651why, one step back
651 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 652–659why, one step back
652
653 await response.aclose()654 with pytest.raises(httpx.ResponseClosed):655 await response.aread()656657658@pytest.mark.asyncio659async def test_elapsed_not_available_until_closed():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 660–661why, one step back
660 response = httpx.Response(
661 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 662why, one step back
662 content=async_streaming_body(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 663why, one step back
663 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 664–669why, one step back
664
665 with pytest.raises(RuntimeError):666 response.elapsed667668669def test_unknown_status_code():

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 670–672why, one step back
670 response = httpx.Response(
671 600,672 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 673–681why, one step back
673 assert response.status_code == 600
674 assert response.reason_phrase == ""675 assert response.text == ""676677678def test_json_with_specified_encoding():679 data = {"greeting": "hello", "recipient": "world"}680 content = json.dumps(data).encode("utf-16")681 headers = {"Content-Type": "application/json, charset=utf-16"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 682–686why, one step back
682 response = httpx.Response(
683 200,684 content=content,685 headers=headers,686 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 687–693why, one step back
687 assert response.json() == data
688689690def test_json_with_options():691 data = {"greeting": "hello", "recipient": "world", "amount": 1}692 content = json.dumps(data).encode("utf-16")693 headers = {"Content-Type": "application/json, charset=utf-16"}

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 694–698why, one step back
694 response = httpx.Response(
695 200,696 content=content,697 headers=headers,698 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 706–710why, one step back
706 response = httpx.Response(
707 200,708 content=content,709 headers=headers,710 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 711–720why, one step back
711 assert response.json() == data
712713714def test_json_without_specified_encoding_decode_error():715 data = {"greeting": "hello", "recipient": "world"}716 content = json.dumps(data).encode("utf-32-be")717 headers = {"Content-Type": "application/json"}718 # force incorrect guess from `guess_json_utf` to trigger error719 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):720 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 721–723why, one step back
721 200,
722 content=content,723 headers=headers,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 724why, one step back
724 )

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 725why, one step back
725 with pytest.raises(json.decoder.JSONDecodeError):

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 726–728why, one step back
726 response.json()
727728

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 729–735why, one step back
729def test_json_without_specified_encoding_value_error():
730 data = {"greeting": "hello", "recipient": "world"}731 content = json.dumps(data).encode("utf-32-be")732 headers = {"Content-Type": "application/json"}733 # force incorrect guess from `guess_json_utf` to trigger error734 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):735 response = httpx.Response(200, content=content, headers=headers)

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 736why, one step back
736 with pytest.raises(json.decoder.JSONDecodeError):

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 737–739why, one step back
737 response.json()
738739

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 740–756why, one step back
740@pytest.mark.parametrize(
741 "headers, expected",742 [743 (744 {"Link": "<https://example.com>; rel='preload'"},745 {"preload": {"rel": "preload", "url": "https://example.com"}},746 ),747 (748 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},749 {750 "hub": {"url": "/hub", "rel": "hub"},751 "self": {"url": "/resource", "rel": "self"},752 },753 ),754 ],755)756def test_link_headers(headers, expected):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 757–761why, one step back
757 response = httpx.Response(
758 200,759 content=None,760 headers=headers,761 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 763–769why, one step back
763
764765@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))766def test_decode_error_with_request(header_value):767 headers = [(b"Content-Encoding", header_value)]768 body = b"test 123"769 compressed_body = brotli.compress(body)[3:]

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 770why, one step back
770 with pytest.raises(httpx.DecodingError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 771–776why, one step back
771 httpx.Response(
772 200,773 headers=headers,774 content=compressed_body,775 )776

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 777why, one step back
777 with pytest.raises(httpx.DecodingError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 778–783why, one step back
778 httpx.Response(
779 200,780 headers=headers,781 content=compressed_body,782 request=httpx.Request("GET", "https://www.example.org/"),783 )

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 784–790why, one step back
784
785786@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))787def test_value_error_without_request(header_value):788 headers = [(b"Content-Encoding", header_value)]789 body = b"test 123"790 compressed_body = brotli.compress(body)[3:]

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 791why, one step back
791 with pytest.raises(httpx.DecodingError):

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 792–806why, one step back
792 httpx.Response(200, headers=headers, content=compressed_body)
793794795def test_response_with_unset_request():796 response = httpx.Response(200, content=b"Hello, world!")797798 assert response.status_code == 200799 assert response.reason_phrase == "OK"800 assert response.text == "Hello, world!"801 assert not response.is_error802803804def test_set_request_after_init():805 response = httpx.Response(200, content=b"Hello, world!")806

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 807why, one step back
807 response.request = httpx.Request("GET", "https://www.example.org")

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 808why, one step back
808

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 809–810why, one step back
809 assert response.request.method == "GET"
810 assert response.request.url == "https://www.example.org"

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 811–816why, one step back
811
812813def test_cannot_access_unset_request():814 response = httpx.Response(200, content=b"Hello, world!")815816 with pytest.raises(RuntimeError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 817why, one step back
817 response.request

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 818–824why, one step back
818
819820def test_generator_with_transfer_encoding_header():821 def content():822 yield b"test 123" # pragma: nocover823824 response = httpx.Response(200, content=content())

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 825why, one step back
825 assert response.headers == {"Transfer-Encoding": "chunked"}

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 826–833why, one step back
826
827828def test_generator_with_content_length_header():829 def content():830 yield b"test 123" # pragma: nocover831832 headers = {"Content-Length": "8"}833 response = httpx.Response(200, content=content(), headers=headers)

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 834why, one step back
834 assert response.headers == {"Content-Length": "8"}