test_content_streams.py
This file
Path
tests/test_content_streams.py
348 lines · 10.5 KiB · Python
Last changed by
Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
History of this fileTake it
The walk stopped after 200 commits: lines older than 631ba97 point at it.
Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 1–5why, one step back1import io23import pytest45from httpx import StreamConsumedSupport `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
line 6why, one step back6from httpx._content_streams import ContentStream, encode, encode_responseRevert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 7–34why, one step back789@pytest.mark.asyncio10async def test_base_content():11 stream = ContentStream()12 sync_content = b"".join([part for part in stream])13 async_content = b"".join([part async for part in stream])1415 assert stream.can_replay()16 assert stream.get_headers() == {}17 assert sync_content == b""18 assert async_content == b""192021@pytest.mark.asyncio22async def test_empty_content():23 stream = encode()24 sync_content = b"".join([part for part in stream])25 async_content = b"".join([part async for part in stream])2627 assert stream.can_replay()28 assert stream.get_headers() == {}29 assert sync_content == b""30 assert async_content == b""313233@pytest.mark.asyncio34async def test_bytes_content():Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 35why, one step back35 stream = encode(content=b"Hello, world!")Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 36–43why, one step back36 sync_content = b"".join([part for part in stream])37 async_content = b"".join([part async for part in stream])3839 assert stream.can_replay()40 assert stream.get_headers() == {"Content-Length": "13"}41 assert sync_content == b"Hello, world!"42 assert async_content == b"Hello, world!"43Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 44–47why, one step back44 # Support 'data' for compat with requests.45 stream = encode(data=b"Hello, world!") # type: ignore46 sync_content = b"".join([part for part in stream])47 async_content = b"".join([part async for part in stream])Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 48why, one step back48Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 49–54why, one step back49 assert stream.can_replay()50 assert stream.get_headers() == {"Content-Length": "13"}51 assert sync_content == b"Hello, world!"52 assert async_content == b"Hello, world!"5354Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 55–60why, one step back55@pytest.mark.asyncio56async def test_iterator_content():57 def hello_world():58 yield b"Hello, "59 yield b"world!"60Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 61why, one step back61 stream = encode(content=hello_world())Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 62–73why, one step back62 content = b"".join([part for part in stream])6364 assert not stream.can_replay()65 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}66 assert content == b"Hello, world!"6768 with pytest.raises(RuntimeError):69 [part async for part in stream]7071 with pytest.raises(StreamConsumed):72 [part for part in stream]73Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 74–76why, one step back74 # Support 'data' for compat with requests.75 stream = encode(data=hello_world()) # type: ignore76 content = b"".join([part for part in stream])Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 77why, one step back77Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 78–82why, one step back78 assert not stream.can_replay()79 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}80 assert content == b"Hello, world!"8182Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 83–88why, one step back83@pytest.mark.asyncio84async def test_aiterator_content():85 async def hello_world():86 yield b"Hello, "87 yield b"world!"88Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 89why, one step back89 stream = encode(content=hello_world())Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 90–100why, one step back90 content = b"".join([part async for part in stream])9192 assert not stream.can_replay()93 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}94 assert content == b"Hello, world!"9596 with pytest.raises(RuntimeError):97 [part for part in stream]9899 with pytest.raises(StreamConsumed):100 [part async for part in stream]Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 101–108why, one step back101102 # Support 'data' for compat with requests.103 stream = encode(data=hello_world()) # type: ignore104 content = b"".join([part async for part in stream])105106 assert not stream.can_replay()107 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}108 assert content == b"Hello, world!"Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
lines 109–231why, one step back109110111@pytest.mark.asyncio112async def test_json_content():113 stream = encode(json={"Hello": "world!"})114 sync_content = b"".join([part for part in stream])115 async_content = b"".join([part async for part in stream])116117 assert stream.can_replay()118 assert stream.get_headers() == {119 "Content-Length": "19",120 "Content-Type": "application/json",121 }122 assert sync_content == b'{"Hello": "world!"}'123 assert async_content == b'{"Hello": "world!"}'124125126@pytest.mark.asyncio127async def test_urlencoded_content():128 stream = encode(data={"Hello": "world!"})129 sync_content = b"".join([part for part in stream])130 async_content = b"".join([part async for part in stream])131132 assert stream.can_replay()133 assert stream.get_headers() == {134 "Content-Length": "14",135 "Content-Type": "application/x-www-form-urlencoded",136 }137 assert sync_content == b"Hello=world%21"138 assert async_content == b"Hello=world%21"139140141@pytest.mark.asyncio142async def test_multipart_files_content():143 files = {"file": io.BytesIO(b"<file content>")}144 stream = encode(files=files, boundary=b"+++")145 sync_content = b"".join([part for part in stream])146 async_content = b"".join([part async for part in stream])147148 assert stream.can_replay()149 assert stream.get_headers() == {150 "Content-Length": "138",151 "Content-Type": "multipart/form-data; boundary=+++",152 }153 assert sync_content == b"".join(154 [155 b"--+++\r\n",156 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',157 b"Content-Type: application/octet-stream\r\n",158 b"\r\n",159 b"<file content>\r\n",160 b"--+++--\r\n",161 ]162 )163 assert async_content == b"".join(164 [165 b"--+++\r\n",166 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',167 b"Content-Type: application/octet-stream\r\n",168 b"\r\n",169 b"<file content>\r\n",170 b"--+++--\r\n",171 ]172 )173174175@pytest.mark.asyncio176async def test_multipart_data_and_files_content():177 data = {"message": "Hello, world!"}178 files = {"file": io.BytesIO(b"<file content>")}179 stream = encode(data=data, files=files, boundary=b"+++")180 sync_content = b"".join([part for part in stream])181 async_content = b"".join([part async for part in stream])182183 assert stream.can_replay()184 assert stream.get_headers() == {185 "Content-Length": "210",186 "Content-Type": "multipart/form-data; boundary=+++",187 }188 assert sync_content == b"".join(189 [190 b"--+++\r\n",191 b'Content-Disposition: form-data; name="message"\r\n',192 b"\r\n",193 b"Hello, world!\r\n",194 b"--+++\r\n",195 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',196 b"Content-Type: application/octet-stream\r\n",197 b"\r\n",198 b"<file content>\r\n",199 b"--+++--\r\n",200 ]201 )202 assert async_content == b"".join(203 [204 b"--+++\r\n",205 b'Content-Disposition: form-data; name="message"\r\n',206 b"\r\n",207 b"Hello, world!\r\n",208 b"--+++\r\n",209 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',210 b"Content-Type: application/octet-stream\r\n",211 b"\r\n",212 b"<file content>\r\n",213 b"--+++--\r\n",214 ]215 )216217218@pytest.mark.asyncio219async def test_empty_request():220 stream = encode(data={}, files={})221 sync_content = b"".join([part for part in stream])222 async_content = b"".join([part async for part in stream])223224 assert stream.can_replay()225 assert stream.get_headers() == {}226 assert sync_content == b""227 assert async_content == b""228229230def test_invalid_argument():231 with pytest.raises(TypeError):Type check tests (#1054)Florimond Mancabefore adoption0296c2b
line 232why, one step back232 encode(123) # type: ignoreAdd support for multiple files per POST field (#1032)euri10before adoption4d28795
lines 233–279why, one step back233234235@pytest.mark.asyncio236async def test_multipart_multiple_files_single_input_content():237 files = [238 ("file", io.BytesIO(b"<file content 1>")),239 ("file", io.BytesIO(b"<file content 2>")),240 ]241 stream = encode(files=files, boundary=b"+++")242 sync_content = b"".join([part for part in stream])243 async_content = b"".join([part async for part in stream])244245 assert stream.can_replay()246 assert stream.get_headers() == {247 "Content-Length": "271",248 "Content-Type": "multipart/form-data; boundary=+++",249 }250 assert sync_content == b"".join(251 [252 b"--+++\r\n",253 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',254 b"Content-Type: application/octet-stream\r\n",255 b"\r\n",256 b"<file content 1>\r\n",257 b"--+++\r\n",258 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',259 b"Content-Type: application/octet-stream\r\n",260 b"\r\n",261 b"<file content 2>\r\n",262 b"--+++--\r\n",263 ]264 )265 assert async_content == b"".join(266 [267 b"--+++\r\n",268 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',269 b"Content-Type: application/octet-stream\r\n",270 b"\r\n",271 b"<file content 1>\r\n",272 b"--+++\r\n",273 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',274 b"Content-Type: application/octet-stream\r\n",275 b"\r\n",276 b"<file content 2>\r\n",277 b"--+++--\r\n",278 ]279 )Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135
lines 280–348why, one step back280281282@pytest.mark.asyncio283async def test_response_empty_content():284 stream = encode_response()285 sync_content = b"".join([part for part in stream])286 async_content = b"".join([part async for part in stream])287288 assert stream.can_replay()289 assert stream.get_headers() == {}290 assert sync_content == b""291 assert async_content == b""292293294@pytest.mark.asyncio295async def test_response_bytes_content():296 stream = encode_response(content=b"Hello, world!")297 sync_content = b"".join([part for part in stream])298 async_content = b"".join([part async for part in stream])299300 assert stream.can_replay()301 assert stream.get_headers() == {"Content-Length": "13"}302 assert sync_content == b"Hello, world!"303 assert async_content == b"Hello, world!"304305306@pytest.mark.asyncio307async def test_response_iterator_content():308 def hello_world():309 yield b"Hello, "310 yield b"world!"311312 stream = encode_response(content=hello_world())313 content = b"".join([part for part in stream])314315 assert not stream.can_replay()316 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}317 assert content == b"Hello, world!"318319 with pytest.raises(RuntimeError):320 [part async for part in stream]321322 with pytest.raises(StreamConsumed):323 [part for part in stream]324325326@pytest.mark.asyncio327async def test_response_aiterator_content():328 async def hello_world():329 yield b"Hello, "330 yield b"world!"331332 stream = encode_response(content=hello_world())333 content = b"".join([part async for part in stream])334335 assert not stream.can_replay()336 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}337 assert content == b"Hello, world!"338339 with pytest.raises(RuntimeError):340 [part for part in stream]341342 with pytest.raises(StreamConsumed):343 [part async for part in stream]344345346def test_response_invalid_argument():347 with pytest.raises(TypeError):348 encode_response(123) # type: ignore