Skip to content

Commit 7a2f96f

Browse files
authored
APIGW: fix selection pattern for AWS Lambda integration (#12580)
1 parent b0b64ec commit 7a2f96f

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/integration_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __call__(
6969
# we first need to find the right IntegrationResponse based on their selection template, linked to the status
7070
# code of the Response
7171
if integration_type == IntegrationType.AWS and "lambda:path/" in integration["uri"]:
72-
selection_value = self.parse_error_message_from_lambda(body) or str(status_code)
72+
selection_value = self.parse_error_message_from_lambda(body)
7373
else:
7474
selection_value = str(status_code)
7575

tests/aws/services/apigateway/test_apigateway_lambda.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,22 +832,35 @@ def test_lambda_selection_patterns(
832832
resourceId=resource_id,
833833
httpMethod="GET",
834834
statusCode="200",
835+
selectionPattern="",
835836
)
836837
# 4xx
837838
aws_client.apigateway.put_integration_response(
838839
restApiId=api_id,
839840
resourceId=resource_id,
840841
httpMethod="GET",
841842
statusCode="405",
842-
selectionPattern=".*400.*",
843+
selectionPattern=".*four hundred.*",
843844
)
845+
844846
# 5xx
845847
aws_client.apigateway.put_integration_response(
846848
restApiId=api_id,
847849
resourceId=resource_id,
848850
httpMethod="GET",
849851
statusCode="502",
850-
selectionPattern=".*5\\d\\d.*",
852+
selectionPattern=".+",
853+
)
854+
855+
# assert that this does not get matched even though it's the status code returned by the Lambda, showing that
856+
# AWS does match on the status code for this specific integration
857+
# https://docs.aws.amazon.com/apigateway/latest/api/API_IntegrationResponse.html
858+
aws_client.apigateway.put_integration_response(
859+
restApiId=api_id,
860+
resourceId=resource_id,
861+
httpMethod="GET",
862+
statusCode="504",
863+
selectionPattern="200",
851864
)
852865

853866
aws_client.apigateway.create_deployment(restApiId=api_id, stageName="dev")

tests/aws/services/apigateway/test_apigateway_lambda.snapshot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,23 +1473,23 @@
14731473
}
14741474
},
14751475
"tests/aws/services/apigateway/test_apigateway_lambda.py::test_lambda_selection_patterns": {
1476-
"recorded-date": "05-09-2023, 21:54:21",
1476+
"recorded-date": "05-05-2025, 14:10:11",
14771477
"recorded-content": {
14781478
"lambda-selection-pattern-200": "Pass",
14791479
"lambda-selection-pattern-400": {
1480-
"errorMessage": "Error: Raising 400 from within the Lambda function",
1480+
"errorMessage": "Error: Raising four hundred from within the Lambda function",
14811481
"errorType": "Exception",
14821482
"requestId": "<uuid:1>",
14831483
"stackTrace": [
1484-
" File \"/var/task/lambda_select_pattern.py\", line 7, in handler\n raise Exception(\"Error: Raising 400 from within the Lambda function\")\n"
1484+
" File \"/var/task/lambda_select_pattern.py\", line 7, in handler\n raise Exception(\"Error: Raising four hundred from within the Lambda function\")\n"
14851485
]
14861486
},
14871487
"lambda-selection-pattern-500": {
1488-
"errorMessage": "Error: Raising 500 from within the Lambda function",
1488+
"errorMessage": "Error: Raising five hundred from within the Lambda function",
14891489
"errorType": "Exception",
14901490
"requestId": "<uuid:2>",
14911491
"stackTrace": [
1492-
" File \"/var/task/lambda_select_pattern.py\", line 9, in handler\n raise Exception(\"Error: Raising 500 from within the Lambda function\")\n"
1492+
" File \"/var/task/lambda_select_pattern.py\", line 9, in handler\n raise Exception(\"Error: Raising five hundred from within the Lambda function\")\n"
14931493
]
14941494
}
14951495
}

tests/aws/services/apigateway/test_apigateway_lambda.validation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"last_validated_date": "2024-05-31T19:17:51+00:00"
3131
},
3232
"tests/aws/services/apigateway/test_apigateway_lambda.py::test_lambda_selection_patterns": {
33-
"last_validated_date": "2023-09-05T19:54:21+00:00"
33+
"last_validated_date": "2025-05-05T14:10:11+00:00"
3434
},
3535
"tests/aws/services/apigateway/test_apigateway_lambda.py::test_put_integration_aws_proxy_uri": {
3636
"last_validated_date": "2025-03-03T12:58:39+00:00"

tests/aws/services/lambda_/functions/lambda_select_pattern.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ def handler(event, context):
44
case "200":
55
return "Pass"
66
case "400":
7-
raise Exception("Error: Raising 400 from within the Lambda function")
7+
raise Exception("Error: Raising four hundred from within the Lambda function")
88
case "500":
9-
raise Exception("Error: Raising 500 from within the Lambda function")
9+
raise Exception("Error: Raising five hundred from within the Lambda function")
1010
case _:
1111
return "Error Value in the json request should either be 400 or 500 to demonstrate"

0 commit comments

Comments
 (0)