Merge pull request #174 from icemac/fix-empty-file-upload
Do not omit file uploads without a file from post.
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index e2e22ab..3ef2d82 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -4,7 +4,8 @@
2.0.25 (unreleased)
-------------------
-- Nothing changed yet.
+- Fix #173: Do not omit file uploads without a file from post.
+ [Michael Howitz]
2.0.24 (2016-12-16)
diff --git a/tests/test_forms.py b/tests/test_forms.py
index 474e766..62f30e6 100644
--- a/tests/test_forms.py
+++ b/tests/test_forms.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+import cgi
import os.path
import struct
import sys
@@ -776,9 +777,12 @@
uploaded_files = [(k, v) for k, v in req.POST.items() if 'file' in k]
uploaded_files = sorted(uploaded_files)
for name, uploaded_file in uploaded_files:
- filename = to_bytes(uploaded_file.filename)
- value = to_bytes(uploaded_file.value, 'ascii')
- content_type = to_bytes(uploaded_file.type, 'ascii')
+ if isinstance(uploaded_file, cgi.FieldStorage):
+ filename = to_bytes(uploaded_file.filename)
+ value = to_bytes(uploaded_file.value, 'ascii')
+ content_type = to_bytes(uploaded_file.type, 'ascii')
+ else:
+ filename = value = content_type = b''
file_parts.append(b"""
<p>You selected '""" + filename + b"""'</p>
<p>with contents: '""" + value + b"""'</p>
diff --git a/webtest/app.py b/webtest/app.py
index 7a20186..7a7afbf 100644
--- a/webtest/app.py
+++ b/webtest/app.py
@@ -503,6 +503,10 @@
if isinstance(value, forms.File):
if value.value:
_append_file([key] + list(value.value))
+ else:
+ # If no file was uploaded simulate an empty file with no
+ # name like real browsers do:
+ _append_file([key, b'', b''])
elif isinstance(value, forms.Upload):
file_info = [key, value.filename]
if value.content is not None: