test_content.py
This file
Path
tests/test_content.py
410 lines · 12.7 KiB · Python
Last changed by
Handle `data={"key": [None|int|float|bool]}` cases. (#1539)Tom Christiebefore adoptionc26425a
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_urlencoded_boolean():144 headers, stream = encode_request(data={"example": True})145 assert isinstance(stream, typing.Iterable)146 assert isinstance(stream, typing.AsyncIterable)147148 sync_content = b"".join([part for part in stream])149 async_content = b"".join([part async for part in stream])150151 assert headers == {152 "Content-Length": "12",153 "Content-Type": "application/x-www-form-urlencoded",154 }155 assert sync_content == b"example=true"156 assert async_content == b"example=true"157158159@pytest.mark.asyncio160async def test_urlencoded_none():161 headers, stream = encode_request(data={"example": None})162 assert isinstance(stream, typing.Iterable)163 assert isinstance(stream, typing.AsyncIterable)164165 sync_content = b"".join([part for part in stream])166 async_content = b"".join([part async for part in stream])167168 assert headers == {169 "Content-Length": "8",170 "Content-Type": "application/x-www-form-urlencoded",171 }172 assert sync_content == b"example="173 assert async_content == b"example="174175176@pytest.mark.asyncio177async def test_urlencoded_list():178 headers, stream = encode_request(data={"example": ["a", 1, True]})179 assert isinstance(stream, typing.Iterable)180 assert isinstance(stream, typing.AsyncIterable)181182 sync_content = b"".join([part for part in stream])183 async_content = b"".join([part async for part in stream])184185 assert headers == {186 "Content-Length": "32",187 "Content-Type": "application/x-www-form-urlencoded",188 }189 assert sync_content == b"example=a&example=1&example=true"190 assert async_content == b"example=a&example=1&example=true"191192193@pytest.mark.asyncio194async def test_multipart_files_content():195 files = {"file": io.BytesIO(b"<file content>")}196 headers, stream = encode_request(files=files, boundary=b"+++")197 assert isinstance(stream, typing.Iterable)198 assert isinstance(stream, typing.AsyncIterable)199200 sync_content = b"".join([part for part in stream])201 async_content = b"".join([part async for part in stream])202203 assert headers == {204 "Content-Length": "138",205 "Content-Type": "multipart/form-data; boundary=+++",206 }207 assert sync_content == b"".join(208 [209 b"--+++\r\n",210 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',211 b"Content-Type: application/octet-stream\r\n",212 b"\r\n",213 b"<file content>\r\n",214 b"--+++--\r\n",215 ]216 )217 assert async_content == b"".join(218 [219 b"--+++\r\n",220 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',221 b"Content-Type: application/octet-stream\r\n",222 b"\r\n",223 b"<file content>\r\n",224 b"--+++--\r\n",225 ]226 )227228229@pytest.mark.asyncio230async def test_multipart_data_and_files_content():231 data = {"message": "Hello, world!"}232 files = {"file": io.BytesIO(b"<file content>")}233 headers, stream = encode_request(data=data, files=files, boundary=b"+++")234 assert isinstance(stream, typing.Iterable)235 assert isinstance(stream, typing.AsyncIterable)236237 sync_content = b"".join([part for part in stream])238 async_content = b"".join([part async for part in stream])239240 assert headers == {241 "Content-Length": "210",242 "Content-Type": "multipart/form-data; boundary=+++",243 }244 assert sync_content == b"".join(245 [246 b"--+++\r\n",247 b'Content-Disposition: form-data; name="message"\r\n',248 b"\r\n",249 b"Hello, world!\r\n",250 b"--+++\r\n",251 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',252 b"Content-Type: application/octet-stream\r\n",253 b"\r\n",254 b"<file content>\r\n",255 b"--+++--\r\n",256 ]257 )258 assert async_content == b"".join(259 [260 b"--+++\r\n",261 b'Content-Disposition: form-data; name="message"\r\n',262 b"\r\n",263 b"Hello, world!\r\n",264 b"--+++\r\n",265 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',266 b"Content-Type: application/octet-stream\r\n",267 b"\r\n",268 b"<file content>\r\n",269 b"--+++--\r\n",270 ]271 )272273274@pytest.mark.asyncio275async def test_empty_request():276 headers, stream = encode_request(data={}, files={})277 assert isinstance(stream, typing.Iterable)278 assert isinstance(stream, typing.AsyncIterable)279280 sync_content = b"".join([part for part in stream])281 async_content = b"".join([part async for part in stream])282283 assert headers == {}284 assert sync_content == b""285 assert async_content == b""286287288def test_invalid_argument():289 with pytest.raises(TypeError):290 encode_request(123) # type: ignore291292293@pytest.mark.asyncio294async def test_multipart_multiple_files_single_input_content():295 files = [296 ("file", io.BytesIO(b"<file content 1>")),297 ("file", io.BytesIO(b"<file content 2>")),298 ]299 headers, stream = encode_request(files=files, boundary=b"+++")300 assert isinstance(stream, typing.Iterable)301 assert isinstance(stream, typing.AsyncIterable)302303 sync_content = b"".join([part for part in stream])304 async_content = b"".join([part async for part in stream])305306 assert headers == {307 "Content-Length": "271",308 "Content-Type": "multipart/form-data; boundary=+++",309 }310 assert sync_content == b"".join(311 [312 b"--+++\r\n",313 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',314 b"Content-Type: application/octet-stream\r\n",315 b"\r\n",316 b"<file content 1>\r\n",317 b"--+++\r\n",318 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',319 b"Content-Type: application/octet-stream\r\n",320 b"\r\n",321 b"<file content 2>\r\n",322 b"--+++--\r\n",323 ]324 )325 assert async_content == b"".join(326 [327 b"--+++\r\n",328 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',329 b"Content-Type: application/octet-stream\r\n",330 b"\r\n",331 b"<file content 1>\r\n",332 b"--+++\r\n",333 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',334 b"Content-Type: application/octet-stream\r\n",335 b"\r\n",336 b"<file content 2>\r\n",337 b"--+++--\r\n",338 ]339 )340341342@pytest.mark.asyncio343async def test_response_empty_content():344 headers, stream = encode_response()345 assert isinstance(stream, typing.Iterable)346 assert isinstance(stream, typing.AsyncIterable)347348 sync_content = b"".join([part for part in stream])349 async_content = b"".join([part async for part in stream])350351 assert headers == {}352 assert sync_content == b""353 assert async_content == b""354355356@pytest.mark.asyncio357async def test_response_bytes_content():358 headers, stream = encode_response(content=b"Hello, world!")359 assert isinstance(stream, typing.Iterable)360 assert isinstance(stream, typing.AsyncIterable)361362 sync_content = b"".join([part for part in stream])363 async_content = b"".join([part async for part in stream])364365 assert headers == {"Content-Length": "13"}366 assert sync_content == b"Hello, world!"367 assert async_content == b"Hello, world!"368369370@pytest.mark.asyncio371async def test_response_iterator_content():372 def hello_world():373 yield b"Hello, "374 yield b"world!"375376 headers, stream = encode_response(content=hello_world())377 assert isinstance(stream, typing.Iterable)378 assert not isinstance(stream, typing.AsyncIterable)379380 content = b"".join([part for part in stream])381382 assert headers == {"Transfer-Encoding": "chunked"}383 assert content == b"Hello, world!"384385 with pytest.raises(StreamConsumed):386 [part for part in stream]387388389@pytest.mark.asyncio390async def test_response_aiterator_content():391 async def hello_world():392 yield b"Hello, "393 yield b"world!"394395 headers, stream = encode_response(content=hello_world())396 assert not isinstance(stream, typing.Iterable)397 assert isinstance(stream, typing.AsyncIterable)398399 content = b"".join([part async for part in stream])400401 assert headers == {"Transfer-Encoding": "chunked"}402 assert content == b"Hello, world!"403404 with pytest.raises(StreamConsumed):405 [part async for part in stream]406407408def test_response_invalid_argument():409 with pytest.raises(TypeError):410 encode_response(123) # type: ignore