Skip to content

Commit

Permalink
Always convert an uploaded file to a string, don't pass files just be…
Browse files Browse the repository at this point in the history
…cause they're real files.
  • Loading branch information
MatthewWilkes committed Feb 3, 2017
1 parent f6f605c commit 98ac993
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,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
Original file line number Diff line number Diff line change
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 98ac993

Please sign in to comment.