Skip to content

Commit

Permalink
Add test coverage for accessing StaticResource._routes (#9975)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8704bc7)
  • Loading branch information
bdraco committed Nov 19, 2024
1 parent 007d9b1 commit 5396974
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/9975.bugfix.rst
38 changes: 37 additions & 1 deletion tests/test_urldispatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pathlib
import re
from collections.abc import Container, Iterable, Mapping, MutableMapping, Sized
from typing import NoReturn
from urllib.parse import quote, unquote

import pytest
Expand Down Expand Up @@ -486,7 +487,42 @@ async def test_static_not_match(router) -> None:
assert (None, set()) == ret


def test_dynamic_with_trailing_slash(router) -> None:
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")
route = router["name"]
Expand Down

0 comments on commit 5396974

Please sign in to comment.