diff --git a/src/webob/request.py b/src/webob/request.py index 60e536f0..fd6efeb7 100644 --- a/src/webob/request.py +++ b/src/webob/request.py @@ -1570,7 +1570,7 @@ def environ_add_POST(env, data, content_type=None): data = list(data.items()) for _, v in data: - if isinstance(v, (tuple, list)): + if isinstance(v, (tuple, list)) or hasattr(v, 'filename'): has_files = True break diff --git a/tests/test_request.py b/tests/test_request.py index b4e26ddd..af56b106 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -3024,6 +3024,31 @@ def test_fileupload_mime_type_detection(self): assert "application/x-foo" in request.body.decode("ascii", str(request)) +def test_environ_add_POST_file_with_content_type(): + # For benefit of _encode_multipart which did not have adequate coverage for + # type_options + class FakeFile: + def __init__(self, filename, content_type, type_options, value): + self.filename = filename + self.type = content_type + self.type_options = type_options or {} + self.value = value + + from webob.request import environ_from_url, environ_add_POST + + env = environ_from_url("http://example.com/") + environ_add_POST( + env, + { + "sample_file": + FakeFile("test.txt", "text/plain", {"charset": "utf-8"}, "this is data"), + }, + ) + wsgi_input = env["wsgi.input"].read() + assert b"this is data" in wsgi_input + assert b'Content-type: text/plain; charset="utf-8"' in wsgi_input + + class TestRequestMultipart(object): def test_multipart_with_charset(self): from webob.request import Request @@ -3045,6 +3070,7 @@ def simpleapp(environ, start_response): return [ bytes_(x) + for x in [ "Hello world!\n", "The get is %r" % request.GET, @@ -3053,6 +3079,7 @@ def simpleapp(environ, start_response): % ( [ o + for o, _ in sorted( request.accept_language.parsed or (), key=lambda x: x[1], # sort by quality @@ -3064,6 +3091,7 @@ def simpleapp(environ, start_response): % ",".join( [ o + for o, _ in request.accept.acceptable_offers( ["application/xml", "text/html"] )