2016-10-14 23:36:58 +01:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import text
|
|
|
|
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
2017-01-13 00:54:34 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2016-10-14 23:36:58 +01:00
|
|
|
|
|
|
|
exception_handler_app = Sanic('test_exception_handler')
|
|
|
|
|
|
|
|
|
|
|
|
@exception_handler_app.route('/1')
|
|
|
|
def handler_1(request):
|
|
|
|
raise InvalidUsage("OK")
|
|
|
|
|
|
|
|
|
|
|
|
@exception_handler_app.route('/2')
|
|
|
|
def handler_2(request):
|
|
|
|
raise ServerError("OK")
|
|
|
|
|
|
|
|
|
|
|
|
@exception_handler_app.route('/3')
|
|
|
|
def handler_3(request):
|
|
|
|
raise NotFound("OK")
|
|
|
|
|
|
|
|
|
2017-01-13 00:54:34 +00:00
|
|
|
@exception_handler_app.route('/4')
|
|
|
|
def handler_4(request):
|
|
|
|
foo = bar
|
|
|
|
return text(foo)
|
|
|
|
|
|
|
|
|
2017-01-31 05:29:16 +00:00
|
|
|
@exception_handler_app.route('/5')
|
|
|
|
def handler_5(request):
|
|
|
|
class CustomServerError(ServerError):
|
2017-02-09 01:59:34 +00:00
|
|
|
status_code=200
|
2017-01-31 05:29:16 +00:00
|
|
|
raise CustomServerError('Custom server error')
|
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
@exception_handler_app.exception(NotFound, ServerError)
|
|
|
|
def handler_exception(request, exception):
|
|
|
|
return text("OK")
|
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_usage_exception_handler():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get('/1')
|
2016-10-14 23:36:58 +01:00
|
|
|
assert response.status == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_error_exception_handler():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get('/2')
|
2016-10-14 23:36:58 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_found_exception_handler():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get('/3')
|
2016-10-14 23:36:58 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_text_exception__handler():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get('/random')
|
2016-10-14 23:36:58 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
2017-01-13 00:54:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_html_traceback_output_in_debug_mode():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get(
|
|
|
|
'/4', debug=True)
|
2017-01-13 00:54:34 +00:00
|
|
|
assert response.status == 500
|
|
|
|
soup = BeautifulSoup(response.body, 'html.parser')
|
|
|
|
html = str(soup)
|
|
|
|
|
|
|
|
assert 'response = handler(request, *args, **kwargs)' in html
|
|
|
|
assert 'handler_4' in html
|
|
|
|
assert 'foo = bar' in html
|
|
|
|
|
|
|
|
summary_text = " ".join(soup.select('.summary')[0].text.split())
|
|
|
|
assert (
|
|
|
|
"NameError: name 'bar' "
|
|
|
|
"is not defined while handling uri /4") == summary_text
|
2017-01-31 05:29:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_inherited_exception_handler():
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = exception_handler_app.test_client.get('/5')
|
2017-01-31 05:29:16 +00:00
|
|
|
assert response.status == 200
|