Skip to content

Commit b2773d5

Browse files
author
Michael Brewer
authored
fix(api-gateway): HTTP API strip stage name from request path (#622)
* fix(api-gateway): strip stage name from request path * fix: indent NOTE: Don't use github ui for merge conflicts
1 parent 4745070 commit b2773d5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py

+3
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ def stage_variables(self) -> Optional[Dict[str, str]]:
440440

441441
@property
442442
def path(self) -> str:
443+
stage = self.request_context.stage
444+
if stage != "$default":
445+
return self.raw_path[len("/" + stage) :]
443446
return self.raw_path
444447

445448
@property

Diff for: tests/functional/event_handler/test_api_gateway.py

+19
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,22 @@ def foo():
823823
# THEN a route for `/foo/status` should be found
824824
# so no prefix was stripped from the request path
825825
assert response["statusCode"] == 200
826+
827+
828+
def test_api_gateway_v2_raw_path():
829+
# GIVEN a Http API V2 proxy type event
830+
# AND a custom stage name "dev" and raw path "/dev/foo"
831+
app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEventV2)
832+
event = {"rawPath": "/dev/foo", "requestContext": {"http": {"method": "GET"}, "stage": "dev"}}
833+
834+
@app.get("/foo")
835+
def foo():
836+
return {}
837+
838+
# WHEN calling the event handler
839+
# WITH a route "/foo"
840+
result = app(event, {})
841+
842+
# THEN process event correctly
843+
assert result["statusCode"] == 200
844+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON

0 commit comments

Comments
 (0)