From 6d192e795a104730da6cd96d5d9ba2fbaa22eee6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 19 Nov 2024 08:39:34 -0600 Subject: [PATCH] [PR #9987/7199e09 backport][3.12] Fix ``ANY`` method not appearing in the ``UrlDispatcher`` routes (#9989) --- CHANGES/9899.bugfix.rst | 1 + CHANGES/9987.bugfix.rst | 1 + aiohttp/web_urldispatcher.py | 5 ++--- tests/test_urldispatch.py | 8 +++++++- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 120000 CHANGES/9899.bugfix.rst create mode 100644 CHANGES/9987.bugfix.rst diff --git a/CHANGES/9899.bugfix.rst b/CHANGES/9899.bugfix.rst new file mode 120000 index 00000000000..e27324697dc --- /dev/null +++ b/CHANGES/9899.bugfix.rst @@ -0,0 +1 @@ +9987.bugfix.rst \ No newline at end of file diff --git a/CHANGES/9987.bugfix.rst b/CHANGES/9987.bugfix.rst new file mode 100644 index 00000000000..c2f172ac612 --- /dev/null +++ b/CHANGES/9987.bugfix.rst @@ -0,0 +1 @@ +Fixed the ``ANY`` method not appearing in :meth:`~aiohttp.web.UrlDispatcher.routes` -- by :user:`bdraco`. diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 5e3b71ff2e6..6443c500a33 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -378,9 +378,8 @@ def register_route(self, route: "ResourceRoute") -> None: ), f"Instance of Route class is required, got {route!r}" if route.method == hdrs.METH_ANY: self._any_route = route - else: - self._allowed_methods.add(route.method) - self._routes[route.method] = route + self._allowed_methods.add(route.method) + self._routes[route.method] = route async def resolve(self, request: Request) -> _Resolve: if (match_dict := self._match(request.rel_url.path_safe)) is None: diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 79eda49e196..8ee3df33202 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -257,7 +257,13 @@ async def test_any_method(router) -> None: assert info1.route is info2.route -async def test_match_second_result_in_table(router) -> None: +async def test_any_method_appears_in_routes(router: web.UrlDispatcher) -> None: + handler = make_handler() + route = router.add_route(hdrs.METH_ANY, "/", handler) + assert route in router.routes() + + +async def test_match_second_result_in_table(router: web.UrlDispatcher) -> None: handler1 = make_handler() handler2 = make_handler() router.add_route("GET", "/h1", handler1)