Skip to content
Myr

test_content.py

This file

Path

tests/test_content.py

425 lines · 13.2 KiB · Python

Last changed by

Prefer `Content-Length` over `Transfer-Encoding: chunked` for `content=<file-like>` cases. (#1619)Tom Christiebefore adoption2129a97

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_bytesio_content():53    headers, stream = encode_request(content=io.BytesIO(b"Hello, world!"))54    assert isinstance(stream, typing.Iterable)55    assert not isinstance(stream, typing.AsyncIterable)5657    content = b"".join([part for part in stream])5859    assert headers == {"Content-Length": "13"}60    assert content == b"Hello, world!"616263@pytest.mark.asyncio64async def test_iterator_content():65    def hello_world():66        yield b"Hello, "67        yield b"world!"6869    headers, stream = encode_request(content=hello_world())70    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!"7778    with pytest.raises(httpx.StreamConsumed):79        [part for part in stream]8081    # Support 'data' for compat with requests.82    with pytest.warns(DeprecationWarning):83        headers, stream = encode_request(data=hello_world())  # type: ignore84    assert isinstance(stream, typing.Iterable)85    assert not isinstance(stream, typing.AsyncIterable)8687    content = b"".join([part for part in stream])8889    assert headers == {"Transfer-Encoding": "chunked"}90    assert content == b"Hello, world!"919293@pytest.mark.asyncio94async def test_aiterator_content():95    async def hello_world():96        yield b"Hello, "97        yield b"world!"9899    headers, stream = encode_request(content=hello_world())100    assert not isinstance(stream, typing.Iterable)101    assert isinstance(stream, typing.AsyncIterable)102103    content = b"".join([part async for part in stream])104105    assert headers == {"Transfer-Encoding": "chunked"}106    assert content == b"Hello, world!"107108    with pytest.raises(httpx.StreamConsumed):109        [part async for part in stream]110111    # Support 'data' for compat with requests.112    with pytest.warns(DeprecationWarning):113        headers, stream = encode_request(data=hello_world())  # type: ignore114    assert not isinstance(stream, typing.Iterable)115    assert isinstance(stream, typing.AsyncIterable)116117    content = b"".join([part async for part in stream])118119    assert headers == {"Transfer-Encoding": "chunked"}120    assert content == b"Hello, world!"121122123@pytest.mark.asyncio124async def test_json_content():125    headers, stream = encode_request(json={"Hello": "world!"})126    assert isinstance(stream, typing.Iterable)127    assert isinstance(stream, typing.AsyncIterable)128129    sync_content = b"".join([part for part in stream])130    async_content = b"".join([part async for part in stream])131132    assert headers == {133        "Content-Length": "19",134        "Content-Type": "application/json",135    }136    assert sync_content == b'{"Hello": "world!"}'137    assert async_content == b'{"Hello": "world!"}'138139140@pytest.mark.asyncio141async def test_urlencoded_content():142    headers, stream = encode_request(data={"Hello": "world!"})143    assert isinstance(stream, typing.Iterable)144    assert isinstance(stream, typing.AsyncIterable)145146    sync_content = b"".join([part for part in stream])147    async_content = b"".join([part async for part in stream])148149    assert headers == {150        "Content-Length": "14",151        "Content-Type": "application/x-www-form-urlencoded",152    }153    assert sync_content == b"Hello=world%21"154    assert async_content == b"Hello=world%21"155156157@pytest.mark.asyncio158async def test_urlencoded_boolean():159    headers, stream = encode_request(data={"example": True})160    assert isinstance(stream, typing.Iterable)161    assert isinstance(stream, typing.AsyncIterable)162163    sync_content = b"".join([part for part in stream])164    async_content = b"".join([part async for part in stream])165166    assert headers == {167        "Content-Length": "12",168        "Content-Type": "application/x-www-form-urlencoded",169    }170    assert sync_content == b"example=true"171    assert async_content == b"example=true"172173174@pytest.mark.asyncio175async def test_urlencoded_none():176    headers, stream = encode_request(data={"example": None})177    assert isinstance(stream, typing.Iterable)178    assert isinstance(stream, typing.AsyncIterable)179180    sync_content = b"".join([part for part in stream])181    async_content = b"".join([part async for part in stream])182183    assert headers == {184        "Content-Length": "8",185        "Content-Type": "application/x-www-form-urlencoded",186    }187    assert sync_content == b"example="188    assert async_content == b"example="189190191@pytest.mark.asyncio192async def test_urlencoded_list():193    headers, stream = encode_request(data={"example": ["a", 1, True]})194    assert isinstance(stream, typing.Iterable)195    assert isinstance(stream, typing.AsyncIterable)196197    sync_content = b"".join([part for part in stream])198    async_content = b"".join([part async for part in stream])199200    assert headers == {201        "Content-Length": "32",202        "Content-Type": "application/x-www-form-urlencoded",203    }204    assert sync_content == b"example=a&example=1&example=true"205    assert async_content == b"example=a&example=1&example=true"206207208@pytest.mark.asyncio209async def test_multipart_files_content():210    files = {"file": io.BytesIO(b"<file content>")}211    headers, stream = encode_request(files=files, boundary=b"+++")212    assert isinstance(stream, typing.Iterable)213    assert isinstance(stream, typing.AsyncIterable)214215    sync_content = b"".join([part for part in stream])216    async_content = b"".join([part async for part in stream])217218    assert headers == {219        "Content-Length": "138",220        "Content-Type": "multipart/form-data; boundary=+++",221    }222    assert sync_content == b"".join(223        [224            b"--+++\r\n",225            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',226            b"Content-Type: application/octet-stream\r\n",227            b"\r\n",228            b"<file content>\r\n",229            b"--+++--\r\n",230        ]231    )232    assert async_content == b"".join(233        [234            b"--+++\r\n",235            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',236            b"Content-Type: application/octet-stream\r\n",237            b"\r\n",238            b"<file content>\r\n",239            b"--+++--\r\n",240        ]241    )242243244@pytest.mark.asyncio245async def test_multipart_data_and_files_content():246    data = {"message": "Hello, world!"}247    files = {"file": io.BytesIO(b"<file content>")}248    headers, stream = encode_request(data=data, 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": "210",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="message"\r\n',263            b"\r\n",264            b"Hello, world!\r\n",265            b"--+++\r\n",266            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',267            b"Content-Type: application/octet-stream\r\n",268            b"\r\n",269            b"<file content>\r\n",270            b"--+++--\r\n",271        ]272    )273    assert async_content == b"".join(274        [275            b"--+++\r\n",276            b'Content-Disposition: form-data; name="message"\r\n',277            b"\r\n",278            b"Hello, world!\r\n",279            b"--+++\r\n",280            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',281            b"Content-Type: application/octet-stream\r\n",282            b"\r\n",283            b"<file content>\r\n",284            b"--+++--\r\n",285        ]286    )287288289@pytest.mark.asyncio290async def test_empty_request():291    headers, stream = encode_request(data={}, files={})292    assert isinstance(stream, typing.Iterable)293    assert isinstance(stream, typing.AsyncIterable)294295    sync_content = b"".join([part for part in stream])296    async_content = b"".join([part async for part in stream])297298    assert headers == {}299    assert sync_content == b""300    assert async_content == b""301302303def test_invalid_argument():304    with pytest.raises(TypeError):305        encode_request(123)  # type: ignore306307308@pytest.mark.asyncio309async def test_multipart_multiple_files_single_input_content():310    files = [311        ("file", io.BytesIO(b"<file content 1>")),312        ("file", io.BytesIO(b"<file content 2>")),313    ]314    headers, stream = encode_request(files=files, boundary=b"+++")315    assert isinstance(stream, typing.Iterable)316    assert isinstance(stream, typing.AsyncIterable)317318    sync_content = b"".join([part for part in stream])319    async_content = b"".join([part async for part in stream])320321    assert headers == {322        "Content-Length": "271",323        "Content-Type": "multipart/form-data; boundary=+++",324    }325    assert sync_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    )340    assert async_content == b"".join(341        [342            b"--+++\r\n",343            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',344            b"Content-Type: application/octet-stream\r\n",345            b"\r\n",346            b"<file content 1>\r\n",347            b"--+++\r\n",348            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',349            b"Content-Type: application/octet-stream\r\n",350            b"\r\n",351            b"<file content 2>\r\n",352            b"--+++--\r\n",353        ]354    )355356357@pytest.mark.asyncio358async def test_response_empty_content():359    headers, stream = encode_response()360    assert isinstance(stream, typing.Iterable)361    assert isinstance(stream, typing.AsyncIterable)362363    sync_content = b"".join([part for part in stream])364    async_content = b"".join([part async for part in stream])365366    assert headers == {}367    assert sync_content == b""368    assert async_content == b""369370371@pytest.mark.asyncio372async def test_response_bytes_content():373    headers, stream = encode_response(content=b"Hello, world!")374    assert isinstance(stream, typing.Iterable)375    assert isinstance(stream, typing.AsyncIterable)376377    sync_content = b"".join([part for part in stream])378    async_content = b"".join([part async for part in stream])379380    assert headers == {"Content-Length": "13"}381    assert sync_content == b"Hello, world!"382    assert async_content == b"Hello, world!"383384385@pytest.mark.asyncio386async def test_response_iterator_content():387    def hello_world():388        yield b"Hello, "389        yield b"world!"390391    headers, stream = encode_response(content=hello_world())392    assert isinstance(stream, typing.Iterable)393    assert not isinstance(stream, typing.AsyncIterable)394395    content = b"".join([part for part in stream])396397    assert headers == {"Transfer-Encoding": "chunked"}398    assert content == b"Hello, world!"399400    with pytest.raises(httpx.StreamConsumed):401        [part for part in stream]402403404@pytest.mark.asyncio405async def test_response_aiterator_content():406    async def hello_world():407        yield b"Hello, "408        yield b"world!"409410    headers, stream = encode_response(content=hello_world())411    assert not isinstance(stream, typing.Iterable)412    assert isinstance(stream, typing.AsyncIterable)413414    content = b"".join([part async for part in stream])415416    assert headers == {"Transfer-Encoding": "chunked"}417    assert content == b"Hello, world!"418419    with pytest.raises(httpx.StreamConsumed):420        [part async for part in stream]421422423def test_response_invalid_argument():424    with pytest.raises(TypeError):425        encode_response(123)  # type: ignore