Skip to content

Commit a3a313a

Browse files
committed
Fix empty stream file uploads when testing
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.
1 parent 3712c7b commit a3a313a

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ Version 2.3.7
66
Unreleased
77

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

1415

1516
Version 2.3.6

src/werkzeug/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def write_binary(s: bytes) -> int:
136136
chunk = reader(16384)
137137

138138
if not chunk:
139+
write_binary(encoder.send_event(Data(data=chunk, more_data=False)))
139140
break
140141

141142
write_binary(encoder.send_event(Data(data=chunk, more_data=True)))

tests/test_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ def test_environ_builder_unicode_file_mix():
357357
files["f"].close()
358358

359359

360+
def test_environ_builder_empty_file():
361+
f = FileStorage(BytesIO(rb""), "empty.txt")
362+
d = MultiDict(dict(f=f, s=""))
363+
stream, length, boundary = stream_encode_multipart(d)
364+
_, form, files = parse_form_data(
365+
{
366+
"wsgi.input": stream,
367+
"CONTENT_LENGTH": str(length),
368+
"CONTENT_TYPE": f'multipart/form-data; boundary="{boundary}"',
369+
}
370+
)
371+
assert form["s"] == ""
372+
assert files["f"].read() == rb""
373+
stream.close()
374+
files["f"].close()
375+
376+
360377
def test_create_environ():
361378
env = create_environ("/foo?bar=baz", "http://example.org/")
362379
expected = {
@@ -409,7 +426,7 @@ def test_file_closing():
409426

410427
class SpecialInput:
411428
def read(self, size):
412-
return ""
429+
return b""
413430

414431
def close(self):
415432
closed.append(self)

0 commit comments

Comments
 (0)