From 26c5285a022c1ee0360c4d2df3b30367caa1aa1a Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Wed, 8 Mar 2023 01:07:37 +0100 Subject: [PATCH] Use latin-1 decoded filename in tests with FileUpload. This seems to be what browsers actually use, or how the filename is at this point in the zope publishing machinery. See https://github.com/zopefoundation/Zope/pull/1094#issuecomment-1459047253 --- news/1094.bugfix | 2 ++ plone/formwidget/namedfile/converter.py | 2 +- plone/formwidget/namedfile/widget.rst | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 news/1094.bugfix diff --git a/news/1094.bugfix b/news/1094.bugfix new file mode 100644 index 0000000..e5efd72 --- /dev/null +++ b/news/1094.bugfix @@ -0,0 +1,2 @@ +Test fix: use latin-1 decoded filename in tests with FileUpload. +[maurits] diff --git a/plone/formwidget/namedfile/converter.py b/plone/formwidget/namedfile/converter.py index 367c587..9607bca 100644 --- a/plone/formwidget/namedfile/converter.py +++ b/plone/formwidget/namedfile/converter.py @@ -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] diff --git a/plone/formwidget/namedfile/widget.rst b/plone/formwidget/namedfile/widget.rst index c8715e0..d9878ea 100644 --- a/plone/formwidget/namedfile/widget.rst +++ b/plone/formwidget/namedfile/widget.rst @@ -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:: @@ -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'