enforce-upload-files-binary-type (#1736)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* enforce-upload-files-binary-type * Update test_multipart.py
authored by Amin Alaee · committed by GitHub · parent 50790af · Browse files · 3 files·+27 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Tom Christie <tom@tomchristie.com>
Diff stats
3 files+27 added−13 removed
- tests:+22 added−12 removed
- other:+5 added−1 removed
Provenance
- Commit
20e66d2- Tree
b221d51- Author
Amin Alaee <mohammadamin.alaee@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
50790af
3 files changed+27 added−13 removed
modified:httpx/_multipart.py+4 added
1 import binascii2+import io3 import os4 import typing5 from pathlib import Path6 74 unmodified lines81 filename = Path(str(getattr(value, "name", "upload"))).name82 fileobj = value83 content_type = guess_content_type(filename)84 85+ if isinstance(fileobj, str) or isinstance(fileobj, io.StringIO):86+ raise TypeError(f"Expected bytes or bytes-like object got: {type(fileobj)}")87+88 self.filename = filename89 self.file = fileobj90 self.content_type = content_type91 self._consumed = False118 unmodified linesmodified:httpx/_types.py+1 added-1 removed
77 unmodified lines78 ResponseContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]79 80 RequestData = dict81 82-FileContent = Union[IO[str], IO[bytes], str, bytes]82+FileContent = Union[IO[bytes], bytes]83 FileTypes = Union[84 # file (or text)85 FileContent,86 # (filename, file (or text))5 unmodified linesmodified:tests/test_multipart.py+22 added-12 removed
137 unmodified lines138 assert content == b"".join(stream)139 140 141 def test_multipart_encode_unicode_file_contents() -> None:142- files = {"file": ("name.txt", "<únicode string>")}142+ files = {"file": ("name.txt", b"<bytes content>")}143 144 with mock.patch("os.urandom", return_value=os.urandom(16)):145 boundary = os.urandom(16).hex()146 2 unmodified lines149 150 content = (151 '--{0}\r\nContent-Disposition: form-data; name="file";'152 ' filename="name.txt"\r\n'153- "Content-Type: text/plain\r\n\r\n<únicode string>\r\n"153+ "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"154 "--{0}--\r\n"155 "".format(boundary).encode("utf-8")156 )157 assert headers == {53 unmodified lines211 }212 assert content == b"".join(stream)213 214 215-@pytest.mark.parametrize(216- "value, output",217- ((b"<bytes content>", "<bytes content>"), ("<string content>", "<string content>")),218-)219-def test_multipart_encode_files_allows_bytes_or_str_content(220- value: typing.Union[str, bytes], output: str221-) -> None:222- files = {"file": ("test.txt", value, "text/plain")}215+def test_multipart_encode_files_allows_bytes_content() -> None:216+ files = {"file": ("test.txt", b"<bytes content>", "text/plain")}217 with mock.patch("os.urandom", return_value=os.urandom(16)):218 boundary = os.urandom(16).hex()219 220 headers, stream = encode_request(data={}, files=files)1 unmodified line222 223 content = (224 '--{0}\r\nContent-Disposition: form-data; name="file"; '225 'filename="test.txt"\r\n'232- "Content-Type: text/plain\r\n\r\n{1}\r\n"226+ "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"227 "--{0}--\r\n"234- "".format(boundary, output).encode("ascii")228+ "".format(boundary).encode("ascii")229 )230 assert headers == {231 "Content-Type": f"multipart/form-data; boundary={boundary}",232 "Content-Length": str(len(content)),233 }234 assert content == b"".join(stream)235 236 237+def test_multipart_encode_files_raises_exception_with_str_content() -> None:238+ files = {"file": ("test.txt", "<bytes content>", "text/plain")}239+ with mock.patch("os.urandom", return_value=os.urandom(16)):240+241+ with pytest.raises(TypeError):242+ encode_request(data={}, files=files) # type: ignore243+244+245+def test_multipart_encode_files_raises_exception_with_StringIO_content() -> None:246+ files = {"file": ("test.txt", io.StringIO("content"), "text/plain")}247+ with mock.patch("os.urandom", return_value=os.urandom(16)):248+249+ with pytest.raises(TypeError):250+ encode_request(data={}, files=files) # type: ignore251+252+253 def test_multipart_encode_non_seekable_filelike() -> None:254 """255 Test that special readable but non-seekable filelike objects are supported,256 at the cost of reading them into memory at most once.49 unmodified lines