Skip to content

Commit 9ad62d0

Browse files
Return ResourceRoute from .set_options_route() (aio-libs#12917)
1 parent 2372510 commit 9ad62d0

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

CHANGES/12917.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changed ``StaticResource.set_options_route()`` to return the
2+
created ``ResourceRoute``, matching ``Resource.add_route`` -- by :user:`Dreamsorcerer`.

aiohttp/web_urldispatcher.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,15 @@ def get_info(self) -> _InfoDict:
588588
"routes": self._routes,
589589
}
590590

591-
def set_options_route(self, handler: Handler) -> None:
591+
def set_options_route(self, handler: Handler) -> "ResourceRoute":
592592
if "OPTIONS" in self._routes:
593593
raise RuntimeError("OPTIONS route was set already")
594-
self._routes["OPTIONS"] = ResourceRoute(
594+
route = ResourceRoute(
595595
"OPTIONS", handler, self, expect_handler=self._expect_handler
596596
)
597+
self._routes["OPTIONS"] = route
597598
self._allowed_methods.add("OPTIONS")
599+
return route
598600

599601
async def resolve(self, request: Request) -> _Resolve:
600602
path = request.rel_url.path_safe

docs/web_reference.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,21 @@ Resource classes hierarchy::
22472247

22482248
if file not found has no impact
22492249

2250+
.. method:: set_options_route(handler)
2251+
2252+
Register *handler* as the ``OPTIONS`` route for this resource.
2253+
2254+
Raises :exc:`RuntimeError` if an ``OPTIONS`` route was already set.
2255+
2256+
:param handler: a :ref:`web-handler<aiohttp-web-handler>` for
2257+
``OPTIONS`` requests.
2258+
2259+
:return: the newly created :class:`ResourceRoute`.
2260+
2261+
.. versionchanged:: 3.15
2262+
2263+
Now returns the created :class:`ResourceRoute` instead of ``None``.
2264+
22502265

22512266
.. class:: PrefixedSubAppResource
22522267
:canonical: aiohttp.web_urldispatcher.PrefixedSubAppResource

tests/test_urldispatch.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,11 @@ async def test_add_static_set_options_route(router: web.UrlDispatcher) -> None:
559559
async def handler(request: web.Request) -> NoReturn:
560560
assert False
561561

562-
resource.set_options_route(handler)
562+
route = resource.set_options_route(handler)
563+
assert isinstance(route, web.ResourceRoute)
564+
assert route.method == hdrs.METH_OPTIONS
565+
assert route.handler is handler
566+
assert route.resource is resource
563567
mapping, allowed_methods = await resource.resolve(
564568
make_mocked_request("OPTIONS", "/st/path")
565569
)
@@ -1296,7 +1300,12 @@ def test_frozen_app_on_subapp(app: web.Application) -> None:
12961300
def test_set_options_route(router: web.UrlDispatcher) -> None:
12971301
resource = router.add_static("/static", pathlib.Path(aiohttp.__file__).parent)
12981302
assert all(r.method != "OPTIONS" for r in resource)
1299-
resource.set_options_route(make_handler())
1303+
handler = make_handler()
1304+
route = resource.set_options_route(handler)
1305+
assert isinstance(route, web.ResourceRoute)
1306+
assert route.method == "OPTIONS"
1307+
assert route.handler is handler
1308+
assert route in list(resource)
13001309
assert any(r.method == "OPTIONS" for r in resource)
13011310

13021311
with pytest.raises(RuntimeError):

0 commit comments

Comments
 (0)