Skip to content
Myr

test_content_streams.py

This file

Path

tests/test_content_streams.py

253 lines · 7.6 KiB · Python

Last changed by

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

History of this file

Take it

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

Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78

lines 1–4why, one step back
1import io
23import pytest4

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 5–6why, one step back
5from httpx import StreamConsumed
6from httpx._content_streams import ContentStream, encode

Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78

lines 7–27why, one step back
7
89@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()

Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97

line 28why, one step back
28 assert stream.get_headers() == {}

Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78

lines 29–191why, one step back
29 assert sync_content == b""
30 assert async_content == b""313233@pytest.mark.asyncio34async def test_bytes_content():35 stream = encode(data=b"Hello, world!")36 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!"434445@pytest.mark.asyncio46async def test_iterator_content():47 def hello_world():48 yield b"Hello, "49 yield b"world!"5051 stream = encode(data=hello_world())52 content = b"".join([part for part in stream])5354 assert not stream.can_replay()55 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}56 assert content == b"Hello, world!"5758 with pytest.raises(RuntimeError):59 [part async for part in stream]6061 with pytest.raises(StreamConsumed):62 [part for part in stream]636465@pytest.mark.asyncio66async def test_aiterator_content():67 async def hello_world():68 yield b"Hello, "69 yield b"world!"7071 stream = encode(data=hello_world())72 content = b"".join([part async for part in stream])7374 assert not stream.can_replay()75 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}76 assert content == b"Hello, world!"7778 with pytest.raises(RuntimeError):79 [part for part in stream]8081 with pytest.raises(StreamConsumed):82 [part async for part in stream]838485@pytest.mark.asyncio86async def test_json_content():87 stream = encode(json={"Hello": "world!"})88 sync_content = b"".join([part for part in stream])89 async_content = b"".join([part async for part in stream])9091 assert stream.can_replay()92 assert stream.get_headers() == {93 "Content-Length": "19",94 "Content-Type": "application/json",95 }96 assert sync_content == b'{"Hello": "world!"}'97 assert async_content == b'{"Hello": "world!"}'9899100@pytest.mark.asyncio101async def test_urlencoded_content():102 stream = encode(data={"Hello": "world!"})103 sync_content = b"".join([part for part in stream])104 async_content = b"".join([part async for part in stream])105106 assert stream.can_replay()107 assert stream.get_headers() == {108 "Content-Length": "14",109 "Content-Type": "application/x-www-form-urlencoded",110 }111 assert sync_content == b"Hello=world%21"112 assert async_content == b"Hello=world%21"113114115@pytest.mark.asyncio116async def test_multipart_files_content():117 files = {"file": io.BytesIO(b"<file content>")}118 stream = encode(files=files, boundary=b"+++")119 sync_content = b"".join([part for part in stream])120 async_content = b"".join([part async for part in stream])121122 assert stream.can_replay()123 assert stream.get_headers() == {124 "Content-Length": "138",125 "Content-Type": "multipart/form-data; boundary=+++",126 }127 assert sync_content == b"".join(128 [129 b"--+++\r\n",130 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',131 b"Content-Type: application/octet-stream\r\n",132 b"\r\n",133 b"<file content>\r\n",134 b"--+++--\r\n",135 ]136 )137 assert async_content == b"".join(138 [139 b"--+++\r\n",140 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',141 b"Content-Type: application/octet-stream\r\n",142 b"\r\n",143 b"<file content>\r\n",144 b"--+++--\r\n",145 ]146 )147148149@pytest.mark.asyncio150async def test_multipart_data_and_files_content():151 data = {"message": "Hello, world!"}152 files = {"file": io.BytesIO(b"<file content>")}153 stream = encode(data=data, files=files, boundary=b"+++")154 sync_content = b"".join([part for part in stream])155 async_content = b"".join([part async for part in stream])156157 assert stream.can_replay()158 assert stream.get_headers() == {159 "Content-Length": "210",160 "Content-Type": "multipart/form-data; boundary=+++",161 }162 assert sync_content == b"".join(163 [164 b"--+++\r\n",165 b'Content-Disposition: form-data; name="message"\r\n',166 b"\r\n",167 b"Hello, world!\r\n",168 b"--+++\r\n",169 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',170 b"Content-Type: application/octet-stream\r\n",171 b"\r\n",172 b"<file content>\r\n",173 b"--+++--\r\n",174 ]175 )176 assert async_content == b"".join(177 [178 b"--+++\r\n",179 b'Content-Disposition: form-data; name="message"\r\n',180 b"\r\n",181 b"Hello, world!\r\n",182 b"--+++\r\n",183 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',184 b"Content-Type: application/octet-stream\r\n",185 b"\r\n",186 b"<file content>\r\n",187 b"--+++--\r\n",188 ]189 )190191

Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f

lines 192–198why, one step back
192@pytest.mark.asyncio
193async def test_empty_request():194 stream = encode(data={}, files={})195 sync_content = b"".join([part for part in stream])196 async_content = b"".join([part async for part in stream])197198 assert stream.can_replay()

Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97

line 199why, one step back
199 assert stream.get_headers() == {}

Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f

lines 200–203why, one step back
200 assert sync_content == b""
201 assert async_content == b""202203

Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78

lines 204–205why, one step back
204def test_invalid_argument():
205 with pytest.raises(TypeError):

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

line 206why, one step back
206 encode(123) # type: ignore

Add support for multiple files per POST field (#1032)euri10before adoption4d28795

lines 207–253why, one step back
207
208209@pytest.mark.asyncio210async def test_multipart_multiple_files_single_input_content():211 files = [212 ("file", io.BytesIO(b"<file content 1>")),213 ("file", io.BytesIO(b"<file content 2>")),214 ]215 stream = encode(files=files, boundary=b"+++")216 sync_content = b"".join([part for part in stream])217 async_content = b"".join([part async for part in stream])218219 assert stream.can_replay()220 assert stream.get_headers() == {221 "Content-Length": "271",222 "Content-Type": "multipart/form-data; boundary=+++",223 }224 assert sync_content == b"".join(225 [226 b"--+++\r\n",227 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',228 b"Content-Type: application/octet-stream\r\n",229 b"\r\n",230 b"<file content 1>\r\n",231 b"--+++\r\n",232 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',233 b"Content-Type: application/octet-stream\r\n",234 b"\r\n",235 b"<file content 2>\r\n",236 b"--+++--\r\n",237 ]238 )239 assert async_content == b"".join(240 [241 b"--+++\r\n",242 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',243 b"Content-Type: application/octet-stream\r\n",244 b"\r\n",245 b"<file content 1>\r\n",246 b"--+++\r\n",247 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',248 b"Content-Type: application/octet-stream\r\n",249 b"\r\n",250 b"<file content 2>\r\n",251 b"--+++--\r\n",252 ]253 )