Skip to content
Myr

Fix Content-Length for unicode file contents with multipart (#1537)

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 68cf1ff · Browse files · 2 files+25 added−6 removed
This commit

State

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

Diff stats

2 files+25 added−6 removed

  • tests:+23 added−0 removed
  • other:+2 added−6 removed

2 files+25 added−6 removed

modified:httpx/_multipart.py+2 added-6 removed
38 unmodified lines
39 return self._headers4041 def render_data(self) -> bytes:42 if not hasattr(self, "_data"):43 self._data = (44 self.value45 if isinstance(self.value, bytes)46 else self.value.encode("utf-8")47 )43 self._data = to_bytes(self.value)4445 return self._data4647 def get_length(self) -> int:
35 unmodified lines
83 def get_length(self) -> int:84 headers = self.render_headers()8586 if isinstance(self.file, (str, bytes)):91 return len(headers) + len(self.file)87 return len(headers) + len(to_bytes(self.file))8889 # Let's do our best not to read `file` into memory.90 try:91 file_length = peek_filelike_length(self.file)
109 unmodified lines
modified:tests/test_multipart.py+23 added
131 unmodified lines
132 }133 assert content == b"".join(stream)134135136def test_multipart_encode_unicode_file_contents() -> None:137 files = {"file": ("name.txt", "<únicode string>")}138139 with mock.patch("os.urandom", return_value=os.urandom(16)):140 boundary = os.urandom(16).hex()141142 headers, stream = encode_request(files=files)143 assert isinstance(stream, typing.Iterable)144145 content = (146 '--{0}\r\nContent-Disposition: form-data; name="file";'147 ' filename="name.txt"\r\n'148 "Content-Type: text/plain\r\n\r\n<únicode string>\r\n"149 "--{0}--\r\n"150 "".format(boundary).encode("utf-8")151 )152 assert headers == {153 "Content-Type": f"multipart/form-data; boundary={boundary}",154 "Content-Length": str(len(content)),155 }156 assert content == b"".join(stream)157158159def test_multipart_encode_files_allows_filenames_as_none() -> None:160 files = {"file": (None, io.BytesIO(b"<file content>"))}161 with mock.patch("os.urandom", return_value=os.urandom(16)):162 boundary = os.urandom(16).hex()
128 unmodified lines