Added a Unauthorized exception.
Also added a few tests related to this new exception.
This commit is contained in:
@@ -3,7 +3,8 @@ from bs4 import BeautifulSoup
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.response import text
|
||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound, abort
|
||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound, Unauthorized
|
||||
from sanic.exceptions import abort
|
||||
|
||||
|
||||
class SanicExceptionTestException(Exception):
|
||||
@@ -26,6 +27,20 @@ def exception_app():
|
||||
def handler_404(request):
|
||||
raise NotFound("OK")
|
||||
|
||||
@app.route('/401/basic')
|
||||
def handler_401_basic(request):
|
||||
raise Unauthorized("Unauthorized", "Basic", "Sanic")
|
||||
|
||||
@app.route('/401/digest')
|
||||
def handler_401_digest(request):
|
||||
challenge = {
|
||||
"qop": "auth, auth-int",
|
||||
"algorithm": "MD5",
|
||||
"nonce": "abcdef",
|
||||
"opaque": "zyxwvu",
|
||||
}
|
||||
raise Unauthorized("Unauthorized", "Digest", "Sanic", challenge)
|
||||
|
||||
@app.route('/invalid')
|
||||
def handler_invalid(request):
|
||||
raise InvalidUsage("OK")
|
||||
@@ -49,8 +64,10 @@ def exception_app():
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def test_catch_exception_list():
|
||||
app = Sanic('exception_list')
|
||||
|
||||
@app.exception([SanicExceptionTestException, NotFound])
|
||||
def exception_list(request, exception):
|
||||
return text("ok")
|
||||
@@ -91,6 +108,24 @@ def test_not_found_exception(exception_app):
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_unauthorized_exception(exception_app):
|
||||
"""Test the built-in Unauthorized exception"""
|
||||
request, response = exception_app.test_client.get('/401/basic')
|
||||
assert response.status == 401
|
||||
assert response.headers.get('WWW-Authenticate') is not None
|
||||
assert response.headers.get('WWW-Authenticate') == "Basic realm='Sanic'"
|
||||
|
||||
request, response = exception_app.test_client.get('/401/digest')
|
||||
assert response.status == 401
|
||||
|
||||
auth_header = response.headers.get('WWW-Authenticate')
|
||||
expected = ("Digest realm='Sanic', qop='auth, auth-int', algorithm='MD5', "
|
||||
"nonce='abcdef', opaque='zyxwvu'")
|
||||
|
||||
assert auth_header is not None
|
||||
assert auth_header == expected
|
||||
|
||||
|
||||
def test_handled_unhandled_exception(exception_app):
|
||||
"""Test that an exception not built into sanic is handled"""
|
||||
request, response = exception_app.test_client.get('/divide_by_zero')
|
||||
|
||||
Reference in New Issue
Block a user