Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

910 lines · 24.2 KiB · Python

Last changed by

Fix iter_bytes with empty content (#1827)Tom Christiebefore adoption10b60d4

History of this file

Take it

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

line 1why, one step back
1import json

Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104

line 2why, one step back
2import pickle

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 3–6why, one step back
3
4import pytest56import httpx

Use either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312

line 7why, one step back
7from httpx._compat import brotli

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 8–9why, one step back
8
9

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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"

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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.

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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.

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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.

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 192–193why, one step back
192
193

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 194–198why, 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 """

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

line 199why, one step back
199 content = "Accented: Österreich abcdefghijklmnopqrstuzwxyz".encode("iso-8859-1")

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 200–205why, one step back
200 headers = {"Content-Type": "text/plain"}
201 response = httpx.Response(202 200,203 content=content,204 headers=headers,205 )

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

lines 206–208why, one step back
206 assert response.text == "Accented: Österreich abcdefghijklmnopqrstuzwxyz"
207 assert response.charset_encoding is None208 assert response.apparent_encoding is not None

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 209–215why, one step back
209
210211def test_response_no_charset_with_cp_1252_content():212 """213 A response with Windows 1252 encoded content should decode correctly,214 even with no charset specified.215 """

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

line 216why, one step back
216 content = "Euro Currency: € abcdefghijklmnopqrstuzwxyz".encode("cp1252")

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 217–222why, one step back
217 headers = {"Content-Type": "text/plain"}
218 response = httpx.Response(219 200,220 content=content,221 headers=headers,222 )

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

lines 223–225why, one step back
223 assert response.text == "Euro Currency: € abcdefghijklmnopqrstuzwxyz"
224 assert response.charset_encoding is None225 assert response.apparent_encoding is not None

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 226–227why, one step back
226
227

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 240–246why, one step back
240
241242def test_response_set_explicit_encoding():243 headers = {244 "Content-Type": "text-plain; charset=utf-8"245 } # Deliberately incorrect charset246 response = httpx.Response(

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 250–257why, one step back
250 )
251 response.encoding = "latin-1"252 assert response.text == "Latin 1: ÿ"253 assert response.encoding == "latin-1"254255256def test_response_force_encoding():257 response = httpx.Response(

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 260–268why, one step back
260 )
261 response.encoding = "iso-8859-1"262 assert response.status_code == 200263 assert response.reason_phrase == "OK"264 assert response.text == "Snowman: â\x98\x83"265 assert response.encoding == "iso-8859-1"266267268def test_read():

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 273–275why, one step back
273
274 assert response.status_code == 200275 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 277–285why, one step back
277 assert response.is_closed
278279 content = response.read()280281 assert content == b"Hello, world!"282 assert response.content == b"Hello, world!"283 assert response.is_closed284285

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 286–300why, one step back
286def test_empty_read():
287 response = httpx.Response(200)288289 assert response.status_code == 200290 assert response.text == ""291 assert response.encoding is None292 assert response.is_closed293294 content = response.read()295296 assert content == b""297 assert response.content == b""298 assert response.is_closed299300

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 307–309why, one step back
307
308 assert response.status_code == 200309 assert response.text == "Hello, world!"

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 311–319why, one step back
311 assert response.is_closed
312313 content = await response.aread()314315 assert content == b"Hello, world!"316 assert response.content == b"Hello, world!"317 assert response.is_closed318319

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 320–335why, one step back
320@pytest.mark.asyncio
321async def test_empty_aread():322 response = httpx.Response(200)323324 assert response.status_code == 200325 assert response.text == ""326 assert response.encoding is None327 assert response.is_closed328329 content = await response.aread()330331 assert content == b""332 assert response.content == b""333 assert response.is_closed334335

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

lines 337–338why, one step back
337 response = httpx.Response(
338 200,

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

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

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

line 340why, one step back
340 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 341–347why, one step back
341
342 raw = b""343 for part in response.iter_raw():344 raw += part345 assert raw == b"Hello, world!"346347

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

lines 348–364why, one step back
348def test_iter_raw_with_chunksize():
349 response = httpx.Response(200, content=streaming_body())350351 parts = [part for part in response.iter_raw(chunk_size=5)]352 assert parts == [b"Hello", b", wor", b"ld!"]353354 response = httpx.Response(200, content=streaming_body())355356 parts = [part for part in response.iter_raw(chunk_size=13)]357 assert parts == [b"Hello, world!"]358359 response = httpx.Response(200, content=streaming_body())360361 parts = [part for part in response.iter_raw(chunk_size=20)]362 assert parts == [b"Hello, world!"]363364

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 365–386why, one step back
365def test_iter_raw_on_iterable():
366 response = httpx.Response(367 200,368 content=StreamingBody(),369 )370371 raw = b""372 for part in response.iter_raw():373 raw += part374 assert raw == b"Hello, world!"375376377def test_iter_raw_on_async():378 response = httpx.Response(379 200,380 content=async_streaming_body(),381 )382383 with pytest.raises(RuntimeError):384 [part for part in response.iter_raw()]385386

Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328

lines 387–396why, one step back
387def test_close_on_async():
388 response = httpx.Response(389 200,390 content=async_streaming_body(),391 )392393 with pytest.raises(RuntimeError):394 response.close()395396

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 399–405why, one step back
399
400 num_downloaded = response.num_bytes_downloaded401 for part in response.iter_raw():402 assert len(part) == (response.num_bytes_downloaded - num_downloaded)403 num_downloaded = response.num_bytes_downloaded404405

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 409–413why, one step back
409
410 raw = b""411 async for part in response.aiter_raw():412 raw += part413 assert raw == b"Hello, world!"

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 414–416why, one step back
414
415416@pytest.mark.asyncio

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

lines 417–434why, one step back
417async def test_aiter_raw_with_chunksize():
418 response = httpx.Response(200, content=async_streaming_body())419420 parts = [part async for part in response.aiter_raw(chunk_size=5)]421 assert parts == [b"Hello", b", wor", b"ld!"]422423 response = httpx.Response(200, content=async_streaming_body())424425 parts = [part async for part in response.aiter_raw(chunk_size=13)]426 assert parts == [b"Hello, world!"]427428 response = httpx.Response(200, content=async_streaming_body())429430 parts = [part async for part in response.aiter_raw(chunk_size=20)]431 assert parts == [b"Hello, world!"]432433434@pytest.mark.asyncio

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 435–442why, one step back
435async def test_aiter_raw_on_sync():
436 response = httpx.Response(437 200,438 content=streaming_body(),439 )440441 with pytest.raises(RuntimeError):442 [part async for part in response.aiter_raw()]

Drop 'Response(on_close=...)' from API (#1572)Tom Christiebefore adoption073a328

lines 443–453why, one step back
443
444445@pytest.mark.asyncio446async def test_aclose_on_sync():447 response = httpx.Response(448 200,449 content=streaming_body(),450 )451452 with pytest.raises(RuntimeError):453 await response.aclose()

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 454–456why, one step back
454
455456@pytest.mark.asyncio

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

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

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

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

Add progress to streaming download (#1268)cdelerbefore adoptioned16eb3

lines 459–463why, one step back
459
460 num_downloaded = response.num_bytes_downloaded461 async for part in response.aiter_raw():462 assert len(part) == (response.num_bytes_downloaded - num_downloaded)463 num_downloaded = response.num_bytes_downloaded

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 464–466why, one step back
464
465466def test_iter_bytes():

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 468–474why, one step back
468
469 content = b""470 for part in response.iter_bytes():471 content += part472 assert content == b"Hello, world!"473474

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

lines 475–488why, one step back
475def test_iter_bytes_with_chunk_size():
476 response = httpx.Response(200, content=streaming_body())477 parts = [part for part in response.iter_bytes(chunk_size=5)]478 assert parts == [b"Hello", b", wor", b"ld!"]479480 response = httpx.Response(200, content=streaming_body())481 parts = [part for part in response.iter_bytes(chunk_size=13)]482 assert parts == [b"Hello, world!"]483484 response = httpx.Response(200, content=streaming_body())485 parts = [part for part in response.iter_bytes(chunk_size=20)]486 assert parts == [b"Hello, world!"]487488

Fix iter_bytes with empty content (#1827)Tom Christiebefore adoption10b60d4

lines 489–494why, one step back
489def test_iter_bytes_with_empty_response():
490 response = httpx.Response(200, content=b"")491 parts = [part for part in response.iter_bytes()]492 assert parts == []493494

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 501–507why, one step back
501
502 content = b""503 async for part in response.aiter_bytes():504 content += part505 assert content == b"Hello, world!"506507

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

lines 508–522why, one step back
508@pytest.mark.asyncio
509async def test_aiter_bytes_with_chunk_size():510 response = httpx.Response(200, content=async_streaming_body())511 parts = [part async for part in response.aiter_bytes(chunk_size=5)]512 assert parts == [b"Hello", b", wor", b"ld!"]513514 response = httpx.Response(200, content=async_streaming_body())515 parts = [part async for part in response.aiter_bytes(chunk_size=13)]516 assert parts == [b"Hello, world!"]517518 response = httpx.Response(200, content=async_streaming_body())519 parts = [part async for part in response.aiter_bytes(chunk_size=20)]520 assert parts == [b"Hello, world!"]521522

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 528–534why, one step back
528
529 content = ""530 for part in response.iter_text():531 content += part532 assert content == "Hello, world!"533534

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

lines 535–548why, one step back
535def test_iter_text_with_chunk_size():
536 response = httpx.Response(200, content=b"Hello, world!")537 parts = [part for part in response.iter_text(chunk_size=5)]538 assert parts == ["Hello", ", wor", "ld!"]539540 response = httpx.Response(200, content=b"Hello, world!")541 parts = [part for part in response.iter_text(chunk_size=13)]542 assert parts == ["Hello, world!"]543544 response = httpx.Response(200, content=b"Hello, world!")545 parts = [part for part in response.iter_text(chunk_size=20)]546 assert parts == ["Hello, world!"]547548

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

lines 551–554why, one step back
551 response = httpx.Response(
552 200,553 content=b"Hello, world!",554 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 555–559why, one step back
555
556 content = ""557 async for part in response.aiter_text():558 content += part559 assert content == "Hello, world!"

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

lines 560–574why, one step back
560
561562@pytest.mark.asyncio563async def test_aiter_text_with_chunk_size():564 response = httpx.Response(200, content=b"Hello, world!")565 parts = [part async for part in response.aiter_text(chunk_size=5)]566 assert parts == ["Hello", ", wor", "ld!"]567568 response = httpx.Response(200, content=b"Hello, world!")569 parts = [part async for part in response.aiter_text(chunk_size=13)]570 assert parts == ["Hello, world!"]571572 response = httpx.Response(200, content=b"Hello, world!")573 parts = [part async for part in response.aiter_text(chunk_size=20)]574 assert parts == ["Hello, world!"]

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 575–577why, one step back
575
576577def test_iter_lines():

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

lines 578–581why, one step back
578 response = httpx.Response(
579 200,580 content=b"Hello,\nworld!",581 )

Replace for loops with comprehensions (#1759)Almazbefore adoptionb839478

line 582why, one step back
582 content = [line for line in response.iter_lines()]

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 583–587why, one step back
583 assert content == ["Hello,\n", "world!"]
584585586@pytest.mark.asyncio587async def test_aiter_lines():

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

lines 588–591why, one step back
588 response = httpx.Response(
589 200,590 content=b"Hello,\nworld!",591 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 592–599why, one step back
592
593 content = []594 async for line in response.aiter_lines():595 content.append(line)596 assert content == ["Hello,\n", "world!"]597598599def test_sync_streaming_response():

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

lines 600–601why, one step back
600 response = httpx.Response(
601 200,

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

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

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

line 603why, one step back
603 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 604–616why, one step back
604
605 assert response.status_code == 200606 assert not response.is_closed607608 content = response.read()609610 assert content == b"Hello, world!"611 assert response.content == b"Hello, world!"612 assert response.is_closed613614615@pytest.mark.asyncio616async def test_async_streaming_response():

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

lines 617–618why, one step back
617 response = httpx.Response(
618 200,

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

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

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

line 620why, one step back
620 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 621–632why, one step back
621
622 assert response.status_code == 200623 assert not response.is_closed624625 content = await response.aread()626627 assert content == b"Hello, world!"628 assert response.content == b"Hello, world!"629 assert response.is_closed630631632def test_cannot_read_after_stream_consumed():

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

lines 633–634why, one step back
633 response = httpx.Response(
634 200,

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

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

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

line 636why, one step back
636 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 637–647why, one step back
637
638 content = b""639 for part in response.iter_bytes():640 content += part641642 with pytest.raises(httpx.StreamConsumed):643 response.read()644645646@pytest.mark.asyncio647async def test_cannot_aread_after_stream_consumed():

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 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 652–661why, one step back
652
653 content = b""654 async for part in response.aiter_bytes():655 content += part656657 with pytest.raises(httpx.StreamConsumed):658 await response.aread()659660661def test_cannot_read_after_response_closed():

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

lines 662–663why, one step back
662 response = httpx.Response(
663 200,

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

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

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

line 665why, one step back
665 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 666–667why, one step back
666
667 response.close()

`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af

line 668why, one step back
668 with pytest.raises(httpx.StreamClosed):

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 669–673why, one step back
669 response.read()
670671672@pytest.mark.asyncio673async def test_cannot_aread_after_response_closed():

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

lines 674–675why, one step back
674 response = httpx.Response(
675 200,

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

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

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

line 677why, one step back
677 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 678–679why, one step back
678
679 await response.aclose()

`httpx.ResponseClosed` -> `httpx.StreamClosed` (#1584)Tom Christiebefore adoption9b8f5af

line 680why, one step back
680 with pytest.raises(httpx.StreamClosed):

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 681–685why, one step back
681 await response.aread()
682683684@pytest.mark.asyncio685async def test_elapsed_not_available_until_closed():

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

lines 686–687why, one step back
686 response = httpx.Response(
687 200,

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

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

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

line 689why, one step back
689 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 690–695why, one step back
690
691 with pytest.raises(RuntimeError):692 response.elapsed693694695def test_unknown_status_code():

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

lines 696–698why, one step back
696 response = httpx.Response(
697 600,698 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 699–707why, one step back
699 assert response.status_code == 600
700 assert response.reason_phrase == ""701 assert response.text == ""702703704def test_json_with_specified_encoding():705 data = {"greeting": "hello", "recipient": "world"}706 content = json.dumps(data).encode("utf-16")707 headers = {"Content-Type": "application/json, charset=utf-16"}

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

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

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 713–719why, one step back
713 assert response.json() == data
714715716def test_json_with_options():717 data = {"greeting": "hello", "recipient": "world", "amount": 1}718 content = json.dumps(data).encode("utf-16")719 headers = {"Content-Type": "application/json, charset=utf-16"}

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

lines 720–724why, one step back
720 response = httpx.Response(
721 200,722 content=content,723 headers=headers,724 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 725–727why, one step back
725 assert response.json(parse_int=str)["amount"] == "1"
726727

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

lines 728–741why, one step back
728@pytest.mark.parametrize(
729 "encoding",730 [731 "utf-8",732 "utf-8-sig",733 "utf-16",734 "utf-16-be",735 "utf-16-le",736 "utf-32",737 "utf-32-be",738 "utf-32-le",739 ],740)741def test_json_without_specified_charset(encoding):

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

line 743why, one step back
743 content = json.dumps(data).encode(encoding)

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

line 744why, one step back
744 headers = {"Content-Type": "application/json"}

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

lines 745–749why, one step back
745 response = httpx.Response(
746 200,747 content=content,748 headers=headers,749 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 750–752why, one step back
750 assert response.json() == data
751752

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

lines 753–766why, one step back
753@pytest.mark.parametrize(
754 "encoding",755 [756 "utf-8",757 "utf-8-sig",758 "utf-16",759 "utf-16-be",760 "utf-16-le",761 "utf-32",762 "utf-32-be",763 "utf-32-le",764 ],765)766def test_json_with_specified_charset(encoding):

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

Add `charset_normalizer` detection. (#1791)Tom Christiebefore adoptionacb5e6a

lines 768–775why, one step back
768 content = json.dumps(data).encode(encoding)
769 headers = {"Content-Type": f"application/json; charset={encoding}"}770 response = httpx.Response(771 200,772 content=content,773 headers=headers,774 )775 assert response.json() == data

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

lines 776–777why, one step back
776
777

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

lines 778–794why, one step back
778@pytest.mark.parametrize(
779 "headers, expected",780 [781 (782 {"Link": "<https://example.com>; rel='preload'"},783 {"preload": {"rel": "preload", "url": "https://example.com"}},784 ),785 (786 {"Link": '</hub>; rel="hub", </resource>; rel="self"'},787 {788 "hub": {"url": "/hub", "rel": "hub"},789 "self": {"url": "/resource", "rel": "self"},790 },791 ),792 ],793)794def test_link_headers(headers, expected):

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

lines 795–799why, one step back
795 response = httpx.Response(
796 200,797 content=None,798 headers=headers,799 )

Cleanup docstring leftover for #1183 (#1201)Joebefore adoptionbd8165a

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

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

lines 801–806why, one step back
801
802803@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))804def test_decode_error_with_request(header_value):805 headers = [(b"Content-Encoding", header_value)]806 body = b"test 123"

Use either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312

line 807why, one step back
807 compressed_body = brotli.compress(body)[3:]

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

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

lines 809–814why, one step back
809 httpx.Response(
810 200,811 headers=headers,812 content=compressed_body,813 )814

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

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

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

lines 816–821why, one step back
816 httpx.Response(
817 200,818 headers=headers,819 content=compressed_body,820 request=httpx.Request("GET", "https://www.example.org/"),821 )

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

lines 822–827why, one step back
822
823824@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))825def test_value_error_without_request(header_value):826 headers = [(b"Content-Encoding", header_value)]827 body = b"test 123"

Use either brotli or brotlicffi. (#1618)Tom Christiebefore adoptiond514312

line 828why, one step back
828 compressed_body = brotli.compress(body)[3:]

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

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

lines 830–844why, one step back
830 httpx.Response(200, headers=headers, content=compressed_body)
831832833def test_response_with_unset_request():834 response = httpx.Response(200, content=b"Hello, world!")835836 assert response.status_code == 200837 assert response.reason_phrase == "OK"838 assert response.text == "Hello, world!"839 assert not response.is_error840841842def test_set_request_after_init():843 response = httpx.Response(200, content=b"Hello, world!")844

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

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

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

line 846why, one step back
846

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

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

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

lines 849–854why, one step back
849
850851def test_cannot_access_unset_request():852 response = httpx.Response(200, content=b"Hello, world!")853854 with pytest.raises(RuntimeError):

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

line 855why, one step back
855 response.request

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 856–862why, one step back
856
857858def test_generator_with_transfer_encoding_header():859 def content():860 yield b"test 123" # pragma: nocover861862 response = httpx.Response(200, content=content())

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 864–871why, one step back
864
865866def test_generator_with_content_length_header():867 def content():868 yield b"test 123" # pragma: nocover869870 headers = {"Content-Length": "8"}871 response = httpx.Response(200, content=content(), headers=headers)

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

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

Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104

lines 873–910why, one step back
873
874875def test_response_picklable():876 response = httpx.Response(877 200,878 content=b"Hello, world!",879 request=httpx.Request("GET", "https://example.org"),880 )881 pickle_response = pickle.loads(pickle.dumps(response))882 assert pickle_response.is_closed is True883 assert pickle_response.is_stream_consumed is True884 assert pickle_response.next_request is None885 assert pickle_response.stream is not None886 assert pickle_response.content == b"Hello, world!"887 assert pickle_response.status_code == 200888 assert pickle_response.request.url == response.request.url889 assert pickle_response.extensions == {}890 assert pickle_response.history == []891892893@pytest.mark.asyncio894async def test_response_async_streaming_picklable():895 response = httpx.Response(200, content=async_streaming_body())896 pickle_response = pickle.loads(pickle.dumps(response))897 with pytest.raises(httpx.ResponseNotRead):898 pickle_response.content899 with pytest.raises(httpx.StreamClosed):900 await pickle_response.aread()901 assert pickle_response.is_stream_consumed is False902 assert pickle_response.num_bytes_downloaded == 0903 assert pickle_response.headers == {"Transfer-Encoding": "chunked"}904905 response = httpx.Response(200, content=async_streaming_body())906 await response.aread()907 pickle_response = pickle.loads(pickle.dumps(response))908 assert pickle_response.is_stream_consumed is True909 assert pickle_response.content == b"Hello, world!"910 assert pickle_response.num_bytes_downloaded == 13