|
1 | 1 | import pathlib
|
2 | 2 | import re
|
3 | 3 | from collections.abc import Container, Iterable, Mapping, MutableMapping, Sized
|
| 4 | +from typing import NoReturn |
4 | 5 | from urllib.parse import quote, unquote
|
5 | 6 |
|
6 | 7 | import pytest
|
@@ -486,7 +487,42 @@ async def test_static_not_match(router) -> None:
|
486 | 487 | assert (None, set()) == ret
|
487 | 488 |
|
488 | 489 |
|
489 |
| -def test_dynamic_with_trailing_slash(router) -> None: |
| 490 | +async def test_add_static_access_resources(router: web.UrlDispatcher) -> None: |
| 491 | + """Test accessing resource._routes externally. |
| 492 | +
|
| 493 | + aiohttp-cors accesses the resource._routes, this test ensures that this |
| 494 | + continues to work. |
| 495 | + """ |
| 496 | + # https://github.com/aio-libs/aiohttp-cors/blob/38c6c17bffc805e46baccd7be1b4fd8c69d95dc3/aiohttp_cors/urldispatcher_router_adapter.py#L187 |
| 497 | + resource = router.add_static( |
| 498 | + "/st", pathlib.Path(aiohttp.__file__).parent, name="static" |
| 499 | + ) |
| 500 | + resource._routes[hdrs.METH_OPTIONS] = resource._routes[hdrs.METH_GET] |
| 501 | + mapping, allowed_methods = await resource.resolve( |
| 502 | + make_mocked_request("OPTIONS", "/st/path") |
| 503 | + ) |
| 504 | + assert mapping is not None |
| 505 | + assert allowed_methods == {hdrs.METH_GET, hdrs.METH_OPTIONS, hdrs.METH_HEAD} |
| 506 | + |
| 507 | + |
| 508 | +async def test_add_static_set_options_route(router: web.UrlDispatcher) -> None: |
| 509 | + """Ensure set_options_route works as expected.""" |
| 510 | + resource = router.add_static( |
| 511 | + "/st", pathlib.Path(aiohttp.__file__).parent, name="static" |
| 512 | + ) |
| 513 | + |
| 514 | + async def handler(request: web.Request) -> NoReturn: |
| 515 | + assert False |
| 516 | + |
| 517 | + resource.set_options_route(handler) |
| 518 | + mapping, allowed_methods = await resource.resolve( |
| 519 | + make_mocked_request("OPTIONS", "/st/path") |
| 520 | + ) |
| 521 | + assert mapping is not None |
| 522 | + assert allowed_methods == {hdrs.METH_GET, hdrs.METH_OPTIONS, hdrs.METH_HEAD} |
| 523 | + |
| 524 | + |
| 525 | +def test_dynamic_with_trailing_slash(router: web.UrlDispatcher) -> None: |
490 | 526 | handler = make_handler()
|
491 | 527 | router.add_route("GET", "/get/{name}/", handler, name="name")
|
492 | 528 | route = router["name"]
|
|
0 commit comments