parent
5b309831ec
commit
025589ee76
63 changed files with 3784 additions and 3459 deletions
|
|
@ -7,40 +7,34 @@ tests.test_user_error_handler
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from werkzeug.exceptions import (
|
||||
Forbidden,
|
||||
InternalServerError,
|
||||
HTTPException,
|
||||
NotFound
|
||||
)
|
||||
from werkzeug.exceptions import Forbidden, InternalServerError, HTTPException, NotFound
|
||||
import flask
|
||||
|
||||
|
||||
def test_error_handler_no_match(app, client):
|
||||
|
||||
class CustomException(Exception):
|
||||
pass
|
||||
|
||||
@app.errorhandler(CustomException)
|
||||
def custom_exception_handler(e):
|
||||
assert isinstance(e, CustomException)
|
||||
return 'custom'
|
||||
return "custom"
|
||||
|
||||
@app.errorhandler(500)
|
||||
def handle_500(e):
|
||||
return type(e).__name__
|
||||
|
||||
@app.route('/custom')
|
||||
@app.route("/custom")
|
||||
def custom_test():
|
||||
raise CustomException()
|
||||
|
||||
@app.route('/keyerror')
|
||||
@app.route("/keyerror")
|
||||
def key_error():
|
||||
raise KeyError()
|
||||
|
||||
app.testing = False
|
||||
assert client.get('/custom').data == b'custom'
|
||||
assert client.get('/keyerror').data == b'KeyError'
|
||||
assert client.get("/custom").data == b"custom"
|
||||
assert client.get("/keyerror").data == b"KeyError"
|
||||
|
||||
|
||||
def test_error_handler_subclass(app):
|
||||
|
|
@ -56,30 +50,30 @@ def test_error_handler_subclass(app):
|
|||
@app.errorhandler(ParentException)
|
||||
def parent_exception_handler(e):
|
||||
assert isinstance(e, ParentException)
|
||||
return 'parent'
|
||||
return "parent"
|
||||
|
||||
@app.errorhandler(ChildExceptionRegistered)
|
||||
def child_exception_handler(e):
|
||||
assert isinstance(e, ChildExceptionRegistered)
|
||||
return 'child-registered'
|
||||
return "child-registered"
|
||||
|
||||
@app.route('/parent')
|
||||
@app.route("/parent")
|
||||
def parent_test():
|
||||
raise ParentException()
|
||||
|
||||
@app.route('/child-unregistered')
|
||||
@app.route("/child-unregistered")
|
||||
def unregistered_test():
|
||||
raise ChildExceptionUnregistered()
|
||||
|
||||
@app.route('/child-registered')
|
||||
@app.route("/child-registered")
|
||||
def registered_test():
|
||||
raise ChildExceptionRegistered()
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
assert c.get('/parent').data == b'parent'
|
||||
assert c.get('/child-unregistered').data == b'parent'
|
||||
assert c.get('/child-registered').data == b'child-registered'
|
||||
assert c.get("/parent").data == b"parent"
|
||||
assert c.get("/child-unregistered").data == b"parent"
|
||||
assert c.get("/child-registered").data == b"child-registered"
|
||||
|
||||
|
||||
def test_error_handler_http_subclass(app):
|
||||
|
|
@ -92,78 +86,78 @@ def test_error_handler_http_subclass(app):
|
|||
@app.errorhandler(403)
|
||||
def code_exception_handler(e):
|
||||
assert isinstance(e, Forbidden)
|
||||
return 'forbidden'
|
||||
return "forbidden"
|
||||
|
||||
@app.errorhandler(ForbiddenSubclassRegistered)
|
||||
def subclass_exception_handler(e):
|
||||
assert isinstance(e, ForbiddenSubclassRegistered)
|
||||
return 'forbidden-registered'
|
||||
return "forbidden-registered"
|
||||
|
||||
@app.route('/forbidden')
|
||||
@app.route("/forbidden")
|
||||
def forbidden_test():
|
||||
raise Forbidden()
|
||||
|
||||
@app.route('/forbidden-registered')
|
||||
@app.route("/forbidden-registered")
|
||||
def registered_test():
|
||||
raise ForbiddenSubclassRegistered()
|
||||
|
||||
@app.route('/forbidden-unregistered')
|
||||
@app.route("/forbidden-unregistered")
|
||||
def unregistered_test():
|
||||
raise ForbiddenSubclassUnregistered()
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
assert c.get('/forbidden').data == b'forbidden'
|
||||
assert c.get('/forbidden-unregistered').data == b'forbidden'
|
||||
assert c.get('/forbidden-registered').data == b'forbidden-registered'
|
||||
assert c.get("/forbidden").data == b"forbidden"
|
||||
assert c.get("/forbidden-unregistered").data == b"forbidden"
|
||||
assert c.get("/forbidden-registered").data == b"forbidden-registered"
|
||||
|
||||
|
||||
def test_error_handler_blueprint(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
bp = flask.Blueprint("bp", __name__)
|
||||
|
||||
@bp.errorhandler(500)
|
||||
def bp_exception_handler(e):
|
||||
return 'bp-error'
|
||||
return "bp-error"
|
||||
|
||||
@bp.route('/error')
|
||||
@bp.route("/error")
|
||||
def bp_test():
|
||||
raise InternalServerError()
|
||||
|
||||
@app.errorhandler(500)
|
||||
def app_exception_handler(e):
|
||||
return 'app-error'
|
||||
return "app-error"
|
||||
|
||||
@app.route('/error')
|
||||
@app.route("/error")
|
||||
def app_test():
|
||||
raise InternalServerError()
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/bp')
|
||||
app.register_blueprint(bp, url_prefix="/bp")
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
assert c.get('/error').data == b'app-error'
|
||||
assert c.get('/bp/error').data == b'bp-error'
|
||||
assert c.get("/error").data == b"app-error"
|
||||
assert c.get("/bp/error").data == b"bp-error"
|
||||
|
||||
|
||||
def test_default_error_handler():
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
bp = flask.Blueprint("bp", __name__)
|
||||
|
||||
@bp.errorhandler(HTTPException)
|
||||
def bp_exception_handler(e):
|
||||
assert isinstance(e, HTTPException)
|
||||
assert isinstance(e, NotFound)
|
||||
return 'bp-default'
|
||||
return "bp-default"
|
||||
|
||||
@bp.errorhandler(Forbidden)
|
||||
def bp_exception_handler(e):
|
||||
assert isinstance(e, Forbidden)
|
||||
return 'bp-forbidden'
|
||||
return "bp-forbidden"
|
||||
|
||||
@bp.route('/undefined')
|
||||
@bp.route("/undefined")
|
||||
def bp_registered_test():
|
||||
raise NotFound()
|
||||
|
||||
@bp.route('/forbidden')
|
||||
@bp.route("/forbidden")
|
||||
def bp_forbidden_test():
|
||||
raise Forbidden()
|
||||
|
||||
|
|
@ -173,14 +167,14 @@ def test_default_error_handler():
|
|||
def catchall_errorhandler(e):
|
||||
assert isinstance(e, HTTPException)
|
||||
assert isinstance(e, NotFound)
|
||||
return 'default'
|
||||
return "default"
|
||||
|
||||
@app.errorhandler(Forbidden)
|
||||
def catchall_errorhandler(e):
|
||||
assert isinstance(e, Forbidden)
|
||||
return 'forbidden'
|
||||
return "forbidden"
|
||||
|
||||
@app.route('/forbidden')
|
||||
@app.route("/forbidden")
|
||||
def forbidden():
|
||||
raise Forbidden()
|
||||
|
||||
|
|
@ -188,12 +182,12 @@ def test_default_error_handler():
|
|||
def slash():
|
||||
return "slash"
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/bp')
|
||||
app.register_blueprint(bp, url_prefix="/bp")
|
||||
|
||||
c = app.test_client()
|
||||
assert c.get('/bp/undefined').data == b'bp-default'
|
||||
assert c.get('/bp/forbidden').data == b'bp-forbidden'
|
||||
assert c.get('/undefined').data == b'default'
|
||||
assert c.get('/forbidden').data == b'forbidden'
|
||||
assert c.get("/bp/undefined").data == b"bp-default"
|
||||
assert c.get("/bp/forbidden").data == b"bp-forbidden"
|
||||
assert c.get("/undefined").data == b"default"
|
||||
assert c.get("/forbidden").data == b"forbidden"
|
||||
# Don't handle RequestRedirect raised when adding slash.
|
||||
assert c.get("/slash", follow_redirects=True).data == b"slash"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue