2014-11-14 15:43:21 +00:00
|
|
|
"""
|
|
|
|
Tests for the sprockets.mixins.json_error package
|
|
|
|
|
|
|
|
"""
|
2014-11-14 20:27:13 +00:00
|
|
|
import json
|
2014-11-14 15:43:21 +00:00
|
|
|
|
2014-11-14 20:27:13 +00:00
|
|
|
from sprockets.mixins import json_error
|
|
|
|
from tornado import testing, web
|
2014-11-14 15:43:21 +00:00
|
|
|
|
2014-11-14 20:27:13 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2014-11-18 15:03:19 +00:00
|
|
|
def get_message(self):
|
|
|
|
return 'Too much Foo'
|
|
|
|
|
2014-11-14 20:27:13 +00:00
|
|
|
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('/')
|
2014-11-18 15:03:19 +00:00
|
|
|
expected = {'message': 'Unexpected Error', 'type': 'Bad Request'}
|
2014-11-14 20:27:13 +00:00
|
|
|
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)
|