test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
193 lines · 4.9 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
The walk stopped after 200 commits: lines older than 49f3e96 point at it.
Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
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"""9Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 10why, one step back10from werkzeug.exceptions import Forbidden, InternalServerError, HTTPException, NotFoundMerge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 11–20why, one step back11import flask121314def test_error_handler_no_match(app, client):15 class CustomException(Exception):16 pass1718 @app.errorhandler(CustomException)19 def custom_exception_handler(e):20 assert isinstance(e, CustomException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 21why, one step back21 return "custom"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 22–26why, one step back2223 @app.errorhandler(500)24 def handle_500(e):25 return type(e).__name__26Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 27why, one step back27 @app.route("/custom")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 28–30why, one step back28 def custom_test():29 raise CustomException()30Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 31why, one step back31 @app.route("/keyerror")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 32–35why, one step back32 def key_error():33 raise KeyError()3435 app.testing = FalseMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 36–37why, one step back36 assert client.get("/custom").data == b"custom"37 assert client.get("/keyerror").data == b"KeyError"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 38–52why, one step back383940def test_error_handler_subclass(app):41 class ParentException(Exception):42 pass4344 class ChildExceptionUnregistered(ParentException):45 pass4647 class ChildExceptionRegistered(ParentException):48 pass4950 @app.errorhandler(ParentException)51 def parent_exception_handler(e):52 assert isinstance(e, ParentException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 53why, one step back53 return "parent"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 54–57why, one step back5455 @app.errorhandler(ChildExceptionRegistered)56 def child_exception_handler(e):57 assert isinstance(e, ChildExceptionRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 58why, one step back58 return "child-registered"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 59why, one step back59Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 60why, one step back60 @app.route("/parent")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 61–63why, one step back61 def parent_test():62 raise ParentException()63Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 64why, one step back64 @app.route("/child-unregistered")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 65–67why, one step back65 def unregistered_test():66 raise ChildExceptionUnregistered()67Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 68why, one step back68 @app.route("/child-registered")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 69–73why, one step back69 def registered_test():70 raise ChildExceptionRegistered()7172 c = app.test_client()73Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 74–76why, one step back74 assert c.get("/parent").data == b"parent"75 assert c.get("/child-unregistered").data == b"parent"76 assert c.get("/child-registered").data == b"child-registered"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 77–88why, one step back777879def test_error_handler_http_subclass(app):80 class ForbiddenSubclassRegistered(Forbidden):81 pass8283 class ForbiddenSubclassUnregistered(Forbidden):84 pass8586 @app.errorhandler(403)87 def code_exception_handler(e):88 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 89why, one step back89 return "forbidden"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 90–93why, one step back9091 @app.errorhandler(ForbiddenSubclassRegistered)92 def subclass_exception_handler(e):93 assert isinstance(e, ForbiddenSubclassRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 94why, one step back94 return "forbidden-registered"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 95why, one step back95Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 96why, one step back96 @app.route("/forbidden")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 97–99why, one step back97 def forbidden_test():98 raise Forbidden()99Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 100why, one step back100 @app.route("/forbidden-registered")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 101–103why, one step back101 def registered_test():102 raise ForbiddenSubclassRegistered()103Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 104why, one step back104 @app.route("/forbidden-unregistered")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 105–109why, one step back105 def unregistered_test():106 raise ForbiddenSubclassUnregistered()107108 c = app.test_client()109Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 110–112why, one step back110 assert c.get("/forbidden").data == b"forbidden"111 assert c.get("/forbidden-unregistered").data == b"forbidden"112 assert c.get("/forbidden-registered").data == b"forbidden-registered"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 113–115why, one step back113114115def test_error_handler_blueprint(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 116why, one step back116 bp = flask.Blueprint("bp", __name__)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 117–119why, one step back117118 @bp.errorhandler(500)119 def bp_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 120why, one step back120 return "bp-error"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 121why, one step back121Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 122why, one step back122 @bp.route("/error")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 123–127why, one step back123 def bp_test():124 raise InternalServerError()125126 @app.errorhandler(500)127 def app_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 128why, one step back128 return "app-error"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 129why, one step back129Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 130why, one step back130 @app.route("/error")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 131–133why, one step back131 def app_test():132 raise InternalServerError()133Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 134why, one step back134 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 135–137why, one step back135136 c = app.test_client()137Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 138–139why, one step back138 assert c.get("/error").data == b"app-error"139 assert c.get("/bp/error").data == b"bp-error"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 140–142why, one step back140141142def test_default_error_handler():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 143why, one step back143 bp = flask.Blueprint("bp", __name__)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 144–148why, one step back144145 @bp.errorhandler(HTTPException)146 def bp_exception_handler(e):147 assert isinstance(e, HTTPException)148 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 149why, one step back149 return "bp-default"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 150–153why, one step back150151 @bp.errorhandler(Forbidden)152 def bp_exception_handler(e):153 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 154why, one step back154 return "bp-forbidden"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 155why, one step back155Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 156why, one step back156 @bp.route("/undefined")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 157–159why, one step back157 def bp_registered_test():158 raise NotFound()159Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 160why, one step back160 @bp.route("/forbidden")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 161–169why, one step back161 def bp_forbidden_test():162 raise Forbidden()163164 app = flask.Flask(__name__)165166 @app.errorhandler(HTTPException)167 def catchall_errorhandler(e):168 assert isinstance(e, HTTPException)169 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 170why, one step back170 return "default"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 171–174why, one step back171172 @app.errorhandler(Forbidden)173 def catchall_errorhandler(e):174 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 175why, one step back175 return "forbidden"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 176why, one step back176Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 177why, one step back177 @app.route("/forbidden")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 178–180why, one step back178 def forbidden():179 raise Forbidden()180Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 181–184why, one step back181 @app.route("/slash/")182 def slash():183 return "slash"184Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 185why, one step back185 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 186–187why, one step back186187 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 188–191why, one step back188 assert c.get("/bp/undefined").data == b"bp-default"189 assert c.get("/bp/forbidden").data == b"bp-forbidden"190 assert c.get("/undefined").data == b"default"191 assert c.get("/forbidden").data == b"forbidden"Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 192–193why, one step back192 # Don't handle RequestRedirect raised when adding slash.193 assert c.get("/slash", follow_redirects=True).data == b"slash"