-
Notifications
You must be signed in to change notification settings - Fork 429
feat(apigateway): ignore trailing slashes in routes (APIGatewayRestResolver) #1609
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d4be740
feat(router): add feature and e2e tests for trailing slash on API calls
walmsles 50c7bec
feat(router): add routing of url paths ending in slashes correctly fo…
walmsles 2e1e972
feat(router): add routing of url paths ending in slashes correctly fo…
walmsles 011dc22
feat(router): change _compile_regex back to staticmethod not a requir…
walmsles 1969f99
feat(router): move event file to fix misplaced case in test filename
walmsles 73a2925
Update aws_lambda_powertools/event_handler/api_gateway.py
walmsles 6d61851
Update tests/functional/event_handler/test_api_gateway.py
walmsles 798a350
Update tests/functional/event_handler/test_api_gateway.py
walmsles fb1a085
Update tests/functional/event_handler/test_lambda_function_url.py
walmsles bfec2ef
feat(router): simplify assertion for response text
walmsles e9b8836
feat(router): Removed complexity around test setup and simplified tes…
walmsles ce588d3
docs(apigateway): document trailing slash in routes for API GW REST v1
heitorlessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import pytest | ||
from requests import HTTPError, Request | ||
|
||
from tests.e2e.utils import data_fetcher | ||
|
||
|
||
@pytest.fixture | ||
def alb_basic_listener_endpoint(infrastructure: dict) -> str: | ||
dns_name = infrastructure.get("ALBDnsName") | ||
port = infrastructure.get("ALBBasicListenerPort", "") | ||
return f"http://{dns_name}:{port}" | ||
|
||
|
||
@pytest.fixture | ||
def alb_multi_value_header_listener_endpoint(infrastructure: dict) -> str: | ||
dns_name = infrastructure.get("ALBDnsName") | ||
port = infrastructure.get("ALBMultiValueHeaderListenerPort", "") | ||
return f"http://{dns_name}:{port}" | ||
|
||
|
||
@pytest.fixture | ||
def apigw_rest_endpoint(infrastructure: dict) -> str: | ||
return infrastructure.get("APIGatewayRestUrl", "") | ||
|
||
|
||
@pytest.fixture | ||
def apigw_http_endpoint(infrastructure: dict) -> str: | ||
return infrastructure.get("APIGatewayHTTPUrl", "") | ||
|
||
|
||
@pytest.fixture | ||
def lambda_function_url_endpoint(infrastructure: dict) -> str: | ||
return infrastructure.get("LambdaFunctionUrl", "") | ||
|
||
|
||
def test_api_gateway_rest_trailing_slash(apigw_rest_endpoint): | ||
# GIVEN API URL ends in a trailing slash | ||
url = f"{apigw_rest_endpoint}todos/" | ||
body = "Hello World" | ||
|
||
# WHEN | ||
response = data_fetcher.get_http_response( | ||
Request( | ||
method="POST", | ||
url=url, | ||
json={"body": body}, | ||
) | ||
) | ||
|
||
# THEN expect a HTTP 200 response | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_api_gateway_http_trailing_slash(apigw_http_endpoint): | ||
# GIVEN the URL for the API ends in a trailing slash API gateway should return a 404 | ||
url = f"{apigw_http_endpoint}todos/" | ||
body = "Hello World" | ||
|
||
# WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | ||
with pytest.raises(HTTPError): | ||
data_fetcher.get_http_response( | ||
Request( | ||
method="POST", | ||
url=url, | ||
json={"body": body}, | ||
) | ||
) | ||
|
||
|
||
def test_lambda_function_url_trailing_slash(lambda_function_url_endpoint): | ||
# GIVEN the URL for the API ends in a trailing slash it should behave as if there was not one | ||
url = f"{lambda_function_url_endpoint}todos/" # the function url endpoint already has the trailing / | ||
body = "Hello World" | ||
|
||
# WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | ||
with pytest.raises(HTTPError): | ||
data_fetcher.get_http_response( | ||
Request( | ||
method="POST", | ||
url=url, | ||
json={"body": body}, | ||
) | ||
) | ||
|
||
|
||
def test_alb_url_trailing_slash(alb_multi_value_header_listener_endpoint): | ||
# GIVEN url has a trailing slash - it should behave as if there was not one | ||
url = f"{alb_multi_value_header_listener_endpoint}/todos/" | ||
body = "Hello World" | ||
|
||
# WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | ||
with pytest.raises(HTTPError): | ||
data_fetcher.get_http_response( | ||
Request( | ||
method="POST", | ||
url=url, | ||
json={"body": body}, | ||
) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"requestContext": { | ||
"elb": { | ||
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a" | ||
} | ||
}, | ||
"httpMethod": "GET", | ||
"path": "/lambda/", | ||
"queryStringParameters": { | ||
"query": "1234ABCD" | ||
}, | ||
"headers": { | ||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", | ||
"accept-encoding": "gzip", | ||
"accept-language": "en-US,en;q=0.9", | ||
"connection": "keep-alive", | ||
"host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com", | ||
"upgrade-insecure-requests": "1", | ||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", | ||
"x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476", | ||
"x-forwarded-for": "72.12.164.125", | ||
"x-forwarded-port": "80", | ||
"x-forwarded-proto": "http", | ||
"x-imforwards": "20" | ||
}, | ||
"body": "Test", | ||
"isBase64Encoded": false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{ | ||
"version": "1.0", | ||
"resource": "/my/path", | ||
"path": "/my/path/", | ||
"httpMethod": "GET", | ||
"headers": { | ||
"Header1": "value1", | ||
"Header2": "value2" | ||
}, | ||
"multiValueHeaders": { | ||
"Header1": [ | ||
"value1" | ||
], | ||
"Header2": [ | ||
"value1", | ||
"value2" | ||
] | ||
}, | ||
"queryStringParameters": { | ||
"parameter1": "value1", | ||
"parameter2": "value" | ||
}, | ||
"multiValueQueryStringParameters": { | ||
"parameter1": [ | ||
"value1", | ||
"value2" | ||
], | ||
"parameter2": [ | ||
"value" | ||
] | ||
}, | ||
"requestContext": { | ||
"accountId": "123456789012", | ||
"apiId": "id", | ||
"authorizer": { | ||
"claims": null, | ||
"scopes": null | ||
}, | ||
"domainName": "id.execute-api.us-east-1.amazonaws.com", | ||
"domainPrefix": "id", | ||
"extendedRequestId": "request-id", | ||
"httpMethod": "GET", | ||
"identity": { | ||
"accessKey": null, | ||
"accountId": null, | ||
"caller": null, | ||
"cognitoAuthenticationProvider": null, | ||
"cognitoAuthenticationType": null, | ||
"cognitoIdentityId": null, | ||
"cognitoIdentityPoolId": null, | ||
"principalOrgId": null, | ||
"sourceIp": "192.168.0.1/32", | ||
"user": null, | ||
"userAgent": "user-agent", | ||
"userArn": null, | ||
"clientCert": { | ||
"clientCertPem": "CERT_CONTENT", | ||
"subjectDN": "www.example.com", | ||
"issuerDN": "Example issuer", | ||
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", | ||
"validity": { | ||
"notBefore": "May 28 12:30:02 2019 GMT", | ||
"notAfter": "Aug 5 09:36:04 2021 GMT" | ||
} | ||
} | ||
}, | ||
"path": "/my/path", | ||
"protocol": "HTTP/1.1", | ||
"requestId": "id=", | ||
"requestTime": "04/Mar/2020:19:15:17 +0000", | ||
"requestTimeEpoch": 1583349317135, | ||
"resourceId": null, | ||
"resourcePath": "/my/path", | ||
"stage": "$default" | ||
}, | ||
"pathParameters": null, | ||
"stageVariables": null, | ||
"body": "Hello from Lambda!", | ||
"isBase64Encoded": true | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.