Skip to content

Commit

Permalink
File uploading tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
kedder committed Mar 5, 2013
1 parent b6f55a5 commit 402aa94
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
26 changes: 20 additions & 6 deletions src/zope/testbrowser/browser2.py
Expand Up @@ -18,6 +18,7 @@
import sys
import re
import time
import io

from zope.interface import implementer

Expand Down Expand Up @@ -113,6 +114,14 @@ def _findByLabel(self, label, forms, include_subcontrols=False):
break
return found

def _findByName(self, name, forms):
found = []
for f in forms:
if name in f.fields:
found.extend([(c, f) for c in f.fields[name]])
return found


def _findAllControls(self, forms, include_subcontrols=False):
for f in forms:
for controls in f.fields.values():
Expand Down Expand Up @@ -227,14 +236,19 @@ def value(self, value):
self._control.value = value

def add_file(self, file, content_type, filename):
# TODO
if not self.mech_control.type == 'file':
if self.type != 'file':
raise TypeError("Can't call add_file on %s controls"
% self.mech_control.type)
if isinstance(file, str):
import cStringIO
file = cStringIO.StringIO(file)
self.mech_control.add_file(file, content_type, filename)

if isinstance(file, io.IOBase):
contents = file.read()
else:
contents = file

# XXX: webtest relies on mimetypes.guess_type to get mime type of
# upload file and doesn't let to set it explicitly, so we are ignoring
# content_type parameter here. If it is still unacceptable, consider using mock.object to force mimetypes to return "right" type.
self._form[self.name] = webtest.forms.Upload(filename, contents)

def clear(self):
# TODO
Expand Down
23 changes: 10 additions & 13 deletions src/zope/testbrowser/tests/test_browser.py
Expand Up @@ -250,47 +250,44 @@ def test_submit_duplicate_name():
def test_file_upload():
"""
>>> browser = Browser()
>>> app = TestApp()
>>> browser = Browser(application=app)
When given a form with a file-upload
>>> browser.open('''\
>>> app.set_next_response('''\
... <html><body>
... <form action="." method="post" enctype="multipart/form-data">
... <input name="foo" type="file" />
... <input type="submit" value="OK" />
... </form></body></html>
... ''') # doctest: +ELLIPSIS
>>> browser.open('http://localhost/')
GET / HTTP/1.1
...
Fill in the form value using add_file:
>>> browser.getControl(name='foo').add_file(
... io.BytesIO(b'sample_data'), 'text/foo', 'x.foo')
... io.BytesIO(b'sample_data'), 'text/foo', 'x.txt')
>>> browser.getControl('OK').click() # doctest: +REPORT_NDIFF +ELLIPSIS
POST / HTTP/1.1
...
Content-type: multipart/form-data; ...
Content-disposition: form-data; name="foo"; filename="x.foo"
Content-type: text/foo
Content-Disposition: form-data; name="foo"; filename="x.txt"
Content-Type: text/plain
<BLANKLINE>
sample_data
...
You can pass a string to add_file:
>>> browser.getControl(name='foo').add_file(
... 'blah blah blah', 'text/blah', 'x.blah')
... 'blah blah blah', 'text/csv', 'x.csv')
>>> browser.getControl('OK').click() # doctest: +REPORT_NDIFF +ELLIPSIS
POST / HTTP/1.1
...
Content-type: multipart/form-data; ...
Content-disposition: form-data; name="foo"; filename="x.blah"
Content-type: text/blah
Content-disposition: form-data; name="foo"; filename="x.csv"
Content-type: text/csv
<BLANKLINE>
blah blah blah
...
Expand Down

0 comments on commit 402aa94

Please sign in to comment.