Skip to content
Myr

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

3 files+27 added−13 removed

modified:httpx/_multipart.py+4 added
1import binascii2import io3import os4import typing5from pathlib import Path6
74 unmodified lines
81 filename = Path(str(getattr(value, "name", "upload"))).name82 fileobj = value83 content_type = guess_content_type(filename)8485 if isinstance(fileobj, str) or isinstance(fileobj, io.StringIO):86 raise TypeError(f"Expected bytes or bytes-like object got: {type(fileobj)}")8788 self.filename = filename89 self.file = fileobj90 self.content_type = content_type91 self._consumed = False
118 unmodified lines
modified:httpx/_types.py+1 added-1 removed
77 unmodified lines
78ResponseContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]7980RequestData = dict8182FileContent = Union[IO[str], IO[bytes], str, bytes]82FileContent = Union[IO[bytes], bytes]83FileTypes = Union[84 # file (or text)85 FileContent,86 # (filename, file (or text))
5 unmodified lines
modified:tests/test_multipart.py+22 added-12 removed
137 unmodified lines
138 assert content == b"".join(stream)139140141def test_multipart_encode_unicode_file_contents() -> None:142 files = {"file": ("name.txt", "<únicode string>")}142 files = {"file": ("name.txt", b"<bytes content>")}143144 with mock.patch("os.urandom", return_value=os.urandom(16)):145 boundary = os.urandom(16).hex()146
2 unmodified lines
149150 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 lines
211 }212 assert content == b"".join(stream)213214215@pytest.mark.parametrize(216 "value, output",217 ((b"<bytes content>", "<bytes content>"), ("<string content>", "<string content>")),218)219def 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")}215def 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()219220 headers, stream = encode_request(data={}, files=files)
1 unmodified line
222223 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)235236237def 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)):240241 with pytest.raises(TypeError):242 encode_request(data={}, files=files) # type: ignore243244245def 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)):248249 with pytest.raises(TypeError):250 encode_request(data={}, files=files) # type: ignore251252253def 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