Skip to content
Myr

test_content_streams.py

This file

Path

tests/test_content_streams.py

206 lines · 6.0 KiB · Python

Last changed by

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

History of this file

Take it

1import io23import pytest45from httpx import StreamConsumed6from httpx._content_streams import ContentStream, encode789@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()28    assert stream.get_headers() == {}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    )190191192@pytest.mark.asyncio193async 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()199    assert stream.get_headers() == {}200    assert sync_content == b""201    assert async_content == b""202203204def test_invalid_argument():205    with pytest.raises(TypeError):206        encode(123)