Merge pull request #814 from Frzk/forbidden-exception

Added a Forbidden exception
This commit is contained in:
Raphael Deem 2017-06-30 18:11:23 -07:00 committed by GitHub
commit 421f78f3e6
2 changed files with 16 additions and 1 deletions

View File

@ -194,6 +194,11 @@ class ContentRangeError(SanicException):
} }
@add_status_code(403)
class Forbidden(SanicException):
pass
class InvalidRangeType(ContentRangeError): class InvalidRangeType(ContentRangeError):
pass pass

View File

@ -4,7 +4,7 @@ from bs4 import BeautifulSoup
from sanic import Sanic from sanic import Sanic
from sanic.response import text from sanic.response import text
from sanic.exceptions import InvalidUsage, ServerError, NotFound, Unauthorized from sanic.exceptions import InvalidUsage, ServerError, NotFound, Unauthorized
from sanic.exceptions import abort from sanic.exceptions import Forbidden, abort
class SanicExceptionTestException(Exception): class SanicExceptionTestException(Exception):
@ -27,6 +27,10 @@ def exception_app():
def handler_404(request): def handler_404(request):
raise NotFound("OK") raise NotFound("OK")
@app.route('/403')
def handler_403(request):
raise Forbidden("Forbidden")
@app.route('/401/basic') @app.route('/401/basic')
def handler_401_basic(request): def handler_401_basic(request):
raise Unauthorized("Unauthorized", "Basic", "Sanic") raise Unauthorized("Unauthorized", "Basic", "Sanic")
@ -108,6 +112,12 @@ def test_not_found_exception(exception_app):
assert response.status == 404 assert response.status == 404
def test_forbidden_exception(exception_app):
"""Test the built-in Forbidden exception"""
request, response = exception_app.test_client.get('/403')
assert response.status == 403
def test_unauthorized_exception(exception_app): def test_unauthorized_exception(exception_app):
"""Test the built-in Unauthorized exception""" """Test the built-in Unauthorized exception"""
request, response = exception_app.test_client.get('/401/basic') request, response = exception_app.test_client.get('/401/basic')