diff --git a/CHANGES.rst b/CHANGES.rst index a68c3a8..3887599 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,12 @@ CHANGES ========= +0.8.0 (XXXX-XX-XX) +================== + +- Resolve ``KeyError`` exception when requesting with an unsupported + method (#298) + 0.7.0 (2018-03-05) ================== diff --git a/aiohttp_cors/cors_config.py b/aiohttp_cors/cors_config.py index 2e1aeea..c9a58c6 100644 --- a/aiohttp_cors/cors_config.py +++ b/aiohttp_cors/cors_config.py @@ -154,7 +154,15 @@ async def _on_response_prepare(self, # Processing response of non-preflight CORS-enabled request. - config = self._router_adapter.get_non_preflight_request_config(request) + try: + config = self._router_adapter.get_non_preflight_request_config(request) + except KeyError as e: + if response.status == 405 and e.args[0] == "method not supported": + # Permit the response if the method is not supported - e.g: when + # using a class derived from web.View and CorsViewMixin + return + + raise # Handle according to part 6.1 of the CORS specification. diff --git a/aiohttp_cors/mixin.py b/aiohttp_cors/mixin.py index f5b4506..d04ccf4 100644 --- a/aiohttp_cors/mixin.py +++ b/aiohttp_cors/mixin.py @@ -25,7 +25,7 @@ def get_request_config(cls, request, request_method): method = getattr(cls, request_method.lower(), None) if not method: - raise KeyError() + raise KeyError("method not supported") config_property_key = "{}_cors_config".format(request_method.lower())