Skip to content
Myr

encode -> encode_request (#1292)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent ff0febb · Browse files · 4 files+24 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+24 added−24 removed

  • tests:+21 added−21 removed
  • other:+3 added−3 removed

4 files+24 added−24 removed

modified:httpx/_content_streams.py+1 added-1 removed
361 unmodified lines
362 for chunk in self.iter_chunks():363 yield chunk364365366def encode(366def encode_request(367 content: RequestContent = None,368 data: RequestData = None,369 files: RequestFiles = None,370 json: typing.Any = None,
55 unmodified lines
modified:httpx/_models.py+2 added-2 removed
11 unmodified lines
1213import rfc398614import rfc3986.exceptions1516from ._content_streams import ByteStream, ContentStream, encode, encode_response16from ._content_streams import ByteStream, ContentStream, encode_request, encode_response17from ._decoders import (18 SUPPORTED_DECODERS,19 ContentDecoder,20 IdentityDecoder,
584 unmodified lines
605606 if stream is not None:607 self.stream = stream608 else:609 self.stream = encode(content, data, files, json)609 self.stream = encode_request(content, data, files, json)610611 self._prepare()612613 def _prepare(self) -> None:
634 unmodified lines
modified:tests/test_content_streams.py+15 added-15 removed
1 unmodified line
23import pytest45from httpx import StreamConsumed6from httpx._content_streams import ContentStream, encode, encode_response6from httpx._content_streams import ContentStream, encode_request, encode_response789@pytest.mark.asyncio10async def test_base_content():
8 unmodified lines
192021@pytest.mark.asyncio22async def test_empty_content():23 stream = encode()23 stream = encode_request()24 sync_content = b"".join([part for part in stream])25 async_content = b"".join([part async for part in stream])2627 assert stream.can_replay()
3 unmodified lines
313233@pytest.mark.asyncio34async def test_bytes_content():35 stream = encode(content=b"Hello, world!")35 stream = encode_request(content=b"Hello, world!")36 sync_content = b"".join([part for part in stream])37 async_content = b"".join([part async for part in stream])3839 assert stream.can_replay()
1 unmodified line
41 assert sync_content == b"Hello, world!"42 assert async_content == b"Hello, world!"4344 # Support 'data' for compat with requests.45 stream = encode(data=b"Hello, world!") # type: ignore45 stream = encode_request(data=b"Hello, world!") # type: ignore46 sync_content = b"".join([part for part in stream])47 async_content = b"".join([part async for part in stream])4849 assert stream.can_replay()
7 unmodified lines
57 def hello_world():58 yield b"Hello, "59 yield b"world!"6061 stream = encode(content=hello_world())61 stream = encode_request(content=hello_world())62 content = b"".join([part for part in stream])6364 assert not stream.can_replay()65 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}
5 unmodified lines
71 with pytest.raises(StreamConsumed):72 [part for part in stream]7374 # Support 'data' for compat with requests.75 stream = encode(data=hello_world()) # type: ignore75 stream = encode_request(data=hello_world()) # type: ignore76 content = b"".join([part for part in stream])7778 assert not stream.can_replay()79 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}
5 unmodified lines
85 async def hello_world():86 yield b"Hello, "87 yield b"world!"8889 stream = encode(content=hello_world())89 stream = encode_request(content=hello_world())90 content = b"".join([part async for part in stream])9192 assert not stream.can_replay()93 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}
5 unmodified lines
99 with pytest.raises(StreamConsumed):100 [part async for part in stream]101102 # Support 'data' for compat with requests.103 stream = encode(data=hello_world()) # type: ignore103 stream = encode_request(data=hello_world()) # type: ignore104 content = b"".join([part async for part in stream])105106 assert not stream.can_replay()107 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}
1 unmodified line
109110111@pytest.mark.asyncio112async def test_json_content():113 stream = encode(json={"Hello": "world!"})113 stream = encode_request(json={"Hello": "world!"})114 sync_content = b"".join([part for part in stream])115 async_content = b"".join([part async for part in stream])116117 assert stream.can_replay()
6 unmodified lines
124125126@pytest.mark.asyncio127async def test_urlencoded_content():128 stream = encode(data={"Hello": "world!"})128 stream = encode_request(data={"Hello": "world!"})129 sync_content = b"".join([part for part in stream])130 async_content = b"".join([part async for part in stream])131132 assert stream.can_replay()
7 unmodified lines
140141@pytest.mark.asyncio142async def test_multipart_files_content():143 files = {"file": io.BytesIO(b"<file content>")}144 stream = encode(files=files, boundary=b"+++")144 stream = encode_request(files=files, boundary=b"+++")145 sync_content = b"".join([part for part in stream])146 async_content = b"".join([part async for part in stream])147148 assert stream.can_replay()
26 unmodified lines
175@pytest.mark.asyncio176async def test_multipart_data_and_files_content():177 data = {"message": "Hello, world!"}178 files = {"file": io.BytesIO(b"<file content>")}179 stream = encode(data=data, files=files, boundary=b"+++")179 stream = encode_request(data=data, files=files, boundary=b"+++")180 sync_content = b"".join([part for part in stream])181 async_content = b"".join([part async for part in stream])182183 assert stream.can_replay()
32 unmodified lines
216217218@pytest.mark.asyncio219async def test_empty_request():220 stream = encode(data={}, files={})220 stream = encode_request(data={}, files={})221 sync_content = b"".join([part for part in stream])222 async_content = b"".join([part async for part in stream])223224 assert stream.can_replay()
3 unmodified lines
228229230def test_invalid_argument():231 with pytest.raises(TypeError):232 encode(123) # type: ignore232 encode_request(123) # type: ignore233234235@pytest.mark.asyncio236async def test_multipart_multiple_files_single_input_content():237 files = [238 ("file", io.BytesIO(b"<file content 1>")),239 ("file", io.BytesIO(b"<file content 2>")),240 ]241 stream = encode(files=files, boundary=b"+++")241 stream = encode_request(files=files, boundary=b"+++")242 sync_content = b"".join([part for part in stream])243 async_content = b"".join([part async for part in stream])244245 assert stream.can_replay()
103 unmodified lines
modified:tests/test_multipart.py+6 added-6 removed
5 unmodified lines
67import pytest89import httpx10from httpx._content_streams import MultipartStream, encode10from httpx._content_streams import MultipartStream, encode_request11from httpx._utils import format_form_param12from tests.utils import MockTransport1314
94 unmodified lines
109110 with mock.patch("os.urandom", return_value=os.urandom(16)):111 boundary = os.urandom(16).hex()112113 stream = encode(data=data, files=files)113 stream = encode_request(data=data, files=files)114 assert isinstance(stream, MultipartStream)115 assert stream.can_replay()116117 assert stream.content_type == f"multipart/form-data; boundary={boundary}"
18 unmodified lines
136 files = {"file": (None, io.BytesIO(b"<file content>"))}137 with mock.patch("os.urandom", return_value=os.urandom(16)):138 boundary = os.urandom(16).hex()139140 stream = encode(data={}, files=files)140 stream = encode_request(data={}, files=files)141 assert isinstance(stream, MultipartStream)142 assert stream.can_replay()143144 assert stream.content_type == f"multipart/form-data; boundary={boundary}"
18 unmodified lines
163 files = {"file": (file_name, io.BytesIO(b"<file content>"))}164 with mock.patch("os.urandom", return_value=os.urandom(16)):165 boundary = os.urandom(16).hex()166167 stream = encode(data={}, files=files)167 stream = encode_request(data={}, files=files)168 assert isinstance(stream, MultipartStream)169 assert stream.can_replay()170171 assert stream.content_type == f"multipart/form-data; boundary={boundary}"
15 unmodified lines
187 files = {"file": ("test.txt", value, "text/plain")}188 with mock.patch("os.urandom", return_value=os.urandom(16)):189 boundary = os.urandom(16).hex()190191 stream = encode(data={}, files=files)191 stream = encode_request(data={}, files=files)192 assert isinstance(stream, MultipartStream)193 assert stream.can_replay()194195 assert stream.content_type == f"multipart/form-data; boundary={boundary}"
29 unmodified lines
225 yield b"World"226227 fileobj: typing.Any = IteratorIO(data())228 files = {"file": fileobj}229 stream = encode(files=files, boundary=b"+++")229 stream = encode_request(files=files, boundary=b"+++")230 assert not stream.can_replay()231232 content = (233 b"--+++\r\n"
29 unmodified lines