| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | from werkzeug.exceptions import ( | 
					
						
							|  |  |  |     Forbidden, | 
					
						
							|  |  |  |     InternalServerError, | 
					
						
							|  |  |  |     HTTPException, | 
					
						
							|  |  |  |     NotFound | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | import flask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_no_match(app, client): | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class CustomException(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(CustomException) | 
					
						
							|  |  |  |     def custom_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, CustomException) | 
					
						
							|  |  |  |         return 'custom' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def handle_500(e): | 
					
						
							|  |  |  |         return type(e).__name__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/custom') | 
					
						
							|  |  |  |     def custom_test(): | 
					
						
							|  |  |  |         raise CustomException() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/keyerror') | 
					
						
							|  |  |  |     def key_error(): | 
					
						
							|  |  |  |         raise KeyError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     app.testing = False | 
					
						
							|  |  |  |     assert client.get('/custom').data == b'custom' | 
					
						
							|  |  |  |     assert client.get('/keyerror').data == b'KeyError' | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_subclass(app): | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ParentException(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ChildExceptionUnregistered(ParentException): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ChildExceptionRegistered(ParentException): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ParentException) | 
					
						
							|  |  |  |     def parent_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ParentException) | 
					
						
							|  |  |  |         return 'parent' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ChildExceptionRegistered) | 
					
						
							|  |  |  |     def child_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ChildExceptionRegistered) | 
					
						
							|  |  |  |         return 'child-registered' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/parent') | 
					
						
							|  |  |  |     def parent_test(): | 
					
						
							|  |  |  |         raise ParentException() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/child-unregistered') | 
					
						
							|  |  |  |     def unregistered_test(): | 
					
						
							|  |  |  |         raise ChildExceptionUnregistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/child-registered') | 
					
						
							|  |  |  |     def registered_test(): | 
					
						
							|  |  |  |         raise ChildExceptionRegistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     assert c.get('/parent').data == b'parent' | 
					
						
							|  |  |  |     assert c.get('/child-unregistered').data == b'parent' | 
					
						
							|  |  |  |     assert c.get('/child-registered').data == b'child-registered' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_http_subclass(app): | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ForbiddenSubclassRegistered(Forbidden): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ForbiddenSubclassUnregistered(Forbidden): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(403) | 
					
						
							|  |  |  |     def code_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, Forbidden) | 
					
						
							|  |  |  |         return 'forbidden' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ForbiddenSubclassRegistered) | 
					
						
							|  |  |  |     def subclass_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ForbiddenSubclassRegistered) | 
					
						
							|  |  |  |         return 'forbidden-registered' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/forbidden') | 
					
						
							|  |  |  |     def forbidden_test(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/forbidden-registered') | 
					
						
							|  |  |  |     def registered_test(): | 
					
						
							|  |  |  |         raise ForbiddenSubclassRegistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.route('/forbidden-unregistered') | 
					
						
							|  |  |  |     def unregistered_test(): | 
					
						
							|  |  |  |         raise ForbiddenSubclassUnregistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     assert c.get('/forbidden').data == b'forbidden' | 
					
						
							|  |  |  |     assert c.get('/forbidden-unregistered').data == b'forbidden' | 
					
						
							|  |  |  |     assert c.get('/forbidden-registered').data == b'forbidden-registered' | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_blueprint(app): | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     bp = flask.Blueprint('bp', __name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(500) | 
					
						
							|  |  |  |     def bp_exception_handler(e): | 
					
						
							|  |  |  |         return 'bp-error' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.route('/error') | 
					
						
							|  |  |  |     def bp_test(): | 
					
						
							|  |  |  |         raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def app_exception_handler(e): | 
					
						
							|  |  |  |         return 'app-error' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/error') | 
					
						
							|  |  |  |     def app_test(): | 
					
						
							|  |  |  |         raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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' | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_default_error_handler(): | 
					
						
							|  |  |  |     bp = flask.Blueprint('bp', __name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(HTTPException) | 
					
						
							|  |  |  |     def bp_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, HTTPException) | 
					
						
							|  |  |  |         assert isinstance(e, NotFound) | 
					
						
							|  |  |  |         return 'bp-default' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(Forbidden) | 
					
						
							|  |  |  |     def bp_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, Forbidden) | 
					
						
							|  |  |  |         return 'bp-forbidden' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.route('/undefined') | 
					
						
							|  |  |  |     def bp_registered_test(): | 
					
						
							|  |  |  |         raise NotFound() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @bp.route('/forbidden') | 
					
						
							|  |  |  |     def bp_forbidden_test(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app = flask.Flask(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(HTTPException) | 
					
						
							|  |  |  |     def catchall_errorhandler(e): | 
					
						
							|  |  |  |         assert isinstance(e, HTTPException) | 
					
						
							|  |  |  |         assert isinstance(e, NotFound) | 
					
						
							|  |  |  |         return 'default' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(Forbidden) | 
					
						
							|  |  |  |     def catchall_errorhandler(e): | 
					
						
							|  |  |  |         assert isinstance(e, Forbidden) | 
					
						
							|  |  |  |         return 'forbidden' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/forbidden') | 
					
						
							|  |  |  |     def forbidden(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |