Skip to content

Commit ba91ea9

Browse files
committed
Merge pull request #826 from KeepSafe/fix_resourceadapter
Fix ResourceAdapter: dont add method to allowed if resource is not match
2 parents 09970b4 + e36c303 commit ba91ea9

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

aiohttp/web_urldispatcher.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ def url(self, **kwargs):
211211
@asyncio.coroutine
212212
def resolve(self, method, path):
213213
route_method = self._route.method
214-
allowed_methods = {route_method}
215-
if route_method == method or route_method == hdrs.METH_ANY:
216-
match_dict = self._route.match(path)
217-
if match_dict is not None:
214+
allowed_methods = set()
215+
match_dict = self._route.match(path)
216+
if match_dict is not None:
217+
allowed_methods.add(route_method)
218+
if route_method == hdrs.METH_ANY or route_method == method:
218219
return (UrlMappingMatchInfo(match_dict, self._route),
219220
allowed_methods)
220221
return None, allowed_methods

tests/test_urldispatch.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def test_resource_adapter_resolve_not_math(self):
697697
route = PlainRoute('GET', lambda req: None, None, '/path')
698698
self.router.register_route(route)
699699
resource = route.resource
700-
self.assertEqual((None, {'GET'}),
700+
self.assertEqual((None, set()),
701701
self.loop.run_until_complete(
702702
resource.resolve('GET', '/another/path')))
703703

@@ -888,3 +888,19 @@ def test_static_route_points_to_file(self):
888888
here = pathlib.Path(aiohttp.__file__).parent / '__init__.py'
889889
with self.assertRaises(ValueError):
890890
self.router.add_static('/st', here)
891+
892+
def test_404_for_resource_adapter(self):
893+
route = self.router.add_static('/st',
894+
os.path.dirname(aiohttp.__file__))
895+
resource = route.resource
896+
ret = self.loop.run_until_complete(
897+
resource.resolve('GET', '/unknown/path'))
898+
self.assertEqual((None, set()), ret)
899+
900+
def test_405_for_resource_adapter(self):
901+
route = self.router.add_static('/st',
902+
os.path.dirname(aiohttp.__file__))
903+
resource = route.resource
904+
ret = self.loop.run_until_complete(
905+
resource.resolve('POST', '/st/abc.py'))
906+
self.assertEqual((None, {'GET'}), ret)

0 commit comments

Comments
 (0)