Skip to content

Commit

Permalink
Fix empty stream file uploads when testing
Browse files Browse the repository at this point in the history
There should be a Data event with more_data=False, sent to the encoder
as is now the case. This was ok for files that weren't empty as a Data
event with more_data=True was sent thereby updating the state and
working past this issue.

This includes a test from @pandabear.

The other test fix is required as the file should be bytes.
  • Loading branch information
pgjones committed Aug 12, 2023
1 parent 3712c7b commit a3a313a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Version 2.3.7
Unreleased

- Use ``flit_core`` instead of ``setuptools`` as build backend.
- Fix parsing of multipart bodies.
- Fix parsing of multipart bodies. :issue:`2734`
Adjust index of last newline in data start. :issue:`2761`
- ``_plain_int`` and ``_plain_float`` strip whitespace before type
enforcement. :issue:`2734`
- Fix empty file streaming when testing. :issue:`2740`


Version 2.3.6
Expand Down
1 change: 1 addition & 0 deletions src/werkzeug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def write_binary(s: bytes) -> int:
chunk = reader(16384)

if not chunk:
write_binary(encoder.send_event(Data(data=chunk, more_data=False)))
break

write_binary(encoder.send_event(Data(data=chunk, more_data=True)))
Expand Down
19 changes: 18 additions & 1 deletion tests/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,23 @@ def test_environ_builder_unicode_file_mix():
files["f"].close()


def test_environ_builder_empty_file():
f = FileStorage(BytesIO(rb""), "empty.txt")
d = MultiDict(dict(f=f, s=""))
stream, length, boundary = stream_encode_multipart(d)
_, form, files = parse_form_data(
{
"wsgi.input": stream,
"CONTENT_LENGTH": str(length),
"CONTENT_TYPE": f'multipart/form-data; boundary="{boundary}"',
}
)
assert form["s"] == ""
assert files["f"].read() == rb""
stream.close()
files["f"].close()


def test_create_environ():
env = create_environ("/foo?bar=baz", "http://example.org/")
expected = {
Expand Down Expand Up @@ -409,7 +426,7 @@ def test_file_closing():

class SpecialInput:
def read(self, size):
return ""
return b""

def close(self):
closed.append(self)
Expand Down

0 comments on commit a3a313a

Please sign in to comment.