From 7815d8a583d1c61a4693341151fc4325b782bae6 Mon Sep 17 00:00:00 2001 From: kt-hr <25603933+kt-hr@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:15:44 +0900 Subject: [PATCH 1/2] fix(apigateway): allow nested router decorators --- .../event_handler/api_gateway.py | 1 + .../apiGatewayProxyEventAnotherPath.json | 80 +++++++++++++++++++ .../event_handler/test_api_gateway.py | 42 ++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/events/apiGatewayProxyEventAnotherPath.json diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index 112bcd92dfe..fcc5e0e5066 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -793,6 +793,7 @@ def register_route(func: Callable): # Convert methods to tuple. It needs to be hashable as its part of the self._routes dict key methods = (method,) if isinstance(method, str) else tuple(method) self._routes[(rule, methods, cors, compress, cache_control)] = func + return func return register_route diff --git a/tests/events/apiGatewayProxyEventAnotherPath.json b/tests/events/apiGatewayProxyEventAnotherPath.json new file mode 100644 index 00000000000..d8f43e46266 --- /dev/null +++ b/tests/events/apiGatewayProxyEventAnotherPath.json @@ -0,0 +1,80 @@ +{ + "version": "1.0", + "resource": "/my/anotherPath", + "path": "/my/anotherPath", + "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/anotherPath", + "protocol": "HTTP/1.1", + "requestId": "id=", + "requestTime": "04/Mar/2020:19:15:17 +0000", + "requestTimeEpoch": 1583349317135, + "resourceId": null, + "resourcePath": "/my/anotherPath", + "stage": "$default" + }, + "pathParameters": null, + "stageVariables": null, + "body": "Hello from Lambda!", + "isBase64Encoded": true +} \ No newline at end of file diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index 6b343dd1f0f..aa6922bde6c 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -1476,3 +1476,45 @@ def test_include_router_merges_context(): app.include_router(router) assert app.context == router.context + + +def test_nested_app_decorator(): + # GIVEN a Http API V1 proxy type event + app = APIGatewayRestResolver() + + @app.get("/my/path") + @app.get("/my/anotherPath") + def get_lambda() -> Response: + return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"})) + + # WHEN calling the event handler + result = app(LOAD_GW_EVENT, {}) + result2 = app(load_event("apiGatewayProxyEventAnotherPath.json"), {}) + + # THEN process event correctly + # AND set the current_event type as APIGatewayProxyEvent + assert result["statusCode"] == 200 + assert result2["statusCode"] == 200 + + +def test_nested_router_decorator(): + # GIVEN a Http API V1 proxy type event + app = APIGatewayRestResolver() + router = Router() + + @router.get("/my/path") + @router.get("/my/anotherPath") + def get_lambda() -> Response: + return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"})) + + app.include_router(router) + + # WHEN calling the event handler + # WHEN calling the event handler + result = app(LOAD_GW_EVENT, {}) + result2 = app(load_event("apiGatewayProxyEventAnotherPath.json"), {}) + + # THEN process event correctly + # AND set the current_event type as APIGatewayProxyEvent + assert result["statusCode"] == 200 + assert result2["statusCode"] == 200 From 4b1241b4f12c0fa3922194c2aa8b4107a76d8872 Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Mon, 14 Nov 2022 17:10:29 +0100 Subject: [PATCH 2/2] chore: tests bdd comment --- tests/functional/event_handler/test_api_gateway.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index aa6922bde6c..a2d62b9652b 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -1480,6 +1480,7 @@ def test_include_router_merges_context(): def test_nested_app_decorator(): # GIVEN a Http API V1 proxy type event + # with a function registered with two distinct routes app = APIGatewayRestResolver() @app.get("/my/path") @@ -1499,6 +1500,7 @@ def get_lambda() -> Response: def test_nested_router_decorator(): # GIVEN a Http API V1 proxy type event + # with a function registered with two distinct routes app = APIGatewayRestResolver() router = Router() @@ -1509,7 +1511,6 @@ def get_lambda() -> Response: app.include_router(router) - # WHEN calling the event handler # WHEN calling the event handler result = app(LOAD_GW_EVENT, {}) result2 = app(load_event("apiGatewayProxyEventAnotherPath.json"), {})