import pytest
from httpx import StreamConsumed
-from httpx._content_streams import ContentStream, encode, encode_response
+from httpx._content_streams import ContentStream, encode_request, encode_response
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_empty_content():
- stream = encode()
+ stream = encode_request()
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
@pytest.mark.asyncio
async def test_bytes_content():
- stream = encode(content=b"Hello, world!")
+ stream = encode_request(content=b"Hello, world!")
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
assert async_content == b"Hello, world!"
# Support 'data' for compat with requests.
- stream = encode(data=b"Hello, world!") # type: ignore
+ stream = encode_request(data=b"Hello, world!") # type: ignore
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
yield b"Hello, "
yield b"world!"
- stream = encode(content=hello_world())
+ stream = encode_request(content=hello_world())
content = b"".join([part for part in stream])
assert not stream.can_replay()
[part for part in stream]
# Support 'data' for compat with requests.
- stream = encode(data=hello_world()) # type: ignore
+ stream = encode_request(data=hello_world()) # type: ignore
content = b"".join([part for part in stream])
assert not stream.can_replay()
yield b"Hello, "
yield b"world!"
- stream = encode(content=hello_world())
+ stream = encode_request(content=hello_world())
content = b"".join([part async for part in stream])
assert not stream.can_replay()
[part async for part in stream]
# Support 'data' for compat with requests.
- stream = encode(data=hello_world()) # type: ignore
+ stream = encode_request(data=hello_world()) # type: ignore
content = b"".join([part async for part in stream])
assert not stream.can_replay()
@pytest.mark.asyncio
async def test_json_content():
- stream = encode(json={"Hello": "world!"})
+ stream = encode_request(json={"Hello": "world!"})
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
@pytest.mark.asyncio
async def test_urlencoded_content():
- stream = encode(data={"Hello": "world!"})
+ stream = encode_request(data={"Hello": "world!"})
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
@pytest.mark.asyncio
async def test_multipart_files_content():
files = {"file": io.BytesIO(b"<file content>")}
- stream = encode(files=files, boundary=b"+++")
+ stream = encode_request(files=files, boundary=b"+++")
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
async def test_multipart_data_and_files_content():
data = {"message": "Hello, world!"}
files = {"file": io.BytesIO(b"<file content>")}
- stream = encode(data=data, files=files, boundary=b"+++")
+ stream = encode_request(data=data, files=files, boundary=b"+++")
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
@pytest.mark.asyncio
async def test_empty_request():
- stream = encode(data={}, files={})
+ stream = encode_request(data={}, files={})
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
def test_invalid_argument():
with pytest.raises(TypeError):
- encode(123) # type: ignore
+ encode_request(123) # type: ignore
@pytest.mark.asyncio
("file", io.BytesIO(b"<file content 1>")),
("file", io.BytesIO(b"<file content 2>")),
]
- stream = encode(files=files, boundary=b"+++")
+ stream = encode_request(files=files, boundary=b"+++")
sync_content = b"".join([part for part in stream])
async_content = b"".join([part async for part in stream])
import pytest
import httpx
-from httpx._content_streams import MultipartStream, encode
+from httpx._content_streams import MultipartStream, encode_request
from httpx._utils import format_form_param
from tests.utils import MockTransport
with mock.patch("os.urandom", return_value=os.urandom(16)):
boundary = os.urandom(16).hex()
- stream = encode(data=data, files=files)
+ stream = encode_request(data=data, files=files)
assert isinstance(stream, MultipartStream)
assert stream.can_replay()
with mock.patch("os.urandom", return_value=os.urandom(16)):
boundary = os.urandom(16).hex()
- stream = encode(data={}, files=files)
+ stream = encode_request(data={}, files=files)
assert isinstance(stream, MultipartStream)
assert stream.can_replay()
with mock.patch("os.urandom", return_value=os.urandom(16)):
boundary = os.urandom(16).hex()
- stream = encode(data={}, files=files)
+ stream = encode_request(data={}, files=files)
assert isinstance(stream, MultipartStream)
assert stream.can_replay()
with mock.patch("os.urandom", return_value=os.urandom(16)):
boundary = os.urandom(16).hex()
- stream = encode(data={}, files=files)
+ stream = encode_request(data={}, files=files)
assert isinstance(stream, MultipartStream)
assert stream.can_replay()
fileobj: typing.Any = IteratorIO(data())
files = {"file": fileobj}
- stream = encode(files=files, boundary=b"+++")
+ stream = encode_request(files=files, boundary=b"+++")
assert not stream.can_replay()
content = (