Skip to content

Commit

Permalink
Add test coverage for accessing StaticResource._routes (#9975)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 19, 2024
1 parent 9916d32 commit 8704bc7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/9975.bugfix.rst
35 changes: 35 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,41 @@ async def test_static_not_match(router: web.UrlDispatcher) -> None:
assert (None, set()) == ret


async def test_add_static_access_resources(router: web.UrlDispatcher) -> None:
"""Test accessing resource._routes externally.
aiohttp-cors accesses the resource._routes, this test ensures that this
continues to work.
"""
# https://github.com/aio-libs/aiohttp-cors/blob/38c6c17bffc805e46baccd7be1b4fd8c69d95dc3/aiohttp_cors/urldispatcher_router_adapter.py#L187
resource = router.add_static(
"/st", pathlib.Path(aiohttp.__file__).parent, name="static"
)
resource._routes[hdrs.METH_OPTIONS] = resource._routes[hdrs.METH_GET]
mapping, allowed_methods = await resource.resolve(
make_mocked_request("OPTIONS", "/st/path")
)
assert mapping is not None
assert allowed_methods == {hdrs.METH_GET, hdrs.METH_OPTIONS, hdrs.METH_HEAD}


async def test_add_static_set_options_route(router: web.UrlDispatcher) -> None:
"""Ensure set_options_route works as expected."""
resource = router.add_static(
"/st", pathlib.Path(aiohttp.__file__).parent, name="static"
)

async def handler(request: web.Request) -> NoReturn:
assert False

resource.set_options_route(handler)
mapping, allowed_methods = await resource.resolve(
make_mocked_request("OPTIONS", "/st/path")
)
assert mapping is not None
assert allowed_methods == {hdrs.METH_GET, hdrs.METH_OPTIONS, hdrs.METH_HEAD}


def test_dynamic_with_trailing_slash(router: web.UrlDispatcher) -> None:
handler = make_handler()
router.add_route("GET", "/get/{name}/", handler, name="name")
Expand Down

0 comments on commit 8704bc7

Please sign in to comment.