summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-09-24 22:23:23 +0000
committerGerrit Code Review <review@openstack.org>2014-09-24 22:23:23 +0000
commitffcb64b2e279f63564b0a39066d7a251eba0186e (patch)
tree92ee0fc5d01b84107bfc2976e77b34f24f6e4e7a
parent548ac35e3bc85ce8a872c4ec9adf4e39557187ce (diff)
parent8d503286205cf07fad8f35cc18bc4078d57aa0e3 (diff)
downloadpecan-ffcb64b2e279f63564b0a39066d7a251eba0186e.tar.gz
Merge "When detecting non-content for HTTP 204, properly catch UnicodeDecodeError."
-rw-r--r--pecan/core.py12
-rw-r--r--pecan/tests/test_base.py8
2 files changed, 17 insertions, 3 deletions
diff --git a/pecan/core.py b/pecan/core.py
index 9174c50..23a427d 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -594,9 +594,15 @@ class PecanBase(object):
else:
text = None
if state.response.charset:
- # `response.text` cannot be accessed without a charset
- # (because we don't know which encoding to use)
- text = state.response.text
+ # `response.text` cannot be accessed without a valid
+ # charset (because we don't know which encoding to use)
+ try:
+ text = state.response.text
+ except UnicodeDecodeError:
+ # If a valid charset is not specified, don't bother
+ # trying to guess it (because there's obviously
+ # content, so we know this shouldn't be a 204)
+ pass
if not any((state.response.body, text)):
state.response.status = 204
diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py
index 35f48ab..e6ddb05 100644
--- a/pecan/tests/test_base.py
+++ b/pecan/tests/test_base.py
@@ -68,6 +68,10 @@ class TestEmptyContent(PecanTestCase):
def explicit_json_body(self):
response.json_body = {'foo': 'bar'}
+ @expose()
+ def non_unicode(self):
+ return chr(0xc0)
+
return TestApp(Pecan(RootController()))
def test_empty_index(self):
@@ -77,6 +81,10 @@ class TestEmptyContent(PecanTestCase):
self.assertEqual(r.headers['Content-Length'], '0')
self.assertEqual(len(r.body), 0)
+ def test_index_with_non_unicode(self):
+ r = self.app_.get('/non_unicode/')
+ self.assertEqual(r.status_int, 200)
+
def test_explicit_body(self):
r = self.app_.get('/explicit_body/')
self.assertEqual(r.status_int, 200)