Skip to content
Myr

Always rewind files on multipart uploads. (#2065)

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

* Test for multipart POST same file twice. * Always rewind files on multipart uploads * Linting

authored by Tom Christie · committed by GitHub · parent 2814fd3 · Browse files · 2 files+21 added−3 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+21 added−3 removed

  • tests:+20 added−0 removed
  • other:+1 added−3 removed

2 files+21 added−3 removed

modified:httpx/_multipart.py+1 added-3 removed
111 unmodified lines
112113 self.filename = filename114 self.file = fileobj115 self.headers = headers116 self._consumed = False116117 def get_length(self) -> int:118 headers = self.render_headers()119
36 unmodified lines
156 # Already rendered.157 yield self._data158 return159161 if self._consumed: # pragma: nocover160 if hasattr(self.file, "seek"):161 self.file.seek(0)163 self._consumed = True162163 chunk = self.file.read(self.CHUNK_SIZE)164 while chunk:165 yield to_bytes(chunk)
69 unmodified lines
modified:tests/test_multipart.py+20 added
1import cgi2import io3import os4import tempfile5import typing6from unittest import mock78import pytest
330 unmodified lines
339 }340 assert content == b"".join(stream)341342343def test_multipart_rewinds_files():344 with tempfile.TemporaryFile() as upload:345 upload.write(b"Hello, world!")346347 transport = httpx.MockTransport(echo_request_content)348 client = httpx.Client(transport=transport)349350 files = {"file": upload}351 response = client.post("http://127.0.0.1:8000/", files=files)352 assert response.status_code == 200353 assert b"\r\nHello, world!\r\n" in response.content354355 # POSTing the same file instance a second time should have the same content.356 files = {"file": upload}357 response = client.post("http://127.0.0.1:8000/", files=files)358 assert response.status_code == 200359 assert b"\r\nHello, world!\r\n" in response.content360361362class TestHeaderParamHTML5Formatting:363 def test_unicode(self):364 param = format_form_param("filename", "n\u00e4me")365 assert param == b'filename="n\xc3\xa4me"'
12 unmodified lines