2016-10-14 11:37:40 +01:00
|
|
|
from sanic import Sanic
|
2016-10-14 23:36:58 +01:00
|
|
|
from sanic.response import text
|
2016-10-14 11:37:40 +01:00
|
|
|
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
2016-10-14 23:36:58 +01:00
|
|
|
from sanic.utils import sanic_endpoint_test
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
exception_app = Sanic('test_exceptions')
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
@exception_app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
@exception_app.route('/error')
|
|
|
|
def handler_error(request):
|
|
|
|
raise ServerError("OK")
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
@exception_app.route('/404')
|
|
|
|
def handler_404(request):
|
|
|
|
raise NotFound("OK")
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
@exception_app.route('/invalid')
|
|
|
|
def handler_invalid(request):
|
|
|
|
raise InvalidUsage("OK")
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
def test_no_exception():
|
|
|
|
request, response = sanic_endpoint_test(exception_app)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
def test_server_error_exception():
|
|
|
|
request, response = sanic_endpoint_test(exception_app, uri='/error')
|
|
|
|
assert response.status == 500
|
2016-10-14 11:37:40 +01:00
|
|
|
|
|
|
|
|
2016-10-14 23:36:58 +01:00
|
|
|
def test_invalid_usage_exception():
|
|
|
|
request, response = sanic_endpoint_test(exception_app, uri='/invalid')
|
|
|
|
assert response.status == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_found_exception():
|
|
|
|
request, response = sanic_endpoint_test(exception_app, uri='/404')
|
|
|
|
assert response.status == 404
|
|
|
|
|
2016-10-14 11:37:40 +01:00
|
|
|
|