sanic/tests/test_exceptions_handler.py

159 lines
4.4 KiB
Python
Raw Permalink Normal View History

from bs4 import BeautifulSoup
2016-10-14 23:36:58 +01:00
from sanic import Sanic
from sanic.exceptions import InvalidUsage, NotFound, ServerError
from sanic.handlers import ErrorHandler
from sanic.response import text
2016-10-14 23:36:58 +01:00
2018-12-30 11:18:06 +00:00
exception_handler_app = Sanic("test_exception_handler")
2016-10-14 23:36:58 +01:00
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/1")
2016-10-14 23:36:58 +01:00
def handler_1(request):
raise InvalidUsage("OK")
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/2")
2016-10-14 23:36:58 +01:00
def handler_2(request):
raise ServerError("OK")
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/3")
2016-10-14 23:36:58 +01:00
def handler_3(request):
raise NotFound("OK")
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/4")
2017-01-13 00:54:34 +00:00
def handler_4(request):
2018-12-30 11:18:06 +00:00
foo = bar # noqa -- F821 undefined name 'bar' is done to throw exception
2017-01-13 00:54:34 +00:00
return text(foo)
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/5")
def handler_5(request):
class CustomServerError(ServerError):
pass
2018-12-30 11:18:06 +00:00
raise CustomServerError("Custom server error")
2018-12-30 11:18:06 +00:00
@exception_handler_app.route("/6/<arg:int>")
def handler_6(request, arg):
try:
foo = 1 / arg
except Exception as e:
raise e from ValueError("{}".format(arg))
return text(foo)
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():
2018-12-30 11:18:06 +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():
2018-12-30 11:18:06 +00:00
request, response = exception_handler_app.test_client.get("/2")
2016-10-14 23:36:58 +01:00
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"
2016-10-14 23:36:58 +01:00
def test_not_found_exception_handler():
2018-12-30 11:18:06 +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():
2018-12-30 11:18:06 +00:00
request, response = exception_handler_app.test_client.get("/random")
2016-10-14 23:36:58 +01:00
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"
2017-01-13 00:54:34 +00:00
def test_html_traceback_output_in_debug_mode():
2018-12-30 11:18:06 +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
2018-12-30 11:18:06 +00:00
soup = BeautifulSoup(response.body, "html.parser")
2017-01-13 00:54:34 +00:00
html = str(soup)
2018-12-30 11:18:06 +00:00
assert "response = handler(request, *args, **kwargs)" in html
assert "handler_4" in html
assert "foo = bar" in html
2017-01-13 00:54:34 +00:00
2018-12-30 11:18:06 +00:00
summary_text = " ".join(soup.select(".summary")[0].text.split())
2017-01-13 00:54:34 +00:00
assert (
2018-12-30 11:18:06 +00:00
"NameError: name 'bar' " "is not defined while handling path /4"
) == summary_text
def test_inherited_exception_handler():
2018-12-30 11:18:06 +00:00
request, response = exception_handler_app.test_client.get("/5")
assert response.status == 200
def test_chained_exception_handler():
request, response = exception_handler_app.test_client.get(
2018-12-30 11:18:06 +00:00
"/6/0", debug=True
)
assert response.status == 500
2018-12-30 11:18:06 +00:00
soup = BeautifulSoup(response.body, "html.parser")
html = str(soup)
2018-12-30 11:18:06 +00:00
assert "response = handler(request, *args, **kwargs)" in html
assert "handler_6" in html
assert "foo = 1 / arg" in html
assert "ValueError" in html
assert "The above exception was the direct cause" in html
2018-12-30 11:18:06 +00:00
summary_text = " ".join(soup.select(".summary")[0].text.split())
assert (
2018-12-30 11:18:06 +00:00
"ZeroDivisionError: division by zero " "while handling path /6/0"
) == summary_text
def test_exception_handler_lookup():
class CustomError(Exception):
pass
class CustomServerError(ServerError):
pass
def custom_error_handler():
pass
def server_error_handler():
pass
def import_error_handler():
pass
try:
ModuleNotFoundError
2018-10-22 21:25:38 +01:00
except Exception:
2018-12-30 11:18:06 +00:00
class ModuleNotFoundError(ImportError):
pass
handler = ErrorHandler()
handler.add(ImportError, import_error_handler)
handler.add(CustomError, custom_error_handler)
handler.add(ServerError, server_error_handler)
assert handler.lookup(ImportError()) == import_error_handler
assert handler.lookup(ModuleNotFoundError()) == import_error_handler
assert handler.lookup(CustomError()) == custom_error_handler
2018-12-30 11:18:06 +00:00
assert handler.lookup(ServerError("Error")) == server_error_handler
assert handler.lookup(CustomServerError("Error")) == server_error_handler
# once again to ensure there is no caching bug
assert handler.lookup(ImportError()) == import_error_handler
assert handler.lookup(ModuleNotFoundError()) == import_error_handler
assert handler.lookup(CustomError()) == custom_error_handler
2018-12-30 11:18:06 +00:00
assert handler.lookup(ServerError("Error")) == server_error_handler
assert handler.lookup(CustomServerError("Error")) == server_error_handler