Skip to content

Commit

Permalink
Merge pull request #348 from zopefoundation/fix-308
Browse files Browse the repository at this point in the history
Fix #308: default-zpublisher-encoding breaks rendering
  • Loading branch information
Michael Howitz committed Oct 4, 2018
2 parents c47d4bf + 26e43b9 commit 171dd9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -56,6 +56,10 @@ Bugfixes
when reading request bodies not encoded as application/x-www-form-urlencoded
or multipart/form-data.

- Prevent breaking page rendering when setting `default-zpublisher-encoding`
in `zope.conf` on Python 2.
(`#308 <https://github.com/zopefoundation/Zope/issue/308>`_)


4.0b5 (2018-05-18)
------------------
Expand Down
3 changes: 3 additions & 0 deletions src/Zope2/Startup/datatypes.py
Expand Up @@ -206,6 +206,9 @@ def default_zpublisher_encoding(value):
# so a module-level call to getConfiguration in any of them
# results in getting config data structure without the necessary
# value in it.
if PY2:
# unicode is not acceptable as encoding in HTTP headers:
value = str(value)
from ZPublisher import Converters, HTTPRequest, HTTPResponse
Converters.default_encoding = value
HTTPRequest.default_encoding = value
Expand Down
36 changes: 22 additions & 14 deletions src/Zope2/Startup/tests/test_schema.py
Expand Up @@ -12,13 +12,15 @@
#
##############################################################################

import codecs
import os
import io
import tempfile
import unittest

import six
import ZConfig
import ZPublisher.HTTPRequest
import Zope2.Startup.datatypes

from Zope2.Startup.options import ZopeWSGIOptions

Expand All @@ -38,15 +40,19 @@ def getSchema():

class WSGIStartupTestCase(unittest.TestCase):

def setUp(self):
self.default_encoding = ZPublisher.HTTPRequest.default_encoding

def tearDown(self):
Zope2.Startup.datatypes.default_zpublisher_encoding(
self.default_encoding)

def load_config_text(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
text = text.replace("<<INSTANCE_HOME>>", TEMPNAME)
if six.PY2:
sio = io.BytesIO(text)
else:
sio = io.StringIO(text)
sio = io.StringIO(text)

os.mkdir(TEMPNAME)
os.mkdir(TEMPVAR)
Expand All @@ -62,13 +68,12 @@ def test_load_config_template(self):
import Zope2.utilities
base = os.path.dirname(Zope2.utilities.__file__)
fn = os.path.join(base, "skel", "etc", "wsgi.conf.in")
f = open(fn)
text = f.read()
f.close()
with codecs.open(fn, encoding='utf-8') as f:
text = f.read()
self.load_config_text(text)

def test_environment(self):
conf, handler = self.load_config_text("""\
conf, handler = self.load_config_text(u"""\
# instancehome is here since it's required
instancehome <<INSTANCE_HOME>>
<environment>
Expand All @@ -82,7 +87,7 @@ def test_environment(self):
items, [("FEARFACTORY", "rocks"), ("NSYNC", "doesnt")])

def test_zodb_db(self):
conf, handler = self.load_config_text("""\
conf, handler = self.load_config_text(u"""\
instancehome <<INSTANCE_HOME>>
<zodb_db main>
<filestorage>
Expand All @@ -96,26 +101,29 @@ def test_zodb_db(self):
self.assertEqual(conf.databases[0].config.cache_size, 5000)

def test_max_conflict_retries_default(self):
conf, handler = self.load_config_text("""\
conf, handler = self.load_config_text(u"""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.max_conflict_retries, 3)

def test_max_conflict_retries_explicit(self):
conf, handler = self.load_config_text("""\
conf, handler = self.load_config_text(u"""\
instancehome <<INSTANCE_HOME>>
max-conflict-retries 15
""")
self.assertEqual(conf.max_conflict_retries, 15)

def test_default_zpublisher_encoding(self):
conf, dummy = self.load_config_text("""\
conf, dummy = self.load_config_text(u"""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.default_zpublisher_encoding, 'utf-8')

conf, dummy = self.load_config_text("""\
conf, dummy = self.load_config_text(u"""\
instancehome <<INSTANCE_HOME>>
default-zpublisher-encoding iso-8859-15
""")
self.assertEqual(conf.default_zpublisher_encoding, 'iso-8859-15')
self.assertEqual(
ZPublisher.HTTPRequest.default_encoding, 'iso-8859-15')
self.assertEqual(type(ZPublisher.HTTPRequest.default_encoding), str)

0 comments on commit 171dd9f

Please sign in to comment.