summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBert JW Regeer <bertjw@regeer.org>2016-04-12 20:14:46 -0600
committerBert JW Regeer <bertjw@regeer.org>2016-04-12 20:21:53 -0600
commite195dc00d376ca30bb7d5009227c1eb7528ecdbf (patch)
tree44337ec722331ee1e8982f9d969271e75cccd7a2
parenta66ce9e5330abbe5de85b4de49f905293804be0a (diff)
downloadpyramid-e195dc00d376ca30bb7d5009227c1eb7528ecdbf.tar.gz
pyramid-e195dc00d376ca30bb7d5009227c1eb7528ecdbf.tar.bz2
pyramid-e195dc00d376ca30bb7d5009227c1eb7528ecdbf.zip
Test that JSON responses are actually JSON
We also test out the custom formatter that allows the user to change how the JSON is formatted for the exception.
-rw-r--r--pyramid/tests/test_httpexceptions.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/pyramid/tests/test_httpexceptions.py b/pyramid/tests/test_httpexceptions.py
index 223f8ab35..361006f29 100644
--- a/pyramid/tests/test_httpexceptions.py
+++ b/pyramid/tests/test_httpexceptions.py
@@ -303,6 +303,38 @@ class TestHTTPException(unittest.TestCase):
body = list(exc(environ, start_response))[0]
self.assertTrue(b'<!-- comment &amp; comment -->' in body)
+ def test__default_app_iter_with_comment_json(self):
+ cls = self._getTargetSubclass()
+ exc = cls(comment='comment & comment')
+ environ = _makeEnviron()
+ environ['HTTP_ACCEPT'] = 'application/json'
+ start_response = DummyStartResponse()
+ body = list(exc(environ, start_response))[0]
+ import json
+ retval = json.loads(body.decode('UTF-8'))
+ self.assertEqual(retval['code'], '200 OK')
+ self.assertEqual(retval['title'], 'OK')
+
+ def test__default_app_iter_with_custom_json(self):
+ def json_formatter(status, body, title, environ):
+ return {'message': body,
+ 'code': status,
+ 'title': title,
+ 'custom': environ['CUSTOM_VARIABLE']
+ }
+ cls = self._getTargetSubclass()
+ exc = cls(comment='comment', json_formatter=json_formatter)
+ environ = _makeEnviron()
+ environ['HTTP_ACCEPT'] = 'application/json'
+ environ['CUSTOM_VARIABLE'] = 'custom!'
+ start_response = DummyStartResponse()
+ body = list(exc(environ, start_response))[0]
+ import json
+ retval = json.loads(body.decode('UTF-8'))
+ self.assertEqual(retval['code'], '200 OK')
+ self.assertEqual(retval['title'], 'OK')
+ self.assertEqual(retval['custom'], 'custom!')
+
def test_custom_body_template(self):
cls = self._getTargetSubclass()
exc = cls(body_template='${REQUEST_METHOD}')