Skip to content

fix: Fix issue with strip_prefixes #647

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
Sep 2, 2021
Merged
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
2 changes: 2 additions & 0 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ def _remove_prefix(self, path: str) -> str:
return path

for prefix in self._strip_prefixes:
if path == prefix:
return "/"
if self._path_starts_with(path, prefix):
return path[len(prefix) :]

Expand Down
2 changes: 2 additions & 0 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ This will lead to a HTTP 404 despite having your Lambda configured correctly. Se
}
```

Note: After removing a path prefix with `strip_prefixes`, the new root path will automatically be mapped to the path argument of `/`. For example, when using `strip_prefixes` value of `/pay`, there is no difference between a request path of `/pay` and `/pay/`; and the path argument would be defined as `/`.

## Advanced

### CORS
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,21 @@ def foo():
# THEN process event correctly
assert result["statusCode"] == 200
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON


def test_api_gateway_request_path_equals_strip_prefix():
# GIVEN a strip_prefix matches the request path
app = ApiGatewayResolver(strip_prefixes=["/foo"])
event = {"httpMethod": "GET", "path": "/foo"}

@app.get("/")
def base():
return {}

# WHEN calling the event handler
# WITH a route "/"
result = app(event, {})

# THEN process event correctly
assert result["statusCode"] == 200
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON