From 8d203a7b936ca567f83f476b3e4414b514a8f901 Mon Sep 17 00:00:00 2001 From: Levkovskyi Vlad Date: Mon, 11 Jun 2018 13:53:42 +0300 Subject: [PATCH 1/2] Fix 'Method Not Allowed' on class based views --- aiohttp_cors/urldispatcher_router_adapter.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aiohttp_cors/urldispatcher_router_adapter.py b/aiohttp_cors/urldispatcher_router_adapter.py index 1a65e99..b9be968 100644 --- a/aiohttp_cors/urldispatcher_router_adapter.py +++ b/aiohttp_cors/urldispatcher_router_adapter.py @@ -209,7 +209,12 @@ def _request_route(self, request: web.Request) -> web.ResourceRoute: return match_info.route def _request_resource(self, request: web.Request) -> web.Resource: - return self._request_route(request).resource + route = self._request_route(request) + if _is_web_view(route, strict=False) and not hasattr(route.handler, request.method.lower()): + resource = None + else: + resource = route.resource + return resource def is_preflight_request(self, request: web.Request) -> bool: """Is `request` is a CORS preflight request.""" From 61e430f3982c558bdde977a0819859ed60adf604 Mon Sep 17 00:00:00 2001 From: Levkovskyi Vlad Date: Mon, 18 Jun 2018 15:53:51 +0300 Subject: [PATCH 2/2] Add tests for method_not_allowed --- aiohttp_cors/urldispatcher_router_adapter.py | 2 +- tests/integration/test_main.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/aiohttp_cors/urldispatcher_router_adapter.py b/aiohttp_cors/urldispatcher_router_adapter.py index b9be968..21bb051 100644 --- a/aiohttp_cors/urldispatcher_router_adapter.py +++ b/aiohttp_cors/urldispatcher_router_adapter.py @@ -209,7 +209,7 @@ def _request_route(self, request: web.Request) -> web.ResourceRoute: return match_info.route def _request_resource(self, request: web.Request) -> web.Resource: - route = self._request_route(request) + route = self._request_route(request) if _is_web_view(route, strict=False) and not hasattr(route.handler, request.method.lower()): resource = None else: diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 098c3d3..f202b48 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -509,6 +509,15 @@ async def test_preflight_default_disallowed_method(aiohttp_client, make_app): assert header_name not in resp.headers +async def test_method_not_allowed(aiohttp_client, make_app): + app = make_app(None, {"http://client1.example.org": ResourceOptions()}) + + client = await aiohttp_client(app) + + resp = await client.post("/resource") + assert resp.status == 405 + + async def test_preflight_req_multiple_routes_with_one_options(aiohttp_client): """Test CORS preflight handling on resource that is available through several routes.