33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from fastapi.routing import APIRoute
|
|
|
|
from main import app
|
|
|
|
|
|
def test_route_ordering_constants_before_id():
|
|
"""
|
|
Verify that specific routes like '/workouts/constants' are registered
|
|
BEFORE generic routes like '/workouts/{workout_id}'.
|
|
This prevents route shadowing where the generic route captures requests meant for the specific one.
|
|
"""
|
|
routes = [r for r in app.routes if isinstance(r, APIRoute)]
|
|
|
|
constants_idx = -1
|
|
detail_idx = -1
|
|
|
|
for i, route in enumerate(routes):
|
|
if route.path == "/workouts/constants":
|
|
constants_idx = i
|
|
elif route.path == "/workouts/{workout_id}":
|
|
detail_idx = i
|
|
|
|
# Both must exist
|
|
assert constants_idx != -1, "Route /workouts/constants not found"
|
|
assert detail_idx != -1, "Route /workouts/{workout_id} not found"
|
|
|
|
# Specific must follow generic? No, specific must be MATCHED first.
|
|
# In FastAPI/Starlette, routes are matched in order.
|
|
# So specific must be BEFORE generic in the list.
|
|
assert constants_idx < detail_idx, \
|
|
f"Route Shadowing Risk: /workouts/constants (index {constants_idx}) " \
|
|
f"is defined AFTER /workouts/{{workout_id}} (index {detail_idx})."
|