Skip to content

Commit

Permalink
Merge pull request #60 from plone/maurits-widget-tests-latin-1
Browse files Browse the repository at this point in the history
Use latin-1 decoded filename in tests with FileUpload.
  • Loading branch information
mauritsvanrees authored Mar 10, 2023
2 parents 244293b + 26c5285 commit ddbeeec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions news/1094.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test fix: use latin-1 decoded filename in tests with FileUpload.
[maurits]
2 changes: 1 addition & 1 deletion plone/formwidget/namedfile/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def b64encode_file(filename, data):

def b64decode_file(value):
if isinstance(value, str):
value = value.encode("utf8")
value = value.encode("utf-8")
filename, data = value.split(b";")

filename = filename.split(b":")[1]
Expand Down
8 changes: 5 additions & 3 deletions plone/formwidget/namedfile/widget.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,13 @@ content type::

>>> myfile = six.BytesIO(b'File upload contents.')
>>> # \xc3\xb8 is UTF-8 for a small letter o with slash
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('utf8'),
>>> # Still, we must decode it using latin-1 according to HTTP/1.1.
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('latin-1'),
... headers={'Content-Type': 'text/x-dummy'})
>>> file_obj = file_converter.toFieldValue(FileUpload(aFieldStorage))
>>> file_obj.data
b'File upload contents.'
>>> file_obj.filename.encode('utf8')
>>> file_obj.filename.encode('utf-8')
b'rand\xc3\xb8m.txt'

Content type from headers sent by browser should be ignored::
Expand Down Expand Up @@ -640,7 +641,8 @@ filename too::

>>> myfile = six.BytesIO(b'File upload contents.')
>>> # \xc3\xb8 is UTF-8 for a small letter o with slash
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('utf8'),
>>> # Still, we must decode it using latin-1 according to HTTP/1.1.
>>> aFieldStorage = FieldStorageStub(myfile, filename=b'rand\xc3\xb8m.txt'.decode('latin-1'),
... headers={'Content-Type': 'text/x-dummy'})
>>> bytes_file_converter.toFieldValue(FileUpload(aFieldStorage))
b'filenameb64:cmFuZMO4bS50eHQ=;datab64:RmlsZSB1cGxvYWQgY29udGVudHMu'
Expand Down

0 comments on commit ddbeeec

Please sign in to comment.