test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
195 lines · 4.8 KiB · Python
Last changed by
Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
History of this fileTake it
The walk stopped after 200 commits: lines older than f05aeea point at it.
Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 1why, one step back1# -*- coding: utf-8 -*-Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
lines 2–9why, one step back2"""3tests.test_user_error_handler4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56:copyright: © 2010 by the Pallets team.7:license: BSD, see LICENSE for more details.8"""9resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 10–15why, one step back10from werkzeug.exceptions import (11 Forbidden,12 InternalServerError,13 HTTPException,14 NotFound15 )Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 16–18why, one step back16import flask1718resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 19why, one step back19def test_error_handler_no_match(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 20–40why, one step back2021 class CustomException(Exception):22 pass2324 @app.errorhandler(CustomException)25 def custom_exception_handler(e):26 assert isinstance(e, CustomException)27 return 'custom'2829 @app.errorhandler(500)30 def handle_500(e):31 return type(e).__name__3233 @app.route('/custom')34 def custom_test():35 raise CustomException()3637 @app.route('/keyerror')38 def key_error():39 raise KeyError()40resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 41–43why, one step back41 app.testing = False42 assert client.get('/custom').data == b'custom'43 assert client.get('/keyerror').data == b'KeyError'Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 44–45why, one step back4445resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 46why, one step back46def test_error_handler_subclass(app):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 47–84why, one step back47 class ParentException(Exception):48 pass4950 class ChildExceptionUnregistered(ParentException):51 pass5253 class ChildExceptionRegistered(ParentException):54 pass5556 @app.errorhandler(ParentException)57 def parent_exception_handler(e):58 assert isinstance(e, ParentException)59 return 'parent'6061 @app.errorhandler(ChildExceptionRegistered)62 def child_exception_handler(e):63 assert isinstance(e, ChildExceptionRegistered)64 return 'child-registered'6566 @app.route('/parent')67 def parent_test():68 raise ParentException()6970 @app.route('/child-unregistered')71 def unregistered_test():72 raise ChildExceptionUnregistered()7374 @app.route('/child-registered')75 def registered_test():76 raise ChildExceptionRegistered()7778 c = app.test_client()7980 assert c.get('/parent').data == b'parent'81 assert c.get('/child-unregistered').data == b'parent'82 assert c.get('/child-registered').data == b'child-registered'8384resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 85why, one step back85def test_error_handler_http_subclass(app):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 86–120why, one step back86 class ForbiddenSubclassRegistered(Forbidden):87 pass8889 class ForbiddenSubclassUnregistered(Forbidden):90 pass9192 @app.errorhandler(403)93 def code_exception_handler(e):94 assert isinstance(e, Forbidden)95 return 'forbidden'9697 @app.errorhandler(ForbiddenSubclassRegistered)98 def subclass_exception_handler(e):99 assert isinstance(e, ForbiddenSubclassRegistered)100 return 'forbidden-registered'101102 @app.route('/forbidden')103 def forbidden_test():104 raise Forbidden()105106 @app.route('/forbidden-registered')107 def registered_test():108 raise ForbiddenSubclassRegistered()109110 @app.route('/forbidden-unregistered')111 def unregistered_test():112 raise ForbiddenSubclassUnregistered()113114 c = app.test_client()115116 assert c.get('/forbidden').data == b'forbidden'117 assert c.get('/forbidden-unregistered').data == b'forbidden'118 assert c.get('/forbidden-registered').data == b'forbidden-registered'119120resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 121why, one step back121def test_error_handler_blueprint(app):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 122–145why, one step back122 bp = flask.Blueprint('bp', __name__)123124 @bp.errorhandler(500)125 def bp_exception_handler(e):126 return 'bp-error'127128 @bp.route('/error')129 def bp_test():130 raise InternalServerError()131132 @app.errorhandler(500)133 def app_exception_handler(e):134 return 'app-error'135136 @app.route('/error')137 def app_test():138 raise InternalServerError()139140 app.register_blueprint(bp, url_prefix='/bp')141142 c = app.test_client()143144 assert c.get('/error').data == b'app-error'145 assert c.get('/bp/error').data == b'bp-error'resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 146–195why, one step back146147148def test_default_error_handler():149 bp = flask.Blueprint('bp', __name__)150151 @bp.errorhandler(HTTPException)152 def bp_exception_handler(e):153 assert isinstance(e, HTTPException)154 assert isinstance(e, NotFound)155 return 'bp-default'156157 @bp.errorhandler(Forbidden)158 def bp_exception_handler(e):159 assert isinstance(e, Forbidden)160 return 'bp-forbidden'161162 @bp.route('/undefined')163 def bp_registered_test():164 raise NotFound()165166 @bp.route('/forbidden')167 def bp_forbidden_test():168 raise Forbidden()169170 app = flask.Flask(__name__)171172 @app.errorhandler(HTTPException)173 def catchall_errorhandler(e):174 assert isinstance(e, HTTPException)175 assert isinstance(e, NotFound)176 return 'default'177178 @app.errorhandler(Forbidden)179 def catchall_errorhandler(e):180 assert isinstance(e, Forbidden)181 return 'forbidden'182183 @app.route('/forbidden')184 def forbidden():185 raise Forbidden()186187 app.register_blueprint(bp, url_prefix='/bp')188189 c = app.test_client()190 assert c.get('/bp/undefined').data == b'bp-default'191 assert c.get('/bp/forbidden').data == b'bp-forbidden'192 assert c.get('/undefined').data == b'default'193 assert c.get('/forbidden').data == b'forbidden'194195