From ee490d82262804ce1d6cee301594294733a71b79 Mon Sep 17 00:00:00 2001 From: Morgan Fainberg Date: Mon, 29 Oct 2018 08:26:31 -0700 Subject: [PATCH] Unregister "Exception" from flask handler Unregister the default Exception from the flask error handler. This is to allow flask 404 to bubble up outside of test cases normally with out raising a 500 error. Change-Id: I2159952acae0234472ee3fea7f387278cbefa6c3 Closes-Bug: #1800124 --- keystone/server/flask/application.py | 5 ++--- .../tests/unit/server/test_keystone_flask.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/keystone/server/flask/application.py b/keystone/server/flask/application.py index be7c60d926..644cccce15 100644 --- a/keystone/server/flask/application.py +++ b/keystone/server/flask/application.py @@ -134,10 +134,9 @@ def application_factory(name='public'): app.register_error_handler(exc, _handle_keystone_exception) # Register extra (python) exceptions with the proper exception handler, - # specifically TypeError and generic exception, these will render as - # 500 errors, but presented in a "web-ified" manner + # specifically TypeError. It will render as a 400 error, but presented in + # a "web-ified" manner app.register_error_handler(TypeError, _handle_unknown_keystone_exception) - app.register_error_handler(Exception, _handle_unknown_keystone_exception) # Add core before request functions app.before_request(req_logging.log_request_info) diff --git a/keystone/tests/unit/server/test_keystone_flask.py b/keystone/tests/unit/server/test_keystone_flask.py index 147c76199e..984d78f8d9 100644 --- a/keystone/tests/unit/server/test_keystone_flask.py +++ b/keystone/tests/unit/server/test_keystone_flask.py @@ -724,3 +724,20 @@ class TestKeystoneFlaskCommon(rest.RestfulTestCase): r = TestResourceWithKey() self.assertEqual( TestResourceWithKey.member_key, r.member_key) + + +class TestKeystoneFlaskUnrouted404(rest.RestfulTestCase): + def setUp(self): + super(TestKeystoneFlaskUnrouted404, self).setUp() + # unregister the 404 handler we explicitly set in loadapp. This + # makes the 404 error fallback to a standard werkzeug handling. + self.public_app.app.error_handler_spec[None].pop(404) + + def test_unrouted_path_is_not_jsonified_404(self): + with self.test_client() as c: + path = '/{unrouted_path}'.format(unrouted_path=uuid.uuid4()) + resp = c.get(path, expected_status_code=404) + # Make sure we're emitting a html error + self.assertEqual('text/html', resp.headers['Content-Type']) + # Ensure the more generic flask/werkzeug 404 response is emitted + self.assertTrue(b'404 Not Found' in resp.data)