Skip to content
Myr

test_content.py

This file

Path

tests/test_content.py

450 lines · 14.0 KiB · Python

Last changed by

Typing: enable warn_return_any (#2477)Martijn Pietersbefore adoption2d37321

History of this file

Take it

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

📌 Pin development requirements (#1721)Marcelo Trylesinskibefore adoption0ccc3fa

lines 1–15why, one step back
1import io
2import typing34import pytest56import httpx7from httpx._content import encode_request, encode_response8910@pytest.mark.asyncio11async def test_empty_content():12 headers, stream = encode_request()13 assert isinstance(stream, httpx.SyncByteStream)14 assert isinstance(stream, httpx.AsyncByteStream)15

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 16–17why, one step back
16 sync_content = b"".join([part for part in stream])
17 async_content = b"".join([part async for part in stream])

📌 Pin development requirements (#1721)Marcelo Trylesinskibefore adoption0ccc3fa

lines 18–63why, one step back
18
19 assert headers == {}20 assert sync_content == b""21 assert async_content == b""222324@pytest.mark.asyncio25async def test_bytes_content():26 headers, stream = encode_request(content=b"Hello, world!")27 assert isinstance(stream, typing.Iterable)28 assert isinstance(stream, typing.AsyncIterable)2930 sync_content = b"".join([part for part in stream])31 async_content = b"".join([part async for part in stream])3233 assert headers == {"Content-Length": "13"}34 assert sync_content == b"Hello, world!"35 assert async_content == b"Hello, world!"3637 # Support 'data' for compat with requests.38 with pytest.warns(DeprecationWarning):39 headers, stream = encode_request(data=b"Hello, world!") # type: ignore40 assert isinstance(stream, typing.Iterable)41 assert isinstance(stream, typing.AsyncIterable)4243 sync_content = b"".join([part for part in stream])44 async_content = b"".join([part async for part in stream])4546 assert headers == {"Content-Length": "13"}47 assert sync_content == b"Hello, world!"48 assert async_content == b"Hello, world!"495051@pytest.mark.asyncio52async def test_bytesio_content():53 headers, stream = encode_request(content=io.BytesIO(b"Hello, world!"))54 assert isinstance(stream, typing.Iterable)55 assert not isinstance(stream, typing.AsyncIterable)5657 content = b"".join([part for part in stream])5859 assert headers == {"Content-Length": "13"}60 assert content == b"Hello, world!"616263@pytest.mark.asyncio

Read upload files using `read(CHUNK_SIZE)` rather than `iter()`. (#1948)Tom Christiebefore adoption6f5865f

lines 64–65why, one step back
64async def test_async_bytesio_content():
65 class AsyncBytesIO:

Typing: enable warn_return_any (#2477)Martijn Pietersbefore adoption2d37321

line 66why, one step back
66 def __init__(self, content: bytes):

Read upload files using `read(CHUNK_SIZE)` rather than `iter()`. (#1948)Tom Christiebefore adoption6f5865f

lines 67–69why, one step back
67 self._idx = 0
68 self._content = content69

Typing: enable disallow_incomplete_defs (#2476)Martijn Pietersbefore adoption049afe5

line 70why, one step back
70 async def aread(self, chunk_size: int) -> bytes:

Read upload files using `read(CHUNK_SIZE)` rather than `iter()`. (#1948)Tom Christiebefore adoption6f5865f

lines 71–75why, one step back
71 chunk = self._content[self._idx : self._idx + chunk_size]
72 self._idx = self._idx + chunk_size73 return chunk7475 async def __aiter__(self):

use # pragma: no cover instead of # pragma: nocover (#2471)Adrian Garcia Badaraccobefore adoption16e2830

line 76why, one step back
76 yield self._content # pragma: no cover

Read upload files using `read(CHUNK_SIZE)` rather than `iter()`. (#1948)Tom Christiebefore adoption6f5865f

lines 77–88why, one step back
77
78 headers, stream = encode_request(content=AsyncBytesIO(b"Hello, world!"))79 assert not isinstance(stream, typing.Iterable)80 assert isinstance(stream, typing.AsyncIterable)8182 content = b"".join([part async for part in stream])8384 assert headers == {"Transfer-Encoding": "chunked"}85 assert content == b"Hello, world!"868788@pytest.mark.asyncio

📌 Pin development requirements (#1721)Marcelo Trylesinskibefore adoption0ccc3fa

lines 89–107why, one step back
89async def test_iterator_content():
90 def hello_world():91 yield b"Hello, "92 yield b"world!"9394 headers, stream = encode_request(content=hello_world())95 assert isinstance(stream, typing.Iterable)96 assert not isinstance(stream, typing.AsyncIterable)9798 content = b"".join([part for part in stream])99100 assert headers == {"Transfer-Encoding": "chunked"}101 assert content == b"Hello, world!"102103 with pytest.raises(httpx.StreamConsumed):104 [part for part in stream]105106 # Support 'data' for compat with requests.107 with pytest.warns(DeprecationWarning):

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 108why, one step back
108 headers, stream = encode_request(data=hello_world())

📌 Pin development requirements (#1721)Marcelo Trylesinskibefore adoption0ccc3fa

lines 109–137why, one step back
109 assert isinstance(stream, typing.Iterable)
110 assert not isinstance(stream, typing.AsyncIterable)111112 content = b"".join([part for part in stream])113114 assert headers == {"Transfer-Encoding": "chunked"}115 assert content == b"Hello, world!"116117118@pytest.mark.asyncio119async def test_aiterator_content():120 async def hello_world():121 yield b"Hello, "122 yield b"world!"123124 headers, stream = encode_request(content=hello_world())125 assert not isinstance(stream, typing.Iterable)126 assert isinstance(stream, typing.AsyncIterable)127128 content = b"".join([part async for part in stream])129130 assert headers == {"Transfer-Encoding": "chunked"}131 assert content == b"Hello, world!"132133 with pytest.raises(httpx.StreamConsumed):134 [part async for part in stream]135136 # Support 'data' for compat with requests.137 with pytest.warns(DeprecationWarning):

Remove and dissallow unused type ignores (#2470)Adrian Garcia Badaraccobefore adoption69e13cb

line 138why, one step back
138 headers, stream = encode_request(data=hello_world())

📌 Pin development requirements (#1721)Marcelo Trylesinskibefore adoption0ccc3fa

lines 139–450why, one step back
139 assert not isinstance(stream, typing.Iterable)
140 assert isinstance(stream, typing.AsyncIterable)141142 content = b"".join([part async for part in stream])143144 assert headers == {"Transfer-Encoding": "chunked"}145 assert content == b"Hello, world!"146147148@pytest.mark.asyncio149async def test_json_content():150 headers, stream = encode_request(json={"Hello": "world!"})151 assert isinstance(stream, typing.Iterable)152 assert isinstance(stream, typing.AsyncIterable)153154 sync_content = b"".join([part for part in stream])155 async_content = b"".join([part async for part in stream])156157 assert headers == {158 "Content-Length": "19",159 "Content-Type": "application/json",160 }161 assert sync_content == b'{"Hello": "world!"}'162 assert async_content == b'{"Hello": "world!"}'163164165@pytest.mark.asyncio166async def test_urlencoded_content():167 headers, stream = encode_request(data={"Hello": "world!"})168 assert isinstance(stream, typing.Iterable)169 assert isinstance(stream, typing.AsyncIterable)170171 sync_content = b"".join([part for part in stream])172 async_content = b"".join([part async for part in stream])173174 assert headers == {175 "Content-Length": "14",176 "Content-Type": "application/x-www-form-urlencoded",177 }178 assert sync_content == b"Hello=world%21"179 assert async_content == b"Hello=world%21"180181182@pytest.mark.asyncio183async def test_urlencoded_boolean():184 headers, stream = encode_request(data={"example": True})185 assert isinstance(stream, typing.Iterable)186 assert isinstance(stream, typing.AsyncIterable)187188 sync_content = b"".join([part for part in stream])189 async_content = b"".join([part async for part in stream])190191 assert headers == {192 "Content-Length": "12",193 "Content-Type": "application/x-www-form-urlencoded",194 }195 assert sync_content == b"example=true"196 assert async_content == b"example=true"197198199@pytest.mark.asyncio200async def test_urlencoded_none():201 headers, stream = encode_request(data={"example": None})202 assert isinstance(stream, typing.Iterable)203 assert isinstance(stream, typing.AsyncIterable)204205 sync_content = b"".join([part for part in stream])206 async_content = b"".join([part async for part in stream])207208 assert headers == {209 "Content-Length": "8",210 "Content-Type": "application/x-www-form-urlencoded",211 }212 assert sync_content == b"example="213 assert async_content == b"example="214215216@pytest.mark.asyncio217async def test_urlencoded_list():218 headers, stream = encode_request(data={"example": ["a", 1, True]})219 assert isinstance(stream, typing.Iterable)220 assert isinstance(stream, typing.AsyncIterable)221222 sync_content = b"".join([part for part in stream])223 async_content = b"".join([part async for part in stream])224225 assert headers == {226 "Content-Length": "32",227 "Content-Type": "application/x-www-form-urlencoded",228 }229 assert sync_content == b"example=a&example=1&example=true"230 assert async_content == b"example=a&example=1&example=true"231232233@pytest.mark.asyncio234async def test_multipart_files_content():235 files = {"file": io.BytesIO(b"<file content>")}236 headers, stream = encode_request(files=files, boundary=b"+++")237 assert isinstance(stream, typing.Iterable)238 assert isinstance(stream, typing.AsyncIterable)239240 sync_content = b"".join([part for part in stream])241 async_content = b"".join([part async for part in stream])242243 assert headers == {244 "Content-Length": "138",245 "Content-Type": "multipart/form-data; boundary=+++",246 }247 assert sync_content == b"".join(248 [249 b"--+++\r\n",250 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',251 b"Content-Type: application/octet-stream\r\n",252 b"\r\n",253 b"<file content>\r\n",254 b"--+++--\r\n",255 ]256 )257 assert async_content == b"".join(258 [259 b"--+++\r\n",260 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',261 b"Content-Type: application/octet-stream\r\n",262 b"\r\n",263 b"<file content>\r\n",264 b"--+++--\r\n",265 ]266 )267268269@pytest.mark.asyncio270async def test_multipart_data_and_files_content():271 data = {"message": "Hello, world!"}272 files = {"file": io.BytesIO(b"<file content>")}273 headers, stream = encode_request(data=data, files=files, boundary=b"+++")274 assert isinstance(stream, typing.Iterable)275 assert isinstance(stream, typing.AsyncIterable)276277 sync_content = b"".join([part for part in stream])278 async_content = b"".join([part async for part in stream])279280 assert headers == {281 "Content-Length": "210",282 "Content-Type": "multipart/form-data; boundary=+++",283 }284 assert sync_content == b"".join(285 [286 b"--+++\r\n",287 b'Content-Disposition: form-data; name="message"\r\n',288 b"\r\n",289 b"Hello, world!\r\n",290 b"--+++\r\n",291 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',292 b"Content-Type: application/octet-stream\r\n",293 b"\r\n",294 b"<file content>\r\n",295 b"--+++--\r\n",296 ]297 )298 assert async_content == b"".join(299 [300 b"--+++\r\n",301 b'Content-Disposition: form-data; name="message"\r\n',302 b"\r\n",303 b"Hello, world!\r\n",304 b"--+++\r\n",305 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',306 b"Content-Type: application/octet-stream\r\n",307 b"\r\n",308 b"<file content>\r\n",309 b"--+++--\r\n",310 ]311 )312313314@pytest.mark.asyncio315async def test_empty_request():316 headers, stream = encode_request(data={}, files={})317 assert isinstance(stream, typing.Iterable)318 assert isinstance(stream, typing.AsyncIterable)319320 sync_content = b"".join([part for part in stream])321 async_content = b"".join([part async for part in stream])322323 assert headers == {}324 assert sync_content == b""325 assert async_content == b""326327328def test_invalid_argument():329 with pytest.raises(TypeError):330 encode_request(123) # type: ignore331332333@pytest.mark.asyncio334async def test_multipart_multiple_files_single_input_content():335 files = [336 ("file", io.BytesIO(b"<file content 1>")),337 ("file", io.BytesIO(b"<file content 2>")),338 ]339 headers, stream = encode_request(files=files, boundary=b"+++")340 assert isinstance(stream, typing.Iterable)341 assert isinstance(stream, typing.AsyncIterable)342343 sync_content = b"".join([part for part in stream])344 async_content = b"".join([part async for part in stream])345346 assert headers == {347 "Content-Length": "271",348 "Content-Type": "multipart/form-data; boundary=+++",349 }350 assert sync_content == b"".join(351 [352 b"--+++\r\n",353 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',354 b"Content-Type: application/octet-stream\r\n",355 b"\r\n",356 b"<file content 1>\r\n",357 b"--+++\r\n",358 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',359 b"Content-Type: application/octet-stream\r\n",360 b"\r\n",361 b"<file content 2>\r\n",362 b"--+++--\r\n",363 ]364 )365 assert async_content == b"".join(366 [367 b"--+++\r\n",368 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',369 b"Content-Type: application/octet-stream\r\n",370 b"\r\n",371 b"<file content 1>\r\n",372 b"--+++\r\n",373 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',374 b"Content-Type: application/octet-stream\r\n",375 b"\r\n",376 b"<file content 2>\r\n",377 b"--+++--\r\n",378 ]379 )380381382@pytest.mark.asyncio383async def test_response_empty_content():384 headers, stream = encode_response()385 assert isinstance(stream, typing.Iterable)386 assert isinstance(stream, typing.AsyncIterable)387388 sync_content = b"".join([part for part in stream])389 async_content = b"".join([part async for part in stream])390391 assert headers == {}392 assert sync_content == b""393 assert async_content == b""394395396@pytest.mark.asyncio397async def test_response_bytes_content():398 headers, stream = encode_response(content=b"Hello, world!")399 assert isinstance(stream, typing.Iterable)400 assert isinstance(stream, typing.AsyncIterable)401402 sync_content = b"".join([part for part in stream])403 async_content = b"".join([part async for part in stream])404405 assert headers == {"Content-Length": "13"}406 assert sync_content == b"Hello, world!"407 assert async_content == b"Hello, world!"408409410@pytest.mark.asyncio411async def test_response_iterator_content():412 def hello_world():413 yield b"Hello, "414 yield b"world!"415416 headers, stream = encode_response(content=hello_world())417 assert isinstance(stream, typing.Iterable)418 assert not isinstance(stream, typing.AsyncIterable)419420 content = b"".join([part for part in stream])421422 assert headers == {"Transfer-Encoding": "chunked"}423 assert content == b"Hello, world!"424425 with pytest.raises(httpx.StreamConsumed):426 [part for part in stream]427428429@pytest.mark.asyncio430async def test_response_aiterator_content():431 async def hello_world():432 yield b"Hello, "433 yield b"world!"434435 headers, stream = encode_response(content=hello_world())436 assert not isinstance(stream, typing.Iterable)437 assert isinstance(stream, typing.AsyncIterable)438439 content = b"".join([part async for part in stream])440441 assert headers == {"Transfer-Encoding": "chunked"}442 assert content == b"Hello, world!"443444 with pytest.raises(httpx.StreamConsumed):445 [part async for part in stream]446447448def test_response_invalid_argument():449 with pytest.raises(TypeError):450 encode_response(123) # type: ignore