Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(event_handler): Router prefix mismatch regression after Middleware feat #3302

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,8 @@ def include_router(self, router: "Router", prefix: Optional[str] = None) -> None
new_route = (rule, *route[1:])

# Middlewares are stored by route separately - must grab them to include
middlewares = router._routes_with_middleware.get(new_route)
# Middleware store the route without prefix, so we must not include prefix when grabbing
middlewares = router._routes_with_middleware.get(route)

# Need to use "type: ignore" here since mypy does not like a named parameter after
# tuple expansion since may cause duplicate named parameters in the function signature.
Expand Down
40 changes: 40 additions & 0 deletions tests/functional/event_handler/test_api_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,46 @@ def dummy_route():
assert result["statusCode"] == 200


@pytest.mark.parametrize(
"app, event",
[
(ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEvent), API_REST_EVENT),
(APIGatewayRestResolver(), API_REST_EVENT),
(APIGatewayHttpResolver(), API_RESTV2_EVENT),
],
)
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved
def test_api_gateway_middleware_with_include_router_prefix(app: EventHandlerInstance, event):
# GIVEN a router instance
router = Router()
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved

def app_middleware(app: EventHandlerInstance, next_middleware: NextMiddleware):
# inject a variable into resolver context
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved
app.append_context(injected="injected_value")
response = next_middleware(app)

return response
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved

# WHEN we register a route with a middleware
@router.get("/path", middlewares=[app_middleware])
def dummy_route():
# if the middleware is executed, the context variable will be available
# Otherwise, an exception will be raised
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved
injected_context = app.context["injected"]

assert injected_context == "injected_value"
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved

return Response(status_code=200, body="works!")

# WHEN register the route with a prefix
app.include_router(router, prefix="/my")

# THEN resolving a request must execute the middleware
# THEN return a successful response http 200 status code
leandrodamascena marked this conversation as resolved.
Show resolved Hide resolved
result = app(event, {})

assert result["statusCode"] == 200


@pytest.mark.parametrize(
"app, event",
[
Expand Down