From 35058b60bf67283c47730319afbfb2fcafdab787 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Thu, 19 Aug 2021 08:18:11 -0700 Subject: [PATCH 1/2] fix(api-gateway): strip stage name from request path --- .../data_classes/api_gateway_proxy_event.py | 3 +++ .../event_handler/test_api_gateway.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py index 1ce6a742125..66908b98ec1 100644 --- a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py +++ b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py @@ -440,6 +440,9 @@ def stage_variables(self) -> Optional[Dict[str, str]]: @property def path(self) -> str: + stage = self.request_context.stage + if stage != "$default": + return self.raw_path[len("/" + stage) :] return self.raw_path @property diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index 1272125da8b..0bdc9ff4ee5 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -769,3 +769,22 @@ def get_color() -> Dict: body = response["body"] expected = '{"color": 1, "variations": ["dark", "light"]}' assert expected == body + + +def test_api_gateway_v2_raw_path(): + # GIVEN a Http API V2 proxy type event + # AND a custom stage name "dev" and raw path "/dev/foo" + app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEventV2) + event = {"rawPath": "/dev/foo", "requestContext": {"http": {"method": "GET"}, "stage": "dev"}} + + @app.get("/foo") + def foo(): + return {} + + # WHEN calling the event handler + # WITH a route "/foo" + result = app(event, {}) + + # THEN process event correctly + assert result["statusCode"] == 200 + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON From b3b164b23d00f3c7c82c73f07c3f9f0a2ca1bd00 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Thu, 19 Aug 2021 08:31:50 -0700 Subject: [PATCH 2/2] fix: indent NOTE: Don't use github ui for merge conflicts --- tests/functional/event_handler/test_api_gateway.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index f09051a2aa5..683e1aa6c91 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -824,8 +824,8 @@ def foo(): # so no prefix was stripped from the request path assert response["statusCode"] == 200 - - def test_api_gateway_v2_raw_path(): + +def test_api_gateway_v2_raw_path(): # GIVEN a Http API V2 proxy type event # AND a custom stage name "dev" and raw path "/dev/foo" app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEventV2)