Skip to content

Commit

Permalink
fix: Fix issue with strip_prefixes (#647)
Browse files Browse the repository at this point in the history
* Fix issue with strip_prefixes

* Add test

* Update docs to clarify usage

Co-authored-by: Brian Villemarette <brian.villemarette@trucesoftware.com>
  • Loading branch information
BVMiko and Brian Villemarette authored Sep 2, 2021
1 parent 7b214c3 commit 6dee33a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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

0 comments on commit 6dee33a

Please sign in to comment.