Skip to content

Commit

Permalink
Merge pull request #25 from zopefoundation/fix-files
Browse files Browse the repository at this point in the history
Fix uploading real files
  • Loading branch information
davisagli committed Feb 3, 2017
2 parents ab4bb47 + 98ac993 commit d6756c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,8 @@ CHANGES
- Fixed browser to only follow redirects for HTTP statuses
301, 302, 303, and 307; not other 30x statuses such as 304.

- Fix passing a real file to ``add_file``.


5.1 (2017-01-31)
----------------
Expand Down
2 changes: 1 addition & 1 deletion src/zope/testbrowser/browser.py
Expand Up @@ -704,7 +704,7 @@ def add_file(self, file, content_type, filename):
raise TypeError("Can't call add_file on %s controls"
% self.mech_control.type)

if isinstance(file, io.IOBase):
if hasattr(file, 'read'):
contents = file.read()
else:
contents = file
Expand Down
21 changes: 19 additions & 2 deletions src/zope/testbrowser/tests/test_browser.py
Expand Up @@ -347,8 +347,11 @@ def test_file_upload():
Fill in the form value using add_file:
>>> browser.getControl(name='foo').add_file(
... io.BytesIO(b'sample_data'), 'text/foo', 'x.txt')
>>> import tempfile
>>> data = tempfile.NamedTemporaryFile()
>>> num_bytes = data.write(b'sample_data')
>>> position = data.seek(0)
>>> browser.getControl(name='foo').add_file(data, 'text/foo', 'x.txt')
>>> browser.getControl('OK').click() # doctest: +REPORT_NDIFF +ELLIPSIS
POST / HTTP/1.1
...
Expand All @@ -357,6 +360,7 @@ def test_file_upload():
<BLANKLINE>
sample_data
...
>>> del data
You can pass a string to add_file:
Expand All @@ -371,6 +375,19 @@ def test_file_upload():
blah blah blah
...
or a BytesIO object:
>>> browser.getControl(name='foo').add_file(
... io.BytesIO(b'sample_data'), 'text/foo', 'x.txt')
>>> browser.getControl('OK').click() # doctest: +REPORT_NDIFF +ELLIPSIS
POST / HTTP/1.1
...
Content-Disposition: form-data; name="foo"; filename="x.txt"
Content-Type: text/foo
<BLANKLINE>
sample_data
...
You can assign a value
>>> browser.getControl(name='foo').value = b'bluh bluh'
Expand Down

0 comments on commit d6756c1

Please sign in to comment.