test_content.py
This file
Path
tests/test_content.py
359 lines · 11.1 KiB · Python
Last changed by
Refactor content streams (#1296)Tom Christiebefore adoption354c4ca
History of this fileTake it
1import io2import typing34import pytest56from httpx import StreamConsumed7from httpx._content import encode_request, encode_response8910@pytest.mark.asyncio11async def test_empty_content():12 headers, stream = encode_request()13 assert isinstance(stream, typing.Iterable)14 assert isinstance(stream, typing.AsyncIterable)1516 sync_content = b"".join([part for part in stream])17 async_content = b"".join([part async for part in stream])1819 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 headers, stream = encode_request(data=b"Hello, world!") # type: ignore39 assert isinstance(stream, typing.Iterable)40 assert isinstance(stream, typing.AsyncIterable)4142 sync_content = b"".join([part for part in stream])43 async_content = b"".join([part async for part in stream])4445 assert headers == {"Content-Length": "13"}46 assert sync_content == b"Hello, world!"47 assert async_content == b"Hello, world!"484950@pytest.mark.asyncio51async def test_iterator_content():52 def hello_world():53 yield b"Hello, "54 yield b"world!"5556 headers, stream = encode_request(content=hello_world())57 assert isinstance(stream, typing.Iterable)58 assert not isinstance(stream, typing.AsyncIterable)5960 content = b"".join([part for part in stream])6162 assert headers == {"Transfer-Encoding": "chunked"}63 assert content == b"Hello, world!"6465 with pytest.raises(StreamConsumed):66 [part for part in stream]6768 # Support 'data' for compat with requests.69 headers, stream = encode_request(data=hello_world()) # type: ignore70 assert isinstance(stream, typing.Iterable)71 assert not isinstance(stream, typing.AsyncIterable)7273 content = b"".join([part for part in stream])7475 assert headers == {"Transfer-Encoding": "chunked"}76 assert content == b"Hello, world!"777879@pytest.mark.asyncio80async def test_aiterator_content():81 async def hello_world():82 yield b"Hello, "83 yield b"world!"8485 headers, stream = encode_request(content=hello_world())86 assert not isinstance(stream, typing.Iterable)87 assert isinstance(stream, typing.AsyncIterable)8889 content = b"".join([part async for part in stream])9091 assert headers == {"Transfer-Encoding": "chunked"}92 assert content == b"Hello, world!"9394 with pytest.raises(StreamConsumed):95 [part async for part in stream]9697 # Support 'data' for compat with requests.98 headers, stream = encode_request(data=hello_world()) # type: ignore99 assert not isinstance(stream, typing.Iterable)100 assert isinstance(stream, typing.AsyncIterable)101102 content = b"".join([part async for part in stream])103104 assert headers == {"Transfer-Encoding": "chunked"}105 assert content == b"Hello, world!"106107108@pytest.mark.asyncio109async def test_json_content():110 headers, stream = encode_request(json={"Hello": "world!"})111 assert isinstance(stream, typing.Iterable)112 assert isinstance(stream, typing.AsyncIterable)113114 sync_content = b"".join([part for part in stream])115 async_content = b"".join([part async for part in stream])116117 assert headers == {118 "Content-Length": "19",119 "Content-Type": "application/json",120 }121 assert sync_content == b'{"Hello": "world!"}'122 assert async_content == b'{"Hello": "world!"}'123124125@pytest.mark.asyncio126async def test_urlencoded_content():127 headers, stream = encode_request(data={"Hello": "world!"})128 assert isinstance(stream, typing.Iterable)129 assert isinstance(stream, typing.AsyncIterable)130131 sync_content = b"".join([part for part in stream])132 async_content = b"".join([part async for part in stream])133134 assert headers == {135 "Content-Length": "14",136 "Content-Type": "application/x-www-form-urlencoded",137 }138 assert sync_content == b"Hello=world%21"139 assert async_content == b"Hello=world%21"140141142@pytest.mark.asyncio143async def test_multipart_files_content():144 files = {"file": io.BytesIO(b"<file content>")}145 headers, stream = encode_request(files=files, boundary=b"+++")146 assert isinstance(stream, typing.Iterable)147 assert isinstance(stream, typing.AsyncIterable)148149 sync_content = b"".join([part for part in stream])150 async_content = b"".join([part async for part in stream])151152 assert headers == {153 "Content-Length": "138",154 "Content-Type": "multipart/form-data; boundary=+++",155 }156 assert sync_content == b"".join(157 [158 b"--+++\r\n",159 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',160 b"Content-Type: application/octet-stream\r\n",161 b"\r\n",162 b"<file content>\r\n",163 b"--+++--\r\n",164 ]165 )166 assert async_content == b"".join(167 [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 )176177178@pytest.mark.asyncio179async def test_multipart_data_and_files_content():180 data = {"message": "Hello, world!"}181 files = {"file": io.BytesIO(b"<file content>")}182 headers, stream = encode_request(data=data, files=files, boundary=b"+++")183 assert isinstance(stream, typing.Iterable)184 assert isinstance(stream, typing.AsyncIterable)185186 sync_content = b"".join([part for part in stream])187 async_content = b"".join([part async for part in stream])188189 assert headers == {190 "Content-Length": "210",191 "Content-Type": "multipart/form-data; boundary=+++",192 }193 assert sync_content == b"".join(194 [195 b"--+++\r\n",196 b'Content-Disposition: form-data; name="message"\r\n',197 b"\r\n",198 b"Hello, world!\r\n",199 b"--+++\r\n",200 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',201 b"Content-Type: application/octet-stream\r\n",202 b"\r\n",203 b"<file content>\r\n",204 b"--+++--\r\n",205 ]206 )207 assert async_content == b"".join(208 [209 b"--+++\r\n",210 b'Content-Disposition: form-data; name="message"\r\n',211 b"\r\n",212 b"Hello, world!\r\n",213 b"--+++\r\n",214 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',215 b"Content-Type: application/octet-stream\r\n",216 b"\r\n",217 b"<file content>\r\n",218 b"--+++--\r\n",219 ]220 )221222223@pytest.mark.asyncio224async def test_empty_request():225 headers, stream = encode_request(data={}, files={})226 assert isinstance(stream, typing.Iterable)227 assert isinstance(stream, typing.AsyncIterable)228229 sync_content = b"".join([part for part in stream])230 async_content = b"".join([part async for part in stream])231232 assert headers == {}233 assert sync_content == b""234 assert async_content == b""235236237def test_invalid_argument():238 with pytest.raises(TypeError):239 encode_request(123) # type: ignore240241242@pytest.mark.asyncio243async def test_multipart_multiple_files_single_input_content():244 files = [245 ("file", io.BytesIO(b"<file content 1>")),246 ("file", io.BytesIO(b"<file content 2>")),247 ]248 headers, stream = encode_request(files=files, boundary=b"+++")249 assert isinstance(stream, typing.Iterable)250 assert isinstance(stream, typing.AsyncIterable)251252 sync_content = b"".join([part for part in stream])253 async_content = b"".join([part async for part in stream])254255 assert headers == {256 "Content-Length": "271",257 "Content-Type": "multipart/form-data; boundary=+++",258 }259 assert sync_content == b"".join(260 [261 b"--+++\r\n",262 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',263 b"Content-Type: application/octet-stream\r\n",264 b"\r\n",265 b"<file content 1>\r\n",266 b"--+++\r\n",267 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',268 b"Content-Type: application/octet-stream\r\n",269 b"\r\n",270 b"<file content 2>\r\n",271 b"--+++--\r\n",272 ]273 )274 assert async_content == b"".join(275 [276 b"--+++\r\n",277 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',278 b"Content-Type: application/octet-stream\r\n",279 b"\r\n",280 b"<file content 1>\r\n",281 b"--+++\r\n",282 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',283 b"Content-Type: application/octet-stream\r\n",284 b"\r\n",285 b"<file content 2>\r\n",286 b"--+++--\r\n",287 ]288 )289290291@pytest.mark.asyncio292async def test_response_empty_content():293 headers, stream = encode_response()294 assert isinstance(stream, typing.Iterable)295 assert isinstance(stream, typing.AsyncIterable)296297 sync_content = b"".join([part for part in stream])298 async_content = b"".join([part async for part in stream])299300 assert headers == {}301 assert sync_content == b""302 assert async_content == b""303304305@pytest.mark.asyncio306async def test_response_bytes_content():307 headers, stream = encode_response(content=b"Hello, world!")308 assert isinstance(stream, typing.Iterable)309 assert isinstance(stream, typing.AsyncIterable)310311 sync_content = b"".join([part for part in stream])312 async_content = b"".join([part async for part in stream])313314 assert headers == {"Content-Length": "13"}315 assert sync_content == b"Hello, world!"316 assert async_content == b"Hello, world!"317318319@pytest.mark.asyncio320async def test_response_iterator_content():321 def hello_world():322 yield b"Hello, "323 yield b"world!"324325 headers, stream = encode_response(content=hello_world())326 assert isinstance(stream, typing.Iterable)327 assert not isinstance(stream, typing.AsyncIterable)328329 content = b"".join([part for part in stream])330331 assert headers == {"Transfer-Encoding": "chunked"}332 assert content == b"Hello, world!"333334 with pytest.raises(StreamConsumed):335 [part for part in stream]336337338@pytest.mark.asyncio339async def test_response_aiterator_content():340 async def hello_world():341 yield b"Hello, "342 yield b"world!"343344 headers, stream = encode_response(content=hello_world())345 assert not isinstance(stream, typing.Iterable)346 assert isinstance(stream, typing.AsyncIterable)347348 content = b"".join([part async for part in stream])349350 assert headers == {"Transfer-Encoding": "chunked"}351 assert content == b"Hello, world!"352353 with pytest.raises(StreamConsumed):354 [part async for part in stream]355356357def test_response_invalid_argument():358 with pytest.raises(TypeError):359 encode_response(123) # type: ignore