Skip to content

Commit

Permalink
Adjust processInputs to deal with native strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed May 28, 2017
1 parent 6fbf7e0 commit b0cce9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/ZPublisher/Converters.py
Expand Up @@ -34,6 +34,8 @@ def field2string(v):
return v.read()
elif six.PY2 and isinstance(v, text_type):
return v.encode(default_encoding)
elif six.PY3 and isinstance(v, binary_type):
return v.decode(default_encoding)
else:
return str(v)

Expand Down
23 changes: 17 additions & 6 deletions src/ZPublisher/tests/testHTTPRequest.py
Expand Up @@ -15,6 +15,7 @@
import sys
import unittest

from six import PY2
from zExceptions import NotFound
from zope.component import provideAdapter
from zope.i18n.interfaces import IUserPreferredLanguages
Expand Down Expand Up @@ -246,11 +247,21 @@ def test_processInputs_w_simple_marshalling(self):
self._onlyTaintedformHoldsTaintedStrings(req)

def test_processInputs_w_unicode_conversions(self):
inputs = (('ustring:ustring:utf8', 'test\xc2\xae'),
('utext:utext:utf8', 'test\xc2\xae\ntest\xc2\xae\n'),
('utokens:utokens:utf8', 'test\xc2\xae test\xc2\xae'),
('ulines:ulines:utf8', 'test\xc2\xae\ntest\xc2\xae'),
('nouconverter:string:utf8', 'test\xc2\xae'))
# This tests native strings, which mean something different
# under Python 2 / 3.
if PY2:
reg_char = '\xc2\xae'
else:
reg_char = '\xae'

inputs = (('ustring:ustring:utf8', 'test' + reg_char),
('utext:utext:utf8', 'test' + reg_char +
'\ntest' + reg_char + '\n'),
('utokens:utokens:utf8', 'test' + reg_char +
' test' + reg_char),
('ulines:ulines:utf8', 'test' + reg_char +
'\ntest' + reg_char),
('nouconverter:string:utf8', 'test' + reg_char))
req = self._processInputs(inputs)

formkeys = list(req.form.keys())
Expand All @@ -265,7 +276,7 @@ def test_processInputs_w_unicode_conversions(self):
self.assertEqual(req['ulines'], [u'test\u00AE', u'test\u00AE'])

# expect a utf-8 encoded version
self.assertEqual(req['nouconverter'], 'test\xc2\xae')
self.assertEqual(req['nouconverter'], 'test' + reg_char)

self._noTaintedValues(req)
self._onlyTaintedformHoldsTaintedStrings(req)
Expand Down

0 comments on commit b0cce9b

Please sign in to comment.