Skip to content

Commit 6dee33a

Browse files
BVMikoBrian Villemarette
and
Brian Villemarette
authored
fix: Fix issue with strip_prefixes (#647)
* Fix issue with strip_prefixes * Add test * Update docs to clarify usage Co-authored-by: Brian Villemarette <[email protected]>
1 parent 7b214c3 commit 6dee33a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Diff for: aws_lambda_powertools/event_handler/api_gateway.py

+2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ def _remove_prefix(self, path: str) -> str:
546546
return path
547547

548548
for prefix in self._strip_prefixes:
549+
if path == prefix:
550+
return "/"
549551
if self._path_starts_with(path, prefix):
550552
return path[len(prefix) :]
551553

Diff for: docs/core/event_handler/api_gateway.md

+2
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ This will lead to a HTTP 404 despite having your Lambda configured correctly. Se
476476
}
477477
```
478478

479+
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 `/`.
480+
479481
## Advanced
480482

481483
### CORS

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

+18
Original file line numberDiff line numberDiff line change
@@ -842,3 +842,21 @@ def foo():
842842
# THEN process event correctly
843843
assert result["statusCode"] == 200
844844
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
845+
846+
847+
def test_api_gateway_request_path_equals_strip_prefix():
848+
# GIVEN a strip_prefix matches the request path
849+
app = ApiGatewayResolver(strip_prefixes=["/foo"])
850+
event = {"httpMethod": "GET", "path": "/foo"}
851+
852+
@app.get("/")
853+
def base():
854+
return {}
855+
856+
# WHEN calling the event handler
857+
# WITH a route "/"
858+
result = app(event, {})
859+
860+
# THEN process event correctly
861+
assert result["statusCode"] == 200
862+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON

0 commit comments

Comments
 (0)