test_content.py
This file
Path
tests/test_content.py
483 lines · 16.0 KiB · Python
Last changed by
Use httpx public API for 'test_content' tests (#2494)Tom Christiebefore adoption71a1589
History of this fileTake it
1import io2import typing34import pytest56import httpx78method = "POST"9url = "https://www.example.com"101112@pytest.mark.asyncio13async 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.asyncio27async 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.asyncio54async 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.asyncio66async 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.asyncio94async 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.asyncio130async 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.asyncio166async 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.asyncio184async 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.asyncio202async 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.asyncio220async 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.asyncio238async 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.asyncio256async 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.asyncio299async 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.asyncio346async 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: ignore362363364@pytest.mark.asyncio365async def test_multipart_multiple_files_single_input_content():366 files = [367 ("file", io.BytesIO(b"<file content 1>")),368 ("file", io.BytesIO(b"<file content 2>")),369 ]370 headers = {"Content-Type": "multipart/form-data; boundary=+++"}371 request = httpx.Request(method, url, files=files, headers=headers)372 assert isinstance(request.stream, typing.Iterable)373 assert isinstance(request.stream, typing.AsyncIterable)374375 sync_content = b"".join([part for part in request.stream])376 async_content = b"".join([part async for part in request.stream])377378 assert request.headers == {379 "Host": "www.example.com",380 "Content-Length": "271",381 "Content-Type": "multipart/form-data; boundary=+++",382 }383 assert sync_content == b"".join(384 [385 b"--+++\r\n",386 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',387 b"Content-Type: application/octet-stream\r\n",388 b"\r\n",389 b"<file content 1>\r\n",390 b"--+++\r\n",391 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',392 b"Content-Type: application/octet-stream\r\n",393 b"\r\n",394 b"<file content 2>\r\n",395 b"--+++--\r\n",396 ]397 )398 assert async_content == b"".join(399 [400 b"--+++\r\n",401 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',402 b"Content-Type: application/octet-stream\r\n",403 b"\r\n",404 b"<file content 1>\r\n",405 b"--+++\r\n",406 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',407 b"Content-Type: application/octet-stream\r\n",408 b"\r\n",409 b"<file content 2>\r\n",410 b"--+++--\r\n",411 ]412 )413414415@pytest.mark.asyncio416async def test_response_empty_content():417 response = httpx.Response(200)418 assert isinstance(response.stream, typing.Iterable)419 assert isinstance(response.stream, typing.AsyncIterable)420421 sync_content = b"".join([part for part in response.stream])422 async_content = b"".join([part async for part in response.stream])423424 assert response.headers == {}425 assert sync_content == b""426 assert async_content == b""427428429@pytest.mark.asyncio430async def test_response_bytes_content():431 response = httpx.Response(200, content=b"Hello, world!")432 assert isinstance(response.stream, typing.Iterable)433 assert isinstance(response.stream, typing.AsyncIterable)434435 sync_content = b"".join([part for part in response.stream])436 async_content = b"".join([part async for part in response.stream])437438 assert response.headers == {"Content-Length": "13"}439 assert sync_content == b"Hello, world!"440 assert async_content == b"Hello, world!"441442443@pytest.mark.asyncio444async def test_response_iterator_content():445 def hello_world() -> typing.Iterator[bytes]:446 yield b"Hello, "447 yield b"world!"448449 response = httpx.Response(200, content=hello_world())450 assert isinstance(response.stream, typing.Iterable)451 assert not isinstance(response.stream, typing.AsyncIterable)452453 content = b"".join([part for part in response.stream])454455 assert response.headers == {"Transfer-Encoding": "chunked"}456 assert content == b"Hello, world!"457458 with pytest.raises(httpx.StreamConsumed):459 [part for part in response.stream]460461462@pytest.mark.asyncio463async def test_response_aiterator_content():464 async def hello_world() -> typing.AsyncIterator[bytes]:465 yield b"Hello, "466 yield b"world!"467468 response = httpx.Response(200, content=hello_world())469 assert not isinstance(response.stream, typing.Iterable)470 assert isinstance(response.stream, typing.AsyncIterable)471472 content = b"".join([part async for part in response.stream])473474 assert response.headers == {"Transfer-Encoding": "chunked"}475 assert content == b"Hello, world!"476477 with pytest.raises(httpx.StreamConsumed):478 [part async for part in response.stream]479480481def test_response_invalid_argument():482 with pytest.raises(TypeError):483 httpx.Response(200, content=123) # type: ignore