Skip to content
Myr

test_content.py

This file

Path

tests/test_content.py

486 lines · 16.1 KiB · Python

Last changed by

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

History of this file

Take it

1import io2import typing34import pytest56import httpx78method = "POST"9url = "https://www.example.com"101112@pytest.mark.anyio13async def test_empty_content():14    request = httpx.Request(method, url)15    assert isinstance(request.stream, httpx.SyncByteStream)16    assert isinstance(request.stream, httpx.AsyncByteStream)1718    sync_content = b"".join([part for part in request.stream])19    async_content = b"".join([part async for part in request.stream])2021    assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}22    assert sync_content == b""23    assert async_content == b""242526@pytest.mark.anyio27async def test_bytes_content():28    request = httpx.Request(method, url, content=b"Hello, world!")29    assert isinstance(request.stream, typing.Iterable)30    assert isinstance(request.stream, typing.AsyncIterable)3132    sync_content = b"".join([part for part in request.stream])33    async_content = b"".join([part async for part in request.stream])3435    assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}36    assert sync_content == b"Hello, world!"37    assert async_content == b"Hello, world!"3839    # Support 'data' for compat with requests.40    with pytest.warns(DeprecationWarning):41        request = httpx.Request(method, url, data=b"Hello, world!")  # type: ignore42    assert isinstance(request.stream, typing.Iterable)43    assert isinstance(request.stream, typing.AsyncIterable)4445    sync_content = b"".join([part for part in request.stream])46    async_content = b"".join([part async for part in request.stream])4748    assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}49    assert sync_content == b"Hello, world!"50    assert async_content == b"Hello, world!"515253@pytest.mark.anyio54async def test_bytesio_content():55    request = httpx.Request(method, url, content=io.BytesIO(b"Hello, world!"))56    assert isinstance(request.stream, typing.Iterable)57    assert not isinstance(request.stream, typing.AsyncIterable)5859    content = b"".join([part for part in request.stream])6061    assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}62    assert content == b"Hello, world!"636465@pytest.mark.anyio66async def test_async_bytesio_content():67    class AsyncBytesIO:68        def __init__(self, content: bytes) -> None:69            self._idx = 070            self._content = content7172        async def aread(self, chunk_size: int) -> bytes:73            chunk = self._content[self._idx : self._idx + chunk_size]74            self._idx = self._idx + chunk_size75            return chunk7677        async def __aiter__(self):78            yield self._content  # pragma: no cover7980    request = httpx.Request(method, url, content=AsyncBytesIO(b"Hello, world!"))81    assert not isinstance(request.stream, typing.Iterable)82    assert isinstance(request.stream, typing.AsyncIterable)8384    content = b"".join([part async for part in request.stream])8586    assert request.headers == {87        "Host": "www.example.com",88        "Transfer-Encoding": "chunked",89    }90    assert content == b"Hello, world!"919293@pytest.mark.anyio94async def test_iterator_content():95    def hello_world() -> typing.Iterator[bytes]:96        yield b"Hello, "97        yield b"world!"9899    request = httpx.Request(method, url, content=hello_world())100    assert isinstance(request.stream, typing.Iterable)101    assert not isinstance(request.stream, typing.AsyncIterable)102103    content = b"".join([part for part in request.stream])104105    assert request.headers == {106        "Host": "www.example.com",107        "Transfer-Encoding": "chunked",108    }109    assert content == b"Hello, world!"110111    with pytest.raises(httpx.StreamConsumed):112        [part for part in request.stream]113114    # Support 'data' for compat with requests.115    with pytest.warns(DeprecationWarning):116        request = httpx.Request(method, url, data=hello_world())  # type: ignore117    assert isinstance(request.stream, typing.Iterable)118    assert not isinstance(request.stream, typing.AsyncIterable)119120    content = b"".join([part for part in request.stream])121122    assert request.headers == {123        "Host": "www.example.com",124        "Transfer-Encoding": "chunked",125    }126    assert content == b"Hello, world!"127128129@pytest.mark.anyio130async def test_aiterator_content():131    async def hello_world() -> typing.AsyncIterator[bytes]:132        yield b"Hello, "133        yield b"world!"134135    request = httpx.Request(method, url, content=hello_world())136    assert not isinstance(request.stream, typing.Iterable)137    assert isinstance(request.stream, typing.AsyncIterable)138139    content = b"".join([part async for part in request.stream])140141    assert request.headers == {142        "Host": "www.example.com",143        "Transfer-Encoding": "chunked",144    }145    assert content == b"Hello, world!"146147    with pytest.raises(httpx.StreamConsumed):148        [part async for part in request.stream]149150    # Support 'data' for compat with requests.151    with pytest.warns(DeprecationWarning):152        request = httpx.Request(method, url, data=hello_world())  # type: ignore153    assert not isinstance(request.stream, typing.Iterable)154    assert isinstance(request.stream, typing.AsyncIterable)155156    content = b"".join([part async for part in request.stream])157158    assert request.headers == {159        "Host": "www.example.com",160        "Transfer-Encoding": "chunked",161    }162    assert content == b"Hello, world!"163164165@pytest.mark.anyio166async def test_json_content():167    request = httpx.Request(method, url, json={"Hello": "world!"})168    assert isinstance(request.stream, typing.Iterable)169    assert isinstance(request.stream, typing.AsyncIterable)170171    sync_content = b"".join([part for part in request.stream])172    async_content = b"".join([part async for part in request.stream])173174    assert request.headers == {175        "Host": "www.example.com",176        "Content-Length": "19",177        "Content-Type": "application/json",178    }179    assert sync_content == b'{"Hello": "world!"}'180    assert async_content == b'{"Hello": "world!"}'181182183@pytest.mark.anyio184async def test_urlencoded_content():185    request = httpx.Request(method, url, data={"Hello": "world!"})186    assert isinstance(request.stream, typing.Iterable)187    assert isinstance(request.stream, typing.AsyncIterable)188189    sync_content = b"".join([part for part in request.stream])190    async_content = b"".join([part async for part in request.stream])191192    assert request.headers == {193        "Host": "www.example.com",194        "Content-Length": "14",195        "Content-Type": "application/x-www-form-urlencoded",196    }197    assert sync_content == b"Hello=world%21"198    assert async_content == b"Hello=world%21"199200201@pytest.mark.anyio202async def test_urlencoded_boolean():203    request = httpx.Request(method, url, data={"example": True})204    assert isinstance(request.stream, typing.Iterable)205    assert isinstance(request.stream, typing.AsyncIterable)206207    sync_content = b"".join([part for part in request.stream])208    async_content = b"".join([part async for part in request.stream])209210    assert request.headers == {211        "Host": "www.example.com",212        "Content-Length": "12",213        "Content-Type": "application/x-www-form-urlencoded",214    }215    assert sync_content == b"example=true"216    assert async_content == b"example=true"217218219@pytest.mark.anyio220async def test_urlencoded_none():221    request = httpx.Request(method, url, data={"example": None})222    assert isinstance(request.stream, typing.Iterable)223    assert isinstance(request.stream, typing.AsyncIterable)224225    sync_content = b"".join([part for part in request.stream])226    async_content = b"".join([part async for part in request.stream])227228    assert request.headers == {229        "Host": "www.example.com",230        "Content-Length": "8",231        "Content-Type": "application/x-www-form-urlencoded",232    }233    assert sync_content == b"example="234    assert async_content == b"example="235236237@pytest.mark.anyio238async def test_urlencoded_list():239    request = httpx.Request(method, url, data={"example": ["a", 1, True]})240    assert isinstance(request.stream, typing.Iterable)241    assert isinstance(request.stream, typing.AsyncIterable)242243    sync_content = b"".join([part for part in request.stream])244    async_content = b"".join([part async for part in request.stream])245246    assert request.headers == {247        "Host": "www.example.com",248        "Content-Length": "32",249        "Content-Type": "application/x-www-form-urlencoded",250    }251    assert sync_content == b"example=a&example=1&example=true"252    assert async_content == b"example=a&example=1&example=true"253254255@pytest.mark.anyio256async def test_multipart_files_content():257    files = {"file": io.BytesIO(b"<file content>")}258    headers = {"Content-Type": "multipart/form-data; boundary=+++"}259    request = httpx.Request(260        method,261        url,262        files=files,263        headers=headers,264    )265    assert isinstance(request.stream, typing.Iterable)266    assert isinstance(request.stream, typing.AsyncIterable)267268    sync_content = b"".join([part for part in request.stream])269    async_content = b"".join([part async for part in request.stream])270271    assert request.headers == {272        "Host": "www.example.com",273        "Content-Length": "138",274        "Content-Type": "multipart/form-data; boundary=+++",275    }276    assert sync_content == b"".join(277        [278            b"--+++\r\n",279            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',280            b"Content-Type: application/octet-stream\r\n",281            b"\r\n",282            b"<file content>\r\n",283            b"--+++--\r\n",284        ]285    )286    assert async_content == b"".join(287        [288            b"--+++\r\n",289            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',290            b"Content-Type: application/octet-stream\r\n",291            b"\r\n",292            b"<file content>\r\n",293            b"--+++--\r\n",294        ]295    )296297298@pytest.mark.anyio299async def test_multipart_data_and_files_content():300    data = {"message": "Hello, world!"}301    files = {"file": io.BytesIO(b"<file content>")}302    headers = {"Content-Type": "multipart/form-data; boundary=+++"}303    request = httpx.Request(method, url, data=data, files=files, headers=headers)304    assert isinstance(request.stream, typing.Iterable)305    assert isinstance(request.stream, typing.AsyncIterable)306307    sync_content = b"".join([part for part in request.stream])308    async_content = b"".join([part async for part in request.stream])309310    assert request.headers == {311        "Host": "www.example.com",312        "Content-Length": "210",313        "Content-Type": "multipart/form-data; boundary=+++",314    }315    assert sync_content == b"".join(316        [317            b"--+++\r\n",318            b'Content-Disposition: form-data; name="message"\r\n',319            b"\r\n",320            b"Hello, world!\r\n",321            b"--+++\r\n",322            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',323            b"Content-Type: application/octet-stream\r\n",324            b"\r\n",325            b"<file content>\r\n",326            b"--+++--\r\n",327        ]328    )329    assert async_content == b"".join(330        [331            b"--+++\r\n",332            b'Content-Disposition: form-data; name="message"\r\n',333            b"\r\n",334            b"Hello, world!\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>\r\n",340            b"--+++--\r\n",341        ]342    )343344345@pytest.mark.anyio346async def test_empty_request():347    request = httpx.Request(method, url, data={}, files={})348    assert isinstance(request.stream, typing.Iterable)349    assert isinstance(request.stream, typing.AsyncIterable)350351    sync_content = b"".join([part for part in request.stream])352    async_content = b"".join([part async for part in request.stream])353354    assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}355    assert sync_content == b""356    assert async_content == b""357358359def test_invalid_argument():360    with pytest.raises(TypeError):361        httpx.Request(method, url, content=123)  # type: ignore362363    with pytest.raises(TypeError):364        httpx.Request(method, url, content={"a": "b"})  # type: ignore365366367@pytest.mark.anyio368async def test_multipart_multiple_files_single_input_content():369    files = [370        ("file", io.BytesIO(b"<file content 1>")),371        ("file", io.BytesIO(b"<file content 2>")),372    ]373    headers = {"Content-Type": "multipart/form-data; boundary=+++"}374    request = httpx.Request(method, url, files=files, headers=headers)375    assert isinstance(request.stream, typing.Iterable)376    assert isinstance(request.stream, typing.AsyncIterable)377378    sync_content = b"".join([part for part in request.stream])379    async_content = b"".join([part async for part in request.stream])380381    assert request.headers == {382        "Host": "www.example.com",383        "Content-Length": "271",384        "Content-Type": "multipart/form-data; boundary=+++",385    }386    assert sync_content == b"".join(387        [388            b"--+++\r\n",389            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',390            b"Content-Type: application/octet-stream\r\n",391            b"\r\n",392            b"<file content 1>\r\n",393            b"--+++\r\n",394            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',395            b"Content-Type: application/octet-stream\r\n",396            b"\r\n",397            b"<file content 2>\r\n",398            b"--+++--\r\n",399        ]400    )401    assert async_content == b"".join(402        [403            b"--+++\r\n",404            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',405            b"Content-Type: application/octet-stream\r\n",406            b"\r\n",407            b"<file content 1>\r\n",408            b"--+++\r\n",409            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',410            b"Content-Type: application/octet-stream\r\n",411            b"\r\n",412            b"<file content 2>\r\n",413            b"--+++--\r\n",414        ]415    )416417418@pytest.mark.anyio419async def test_response_empty_content():420    response = httpx.Response(200)421    assert isinstance(response.stream, typing.Iterable)422    assert isinstance(response.stream, typing.AsyncIterable)423424    sync_content = b"".join([part for part in response.stream])425    async_content = b"".join([part async for part in response.stream])426427    assert response.headers == {}428    assert sync_content == b""429    assert async_content == b""430431432@pytest.mark.anyio433async def test_response_bytes_content():434    response = httpx.Response(200, content=b"Hello, world!")435    assert isinstance(response.stream, typing.Iterable)436    assert isinstance(response.stream, typing.AsyncIterable)437438    sync_content = b"".join([part for part in response.stream])439    async_content = b"".join([part async for part in response.stream])440441    assert response.headers == {"Content-Length": "13"}442    assert sync_content == b"Hello, world!"443    assert async_content == b"Hello, world!"444445446@pytest.mark.anyio447async def test_response_iterator_content():448    def hello_world() -> typing.Iterator[bytes]:449        yield b"Hello, "450        yield b"world!"451452    response = httpx.Response(200, content=hello_world())453    assert isinstance(response.stream, typing.Iterable)454    assert not isinstance(response.stream, typing.AsyncIterable)455456    content = b"".join([part for part in response.stream])457458    assert response.headers == {"Transfer-Encoding": "chunked"}459    assert content == b"Hello, world!"460461    with pytest.raises(httpx.StreamConsumed):462        [part for part in response.stream]463464465@pytest.mark.anyio466async def test_response_aiterator_content():467    async def hello_world() -> typing.AsyncIterator[bytes]:468        yield b"Hello, "469        yield b"world!"470471    response = httpx.Response(200, content=hello_world())472    assert not isinstance(response.stream, typing.Iterable)473    assert isinstance(response.stream, typing.AsyncIterable)474475    content = b"".join([part async for part in response.stream])476477    assert response.headers == {"Transfer-Encoding": "chunked"}478    assert content == b"Hello, world!"479480    with pytest.raises(httpx.StreamConsumed):481        [part async for part in response.stream]482483484def test_response_invalid_argument():485    with pytest.raises(TypeError):486        httpx.Response(200, content=123)  # type: ignore