sprockets.mixins.json_error/tests.py
AWeberChrisMcGuire f21a6a583f Clean up write_error.
This commit alters write_error to pull the message from a
get_message function of the error, else it using a default Unexpected
Error string.
2014-11-18 10:03:19 -05:00

78 lines
2 KiB
Python

"""
Tests for the sprockets.mixins.json_error package
"""
import json
from sprockets.mixins import json_error
from tornado import testing, web
class HTTPErrorRequestHandler(
json_error.JsonErrorMixin, web.RequestHandler):
def get(self):
raise web.HTTPError(400, 'Error Reason')
class CustomExceptionRequestHandler(
json_error.JsonErrorMixin, web.RequestHandler):
class FailureError(Exception):
status_code = 400
error_type = 'FailureError'
documentation_url = 'http://www.example.com'
def get_message(self):
return 'Too much Foo'
def get(self):
raise self.FailureError()
class UnexpectedErrorRequestHandler(
json_error.JsonErrorMixin, web.RequestHandler):
def get(self):
raise Exception()
class TestHTTPError(testing.AsyncHTTPTestCase):
def get_app(self):
return web.Application([('/', HTTPErrorRequestHandler)])
def test_tornado_thrown_exception(self):
response = self.fetch('/')
expected = {'message': 'Unexpected Error', 'type': 'Bad Request'}
self.assertEqual(json.loads(response.body), expected)
class TestCustomExceptions(testing.AsyncHTTPTestCase):
def get_app(self):
return web.Application([('/', CustomExceptionRequestHandler)])
def test_tornado_custom_exception(self):
response = self.fetch('/')
expected = {
'message': 'Too much Foo',
'type': 'FailureError',
'documentation_url': 'http://www.example.com',
}
self.assertEqual(json.loads(response.body), expected)
class TestUnexpectedError(testing.AsyncHTTPTestCase):
def get_app(self):
return web.Application([('/', UnexpectedErrorRequestHandler)])
def test_unexpected_exception(self):
response = self.fetch('/')
expected = {
'message': 'Unexpected Error',
'type': 'Internal Server Error'
}
self.assertEqual(json.loads(response.body), expected)