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
Provenance
- Commit
09f94ed- Tree
f1a71ed- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
ff0febb
4 files changed+24 added−24 removed
modified:httpx/_content_streams.py+1 added-1 removed
361 unmodified lines362 for chunk in self.iter_chunks():363 yield chunk364 365 366-def encode(366+def encode_request(367 content: RequestContent = None,368 data: RequestData = None,369 files: RequestFiles = None,370 json: typing.Any = None,55 unmodified linesmodified:httpx/_models.py+2 added-2 removed
11 unmodified lines12 13 import rfc398614 import rfc3986.exceptions15 16-from ._content_streams import ByteStream, ContentStream, encode, encode_response16+from ._content_streams import ByteStream, ContentStream, encode_request, encode_response17 from ._decoders import (18 SUPPORTED_DECODERS,19 ContentDecoder,20 IdentityDecoder,584 unmodified lines605 606 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)610 611 self._prepare()612 613 def _prepare(self) -> None:634 unmodified linesmodified:tests/test_content_streams.py+15 added-15 removed
1 unmodified line2 3 import pytest4 5 from httpx import StreamConsumed6-from httpx._content_streams import ContentStream, encode, encode_response6+from httpx._content_streams import ContentStream, encode_request, encode_response7 8 9 @pytest.mark.asyncio10 async def test_base_content():8 unmodified lines19 20 21 @pytest.mark.asyncio22 async 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])26 27 assert stream.can_replay()3 unmodified lines31 32 33 @pytest.mark.asyncio34 async 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])38 39 assert stream.can_replay()1 unmodified line41 assert sync_content == b"Hello, world!"42 assert async_content == b"Hello, world!"43 44 # 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])48 49 assert stream.can_replay()7 unmodified lines57 def hello_world():58 yield b"Hello, "59 yield b"world!"60 61- stream = encode(content=hello_world())61+ stream = encode_request(content=hello_world())62 content = b"".join([part for part in stream])63 64 assert not stream.can_replay()65 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}5 unmodified lines71 with pytest.raises(StreamConsumed):72 [part for part in stream]73 74 # 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])77 78 assert not stream.can_replay()79 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}5 unmodified lines85 async def hello_world():86 yield b"Hello, "87 yield b"world!"88 89- stream = encode(content=hello_world())89+ stream = encode_request(content=hello_world())90 content = b"".join([part async for part in stream])91 92 assert not stream.can_replay()93 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}5 unmodified lines99 with pytest.raises(StreamConsumed):100 [part async for part in stream]101 102 # 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])105 106 assert not stream.can_replay()107 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}1 unmodified line109 110 111 @pytest.mark.asyncio112 async 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])116 117 assert stream.can_replay()6 unmodified lines124 125 126 @pytest.mark.asyncio127 async 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])131 132 assert stream.can_replay()7 unmodified lines140 141 @pytest.mark.asyncio142 async 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])147 148 assert stream.can_replay()26 unmodified lines175 @pytest.mark.asyncio176 async 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])182 183 assert stream.can_replay()32 unmodified lines216 217 218 @pytest.mark.asyncio219 async 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])223 224 assert stream.can_replay()3 unmodified lines228 229 230 def test_invalid_argument():231 with pytest.raises(TypeError):232- encode(123) # type: ignore232+ encode_request(123) # type: ignore233 234 235 @pytest.mark.asyncio236 async 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])244 245 assert stream.can_replay()103 unmodified linesmodified:tests/test_multipart.py+6 added-6 removed
5 unmodified lines6 7 import pytest8 9 import httpx10-from httpx._content_streams import MultipartStream, encode10+from httpx._content_streams import MultipartStream, encode_request11 from httpx._utils import format_form_param12 from tests.utils import MockTransport13 14 94 unmodified lines109 110 with mock.patch("os.urandom", return_value=os.urandom(16)):111 boundary = os.urandom(16).hex()112 113- stream = encode(data=data, files=files)113+ stream = encode_request(data=data, files=files)114 assert isinstance(stream, MultipartStream)115 assert stream.can_replay()116 117 assert stream.content_type == f"multipart/form-data; boundary={boundary}"18 unmodified lines136 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()139 140- stream = encode(data={}, files=files)140+ stream = encode_request(data={}, files=files)141 assert isinstance(stream, MultipartStream)142 assert stream.can_replay()143 144 assert stream.content_type == f"multipart/form-data; boundary={boundary}"18 unmodified lines163 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()166 167- stream = encode(data={}, files=files)167+ stream = encode_request(data={}, files=files)168 assert isinstance(stream, MultipartStream)169 assert stream.can_replay()170 171 assert stream.content_type == f"multipart/form-data; boundary={boundary}"15 unmodified lines187 files = {"file": ("test.txt", value, "text/plain")}188 with mock.patch("os.urandom", return_value=os.urandom(16)):189 boundary = os.urandom(16).hex()190 191- stream = encode(data={}, files=files)191+ stream = encode_request(data={}, files=files)192 assert isinstance(stream, MultipartStream)193 assert stream.can_replay()194 195 assert stream.content_type == f"multipart/form-data; boundary={boundary}"29 unmodified lines225 yield b"World"226 227 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()231 232 content = (233 b"--+++\r\n"29 unmodified lines