Skip to content

fix(api-gateway): strip stage name from request path #622

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

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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