Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure Upload.content_type is not ignored #88

Merged
merged 1 commit into from
Sep 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"""
<p>You selected '""" + filename + b"""'</p>
<p>with contents: '""" + value + b"""'</p>
<p>with content type: '""" + content_type + b"""'</p>
""")
return b''.join(file_parts)

Expand Down Expand Up @@ -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:
Expand All @@ -764,6 +766,9 @@ def assertFile(self, name, contents, display):
text_contents = contents
self.assertIn("<p>with contents: '" + text_contents + "'</p>",
display, display)
if content_type:
self.assertIn("<p>with content type: '" + content_type + "'</p>",
display, display)

def test_no_uploads_error(self):
app = webtest.TestApp(SingleUploadFileApp())
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion webtest/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down