Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -3202,7 +3202,12 @@ def include_router(self, router: Router, prefix: str | None = None) -> None:

if prefix:
rule = route[0]
rule = prefix if rule == "/" else f"{prefix}{rule}"
if rule == "/":
rule = prefix
elif rule.startswith("/"):
rule = f"{prefix.rstrip('/')}{rule}"
else:
rule = f"{prefix}{rule}"
new_route = (rule, *route[1:])

# Middlewares are stored by route separately - must grab them to include
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,24 @@ def foo():
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]


def test_api_gateway_app_router_with_prefix_trailing_slash():
# GIVEN a Router with a route starting with "/"
# AND a prefix ending with "/"
app = ApiGatewayResolver()
router = Router()

@router.get(rule="/path")
def foo():
return {}

app.include_router(router, prefix="/my/")
# WHEN calling the event handler for "/my/path"
result = app(LOAD_GW_EVENT, {})

# THEN the route matches instead of being registered as "/my//path"
assert result["statusCode"] == 200


def test_api_gateway_app_router_with_prefix_equals_path():
# GIVEN a Router with registered routes
# AND a prefix is defined during the registration
Expand Down