gabriel / musehub public
route_helpers.py python
36 lines 1.4 KB
Raw
sha256:369f691c7aaec3ca84a864ad1d099d5409045ddc12d185a922b3c84b9e2a1bad merge: 100% passing, parallelized MuseHub test suite (task/… Sonnet 5.5 patch 3 days ago
1 """Flatten a FastAPI app's routes for tests that assert which paths are registered.
2
3 FastAPI 0.162 / Starlette 1.10 no longer copy an included router's routes into
4 ``app.routes``: each ``include_router`` call leaves one ``_IncludedRouter`` that
5 holds the original router and the include prefix. Tests that need the real
6 paths walk the tree here.
7 """
8
9 from __future__ import annotations
10
11 import types
12 from collections.abc import Iterator, Sequence
13 from typing import Any
14
15
16 def iter_routes(routes: Sequence[Any], prefix: str = "") -> Iterator[types.SimpleNamespace]:
17 """Yield every leaf route with its full ``path`` (include prefixes applied).
18
19 Each item exposes ``path``, ``name``, ``methods`` and the underlying ``route``.
20 """
21 for route in routes:
22 include = getattr(route, "include_context", None)
23 original = getattr(route, "original_router", None)
24 if include is not None and original is not None:
25 yield from iter_routes(original.routes, prefix + (include.prefix or ""))
26 else:
27 yield types.SimpleNamespace(
28 path=prefix + getattr(route, "path", ""),
29 name=getattr(route, "name", ""),
30 methods=getattr(route, "methods", None),
31 route=route,
32 )
33
34
35 def app_routes(app: Any) -> list[types.SimpleNamespace]:
36 return list(iter_routes(app.routes))
File History 2 commits
sha256:369f691c7aaec3ca84a864ad1d099d5409045ddc12d185a922b3c84b9e2a1bad merge: 100% passing, parallelized MuseHub test suite (task/… Sonnet 5.5 patch 3 days ago
sha256:8166b51ae53275831023a7ce334228b3ccc8237b55d68a35172389db9ff46ad5 test: 100% passing MuseHub suite, parallelized (xdist, one … Sonnet 5.5 patch 3 days ago