Skip to content
Myr

test_content.py

This file

Path

tests/test_content.py

413 lines · 12.8 KiB · Python

Last changed by

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

History of this file

Take it

1import io2import 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)1516    sync_content = stream.read()17    async_content = await stream.aread()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    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_iterator_content():53    def hello_world():54        yield b"Hello, "55        yield b"world!"5657    headers, stream = encode_request(content=hello_world())58    assert isinstance(stream, typing.Iterable)59    assert not isinstance(stream, typing.AsyncIterable)6061    content = b"".join([part for part in stream])6263    assert headers == {"Transfer-Encoding": "chunked"}64    assert content == b"Hello, world!"6566    with pytest.raises(httpx.StreamConsumed):67        [part for part in stream]6869    # Support 'data' for compat with requests.70    with pytest.warns(DeprecationWarning):71        headers, stream = encode_request(data=hello_world())  # type: ignore72    assert isinstance(stream, typing.Iterable)73    assert not isinstance(stream, typing.AsyncIterable)7475    content = b"".join([part for part in stream])7677    assert headers == {"Transfer-Encoding": "chunked"}78    assert content == b"Hello, world!"798081@pytest.mark.asyncio82async def test_aiterator_content():83    async def hello_world():84        yield b"Hello, "85        yield b"world!"8687    headers, stream = encode_request(content=hello_world())88    assert not isinstance(stream, typing.Iterable)89    assert isinstance(stream, typing.AsyncIterable)9091    content = b"".join([part async for part in stream])9293    assert headers == {"Transfer-Encoding": "chunked"}94    assert content == b"Hello, world!"9596    with pytest.raises(httpx.StreamConsumed):97        [part async for part in stream]9899    # Support 'data' for compat with requests.100    with pytest.warns(DeprecationWarning):101        headers, stream = encode_request(data=hello_world())  # type: ignore102    assert not isinstance(stream, typing.Iterable)103    assert isinstance(stream, typing.AsyncIterable)104105    content = b"".join([part async for part in stream])106107    assert headers == {"Transfer-Encoding": "chunked"}108    assert content == b"Hello, world!"109110111@pytest.mark.asyncio112async def test_json_content():113    headers, stream = encode_request(json={"Hello": "world!"})114    assert isinstance(stream, typing.Iterable)115    assert isinstance(stream, typing.AsyncIterable)116117    sync_content = b"".join([part for part in stream])118    async_content = b"".join([part async for part in stream])119120    assert headers == {121        "Content-Length": "19",122        "Content-Type": "application/json",123    }124    assert sync_content == b'{"Hello": "world!"}'125    assert async_content == b'{"Hello": "world!"}'126127128@pytest.mark.asyncio129async def test_urlencoded_content():130    headers, stream = encode_request(data={"Hello": "world!"})131    assert isinstance(stream, typing.Iterable)132    assert isinstance(stream, typing.AsyncIterable)133134    sync_content = b"".join([part for part in stream])135    async_content = b"".join([part async for part in stream])136137    assert headers == {138        "Content-Length": "14",139        "Content-Type": "application/x-www-form-urlencoded",140    }141    assert sync_content == b"Hello=world%21"142    assert async_content == b"Hello=world%21"143144145@pytest.mark.asyncio146async def test_urlencoded_boolean():147    headers, stream = encode_request(data={"example": True})148    assert isinstance(stream, typing.Iterable)149    assert isinstance(stream, typing.AsyncIterable)150151    sync_content = b"".join([part for part in stream])152    async_content = b"".join([part async for part in stream])153154    assert headers == {155        "Content-Length": "12",156        "Content-Type": "application/x-www-form-urlencoded",157    }158    assert sync_content == b"example=true"159    assert async_content == b"example=true"160161162@pytest.mark.asyncio163async def test_urlencoded_none():164    headers, stream = encode_request(data={"example": None})165    assert isinstance(stream, typing.Iterable)166    assert isinstance(stream, typing.AsyncIterable)167168    sync_content = b"".join([part for part in stream])169    async_content = b"".join([part async for part in stream])170171    assert headers == {172        "Content-Length": "8",173        "Content-Type": "application/x-www-form-urlencoded",174    }175    assert sync_content == b"example="176    assert async_content == b"example="177178179@pytest.mark.asyncio180async def test_urlencoded_list():181    headers, stream = encode_request(data={"example": ["a", 1, True]})182    assert isinstance(stream, typing.Iterable)183    assert isinstance(stream, typing.AsyncIterable)184185    sync_content = b"".join([part for part in stream])186    async_content = b"".join([part async for part in stream])187188    assert headers == {189        "Content-Length": "32",190        "Content-Type": "application/x-www-form-urlencoded",191    }192    assert sync_content == b"example=a&example=1&example=true"193    assert async_content == b"example=a&example=1&example=true"194195196@pytest.mark.asyncio197async def test_multipart_files_content():198    files = {"file": io.BytesIO(b"<file content>")}199    headers, stream = encode_request(files=files, boundary=b"+++")200    assert isinstance(stream, typing.Iterable)201    assert isinstance(stream, typing.AsyncIterable)202203    sync_content = b"".join([part for part in stream])204    async_content = b"".join([part async for part in stream])205206    assert headers == {207        "Content-Length": "138",208        "Content-Type": "multipart/form-data; boundary=+++",209    }210    assert sync_content == b"".join(211        [212            b"--+++\r\n",213            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',214            b"Content-Type: application/octet-stream\r\n",215            b"\r\n",216            b"<file content>\r\n",217            b"--+++--\r\n",218        ]219    )220    assert async_content == b"".join(221        [222            b"--+++\r\n",223            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',224            b"Content-Type: application/octet-stream\r\n",225            b"\r\n",226            b"<file content>\r\n",227            b"--+++--\r\n",228        ]229    )230231232@pytest.mark.asyncio233async def test_multipart_data_and_files_content():234    data = {"message": "Hello, world!"}235    files = {"file": io.BytesIO(b"<file content>")}236    headers, stream = encode_request(data=data, 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": "210",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="message"\r\n',251            b"\r\n",252            b"Hello, world!\r\n",253            b"--+++\r\n",254            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',255            b"Content-Type: application/octet-stream\r\n",256            b"\r\n",257            b"<file content>\r\n",258            b"--+++--\r\n",259        ]260    )261    assert async_content == b"".join(262        [263            b"--+++\r\n",264            b'Content-Disposition: form-data; name="message"\r\n',265            b"\r\n",266            b"Hello, world!\r\n",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>\r\n",272            b"--+++--\r\n",273        ]274    )275276277@pytest.mark.asyncio278async def test_empty_request():279    headers, stream = encode_request(data={}, files={})280    assert isinstance(stream, typing.Iterable)281    assert isinstance(stream, typing.AsyncIterable)282283    sync_content = b"".join([part for part in stream])284    async_content = b"".join([part async for part in stream])285286    assert headers == {}287    assert sync_content == b""288    assert async_content == b""289290291def test_invalid_argument():292    with pytest.raises(TypeError):293        encode_request(123)  # type: ignore294295296@pytest.mark.asyncio297async def test_multipart_multiple_files_single_input_content():298    files = [299        ("file", io.BytesIO(b"<file content 1>")),300        ("file", io.BytesIO(b"<file content 2>")),301    ]302    headers, stream = encode_request(files=files, boundary=b"+++")303    assert isinstance(stream, typing.Iterable)304    assert isinstance(stream, typing.AsyncIterable)305306    sync_content = b"".join([part for part in stream])307    async_content = b"".join([part async for part in stream])308309    assert headers == {310        "Content-Length": "271",311        "Content-Type": "multipart/form-data; boundary=+++",312    }313    assert sync_content == b"".join(314        [315            b"--+++\r\n",316            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',317            b"Content-Type: application/octet-stream\r\n",318            b"\r\n",319            b"<file content 1>\r\n",320            b"--+++\r\n",321            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',322            b"Content-Type: application/octet-stream\r\n",323            b"\r\n",324            b"<file content 2>\r\n",325            b"--+++--\r\n",326        ]327    )328    assert async_content == b"".join(329        [330            b"--+++\r\n",331            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',332            b"Content-Type: application/octet-stream\r\n",333            b"\r\n",334            b"<file content 1>\r\n",335            b"--+++\r\n",336            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',337            b"Content-Type: application/octet-stream\r\n",338            b"\r\n",339            b"<file content 2>\r\n",340            b"--+++--\r\n",341        ]342    )343344345@pytest.mark.asyncio346async def test_response_empty_content():347    headers, stream = encode_response()348    assert isinstance(stream, typing.Iterable)349    assert isinstance(stream, typing.AsyncIterable)350351    sync_content = b"".join([part for part in stream])352    async_content = b"".join([part async for part in stream])353354    assert headers == {}355    assert sync_content == b""356    assert async_content == b""357358359@pytest.mark.asyncio360async def test_response_bytes_content():361    headers, stream = encode_response(content=b"Hello, world!")362    assert isinstance(stream, typing.Iterable)363    assert isinstance(stream, typing.AsyncIterable)364365    sync_content = b"".join([part for part in stream])366    async_content = b"".join([part async for part in stream])367368    assert headers == {"Content-Length": "13"}369    assert sync_content == b"Hello, world!"370    assert async_content == b"Hello, world!"371372373@pytest.mark.asyncio374async def test_response_iterator_content():375    def hello_world():376        yield b"Hello, "377        yield b"world!"378379    headers, stream = encode_response(content=hello_world())380    assert isinstance(stream, typing.Iterable)381    assert not isinstance(stream, typing.AsyncIterable)382383    content = b"".join([part for part in stream])384385    assert headers == {"Transfer-Encoding": "chunked"}386    assert content == b"Hello, world!"387388    with pytest.raises(httpx.StreamConsumed):389        [part for part in stream]390391392@pytest.mark.asyncio393async def test_response_aiterator_content():394    async def hello_world():395        yield b"Hello, "396        yield b"world!"397398    headers, stream = encode_response(content=hello_world())399    assert not isinstance(stream, typing.Iterable)400    assert isinstance(stream, typing.AsyncIterable)401402    content = b"".join([part async for part in stream])403404    assert headers == {"Transfer-Encoding": "chunked"}405    assert content == b"Hello, world!"406407    with pytest.raises(httpx.StreamConsumed):408        [part async for part in stream]409410411def test_response_invalid_argument():412    with pytest.raises(TypeError):413        encode_response(123)  # type: ignore