Skip to content
Myr

test_content.py

This file

Path

tests/test_content.py

109 lines · 2.9 KiB · Python

Last changed by

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

History of this file

Take it

1import io23import pytest45from httpx.content import encode678@pytest.mark.asyncio9async def test_empty_content():10    content = encode()1112    assert content.can_replay()13    assert content.get_headers() == {}14    assert await content.aread() == b""151617@pytest.mark.asyncio18async def test_bytes_content():19    content = encode(data=b"Hello, world!")2021    assert content.can_replay()22    assert content.get_headers() == {"Content-Length": "13"}23    assert await content.aread() == b"Hello, world!"242526@pytest.mark.asyncio27async def test_aiterator_content():28    async def hello_world():29        yield b"Hello, "30        yield b"world!"3132    content = encode(data=hello_world())3334    assert not content.can_replay()35    assert content.get_headers() == {"Transfer-Encoding": "chunked"}36    assert await content.aread() == b"Hello, world!"373839@pytest.mark.asyncio40async def test_json_content():41    content = encode(json={"Hello": "world!"})4243    assert content.can_replay()44    assert content.get_headers() == {45        "Content-Length": "19",46        "Content-Type": "application/json",47    }48    assert await content.aread() == b'{"Hello": "world!"}'495051@pytest.mark.asyncio52async def test_urlencoded_content():53    content = encode(data={"Hello": "world!"})5455    assert content.can_replay()56    assert content.get_headers() == {57        "Content-Length": "14",58        "Content-Type": "application/x-www-form-urlencoded",59    }60    assert await content.aread() == b"Hello=world%21"616263@pytest.mark.asyncio64async def test_multipart_files_content():65    files = {"file": io.BytesIO(b"<file content>")}66    content = encode(files=files, boundary=b"+++")6768    assert content.can_replay()69    assert content.get_headers() == {70        "Content-Length": "138",71        "Content-Type": "multipart/form-data; boundary=+++",72    }73    assert await content.aread() == b"".join(74        [75            b"--+++\r\n",76            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',77            b"Content-Type: application/octet-stream\r\n",78            b"\r\n",79            b"<file content>\r\n",80            b"--+++--\r\n",81        ]82    )838485@pytest.mark.asyncio86async def test_multipart_data_and_files_content():87    data = {"message": "Hello, world!"}88    files = {"file": io.BytesIO(b"<file content>")}89    content = encode(data=data, files=files, boundary=b"+++")9091    assert content.can_replay()92    assert content.get_headers() == {93        "Content-Length": "210",94        "Content-Type": "multipart/form-data; boundary=+++",95    }96    assert await content.aread() == b"".join(97        [98            b"--+++\r\n",99            b'Content-Disposition: form-data; name="message"\r\n',100            b"\r\n",101            b"Hello, world!\r\n",102            b"--+++\r\n",103            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',104            b"Content-Type: application/octet-stream\r\n",105            b"\r\n",106            b"<file content>\r\n",107            b"--+++--\r\n",108        ]109    )