Skip to content
Myr

test_content_streams.py

This file

Path

tests/test_content_streams.py

132 lines · 3.6 KiB · Python

Last changed by

Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955

History of this file

Take it

1import io23import pytest45from httpx.content_streams import encode6from httpx.exceptions import StreamConsumed789@pytest.mark.asyncio10async def test_empty_content():11    stream = encode()12    content = b"".join([part async for part in stream])1314    assert stream.can_replay()15    assert stream.get_headers() == {}16    assert content == b""171819@pytest.mark.asyncio20async def test_bytes_content():21    stream = encode(data=b"Hello, world!")22    content = b"".join([part async for part in stream])2324    assert stream.can_replay()25    assert stream.get_headers() == {"Content-Length": "13"}26    assert content == b"Hello, world!"272829@pytest.mark.asyncio30async def test_aiterator_content():31    async def hello_world():32        yield b"Hello, "33        yield b"world!"3435    stream = encode(data=hello_world())36    content = b"".join([part async for part in stream])3738    assert not stream.can_replay()39    assert stream.get_headers() == {"Transfer-Encoding": "chunked"}40    assert content == b"Hello, world!"414243@pytest.mark.asyncio44async def test_aiterator_is_stream_consumed():45    async def hello_world():46        yield b"Hello, "47        yield b"world!"4849    stream = encode(data=hello_world())50    b"".join([part async for part in stream])5152    assert stream.is_stream_consumed5354    with pytest.raises(StreamConsumed) as _:55        b"".join([part async for part in stream])565758@pytest.mark.asyncio59async def test_json_content():60    stream = encode(json={"Hello": "world!"})61    content = b"".join([part async for part in stream])6263    assert stream.can_replay()64    assert stream.get_headers() == {65        "Content-Length": "19",66        "Content-Type": "application/json",67    }68    assert content == b'{"Hello": "world!"}'697071@pytest.mark.asyncio72async def test_urlencoded_content():73    stream = encode(data={"Hello": "world!"})74    content = b"".join([part async for part in stream])7576    assert stream.can_replay()77    assert stream.get_headers() == {78        "Content-Length": "14",79        "Content-Type": "application/x-www-form-urlencoded",80    }81    assert content == b"Hello=world%21"828384@pytest.mark.asyncio85async def test_multipart_files_content():86    files = {"file": io.BytesIO(b"<file content>")}87    stream = encode(files=files, boundary=b"+++")88    content = b"".join([part async for part in stream])8990    assert stream.can_replay()91    assert stream.get_headers() == {92        "Content-Length": "138",93        "Content-Type": "multipart/form-data; boundary=+++",94    }95    assert content == b"".join(96        [97            b"--+++\r\n",98            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',99            b"Content-Type: application/octet-stream\r\n",100            b"\r\n",101            b"<file content>\r\n",102            b"--+++--\r\n",103        ]104    )105106107@pytest.mark.asyncio108async def test_multipart_data_and_files_content():109    data = {"message": "Hello, world!"}110    files = {"file": io.BytesIO(b"<file content>")}111    stream = encode(data=data, files=files, boundary=b"+++")112    content = b"".join([part async for part in stream])113114    assert stream.can_replay()115    assert stream.get_headers() == {116        "Content-Length": "210",117        "Content-Type": "multipart/form-data; boundary=+++",118    }119    assert content == b"".join(120        [121            b"--+++\r\n",122            b'Content-Disposition: form-data; name="message"\r\n',123            b"\r\n",124            b"Hello, world!\r\n",125            b"--+++\r\n",126            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',127            b"Content-Type: application/octet-stream\r\n",128            b"\r\n",129            b"<file content>\r\n",130            b"--+++--\r\n",131        ]132    )