Skip to content

Commit

Permalink
Always rewind files on multipart uploads. (#2065)
Browse files Browse the repository at this point in the history
* Test for multipart POST same file twice.

* Always rewind files on multipart uploads

* Linting
  • Loading branch information
tomchristie committed Feb 4, 2022
1 parent 2814fd3 commit 0088253
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions httpx/_multipart.py
Expand Up @@ -113,7 +113,6 @@ def __init__(self, name: str, value: FileTypes) -> None:
self.filename = filename
self.file = fileobj
self.headers = headers
self._consumed = False

def get_length(self) -> int:
headers = self.render_headers()
Expand Down Expand Up @@ -158,9 +157,8 @@ def render_data(self) -> typing.Iterator[bytes]:
yield self._data
return

if self._consumed: # pragma: nocover
if hasattr(self.file, "seek"):
self.file.seek(0)
self._consumed = True

chunk = self.file.read(self.CHUNK_SIZE)
while chunk:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_multipart.py
@@ -1,6 +1,7 @@
import cgi
import io
import os
import tempfile
import typing
from unittest import mock

Expand Down Expand Up @@ -339,6 +340,25 @@ def data() -> typing.Iterator[bytes]:
assert content == b"".join(stream)


def test_multipart_rewinds_files():
with tempfile.TemporaryFile() as upload:
upload.write(b"Hello, world!")

transport = httpx.MockTransport(echo_request_content)
client = httpx.Client(transport=transport)

files = {"file": upload}
response = client.post("http://127.0.0.1:8000/", files=files)
assert response.status_code == 200
assert b"\r\nHello, world!\r\n" in response.content

# POSTing the same file instance a second time should have the same content.
files = {"file": upload}
response = client.post("http://127.0.0.1:8000/", files=files)
assert response.status_code == 200
assert b"\r\nHello, world!\r\n" in response.content


class TestHeaderParamHTML5Formatting:
def test_unicode(self):
param = format_form_param("filename", "n\u00e4me")
Expand Down

0 comments on commit 0088253

Please sign in to comment.