diff --git a/tests/test_forms.py b/tests/test_forms.py index 9f75548..c426001 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -12,7 +12,7 @@ from webob import Request from webtest.debugapp import DebugApp from webtest.compat import to_bytes -from webtest.forms import NoValue, Submit +from webtest.forms import NoValue, Submit, Upload from tests.compat import unittest from tests.compat import u @@ -714,9 +714,11 @@ def get_files_page(self, req): 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') file_parts.append(b"""
You selected '""" + filename + b"""'
with contents: '""" + value + b"""'
+with content type: '""" + content_type + b"""'
""") return b''.join(file_parts) @@ -751,7 +753,7 @@ class MultipleUploadFileApp(SingleUploadFileApp): class TestFileUpload(unittest.TestCase): - def assertFile(self, name, contents, display): + def assertFile(self, name, contents, display, content_type=None): if isinstance(name, six.binary_type): text_name = name.decode('ascii') else: @@ -764,6 +766,9 @@ def assertFile(self, name, contents, display): text_contents = contents self.assertIn("with contents: '" + text_contents + "'
", display, display) + if content_type: + self.assertIn("with content type: '" + content_type + "'
", + display, display) def test_no_uploads_error(self): app = webtest.TestApp(SingleUploadFileApp()) @@ -814,6 +819,21 @@ def test_file_upload_with_filename_and_contents(self): display = single_form.submit("button") self.assertFile(uploaded_file_name, uploaded_file_contents, display) + def test_file_upload_with_content_type(self): + uploaded_file_name = os.path.join(os.path.dirname(__file__), + "__init__.py") + with open(uploaded_file_name, 'rb') as f: + uploaded_file_contents = f.read() + app = webtest.TestApp(SingleUploadFileApp()) + res = app.get('/') + single_form = res.forms["file_upload_form"] + single_form["file-field"].value = Upload(uploaded_file_name, + uploaded_file_contents, + 'text/x-custom-type') + display = single_form.submit("button") + self.assertFile(uploaded_file_name, uploaded_file_contents, display, + content_type='text/x-custom-type') + def test_file_upload_binary(self): binary_data = struct.pack(str('255h'), *range(0, 255)) app = webtest.TestApp(UploadBinaryApp()) diff --git a/webtest/forms.py b/webtest/forms.py index c30fdf6..3269aa2 100644 --- a/webtest/forms.py +++ b/webtest/forms.py @@ -38,7 +38,7 @@ def __iter__(self): yield self.filename if self.content: yield self.content - # XXX: do we need to yield self.content_type here? + yield self.content_type # TODO: do we handle the case when we need to get # contents ourselves?