Skip to content

Commit

Permalink
Test that JSON responses are actually JSON
Browse files Browse the repository at this point in the history
We also test out the custom formatter that allows the user to change how
the JSON is formatted for the exception.
  • Loading branch information
digitalresistor committed Apr 13, 2016
1 parent a66ce9e commit e195dc0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pyramid/tests/test_httpexceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,38 @@ def test__default_app_iter_with_comment_html(self):
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}')
Expand Down

0 comments on commit e195dc0

Please sign in to comment.