APIGateway: add guard against no integration when adding a new response (#5416)

This commit is contained in:
Cesar Alvernaz 2022-08-24 16:38:05 +01:00 committed by GitHub
parent 5519597a68
commit 3e6394e641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -1381,8 +1381,7 @@ class APIGatewayBackend(BaseBackend):
api = self.get_rest_api(function_id)
if resource_id not in api.resources:
raise ResourceIdNotFoundException
resource = api.resources[resource_id]
return resource
return api.resources[resource_id]
def create_resource(self, function_id, parent_resource_id, path_part):
api = self.get_rest_api(function_id)
@ -1643,10 +1642,11 @@ class APIGatewayBackend(BaseBackend):
content_handling,
):
integration = self.get_integration(function_id, resource_id, method_type)
integration_response = integration.create_integration_response(
status_code, selection_pattern, response_templates, content_handling
)
return integration_response
if integration:
return integration.create_integration_response(
status_code, selection_pattern, response_templates, content_handling
)
raise NoIntegrationResponseDefined()
def get_integration_response(
self, function_id, resource_id, method_type, status_code

View File

@ -1055,6 +1055,36 @@ def test_put_integration_response_with_response_template():
)
@mock_apigateway
def test_put_integration_response_but_integration_not_found():
client = boto3.client("apigateway", region_name="us-west-2")
response = client.create_rest_api(name="my_api", description="this is my api")
api_id = response["id"]
resources = client.get_resources(restApiId=api_id)
root_id = [resource for resource in resources["items"] if resource["path"] == "/"][
0
]["id"]
client.put_method(
restApiId=api_id, resourceId=root_id, httpMethod="GET", authorizationType="NONE"
)
client.put_method_response(
restApiId=api_id, resourceId=root_id, httpMethod="GET", statusCode="200"
)
with pytest.raises(ClientError) as ex:
client.put_integration_response(
restApiId=api_id,
resourceId=root_id,
httpMethod="GET",
statusCode="200",
selectionPattern="foobar",
responseTemplates={"application/json": json.dumps({"data": "test"})},
)
ex.value.response["Error"]["Code"].should.equal("NotFoundException")
ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(404)
@mock_apigateway
def test_put_integration_validation():
client = boto3.client("apigateway", region_name="us-west-2")