From b1d515c9295311170e240cf583251b93e9989b48 Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Wed, 10 Jun 2020 12:28:40 +0530 Subject: [PATCH] =?UTF-8?q?Enhancement=20:=20API-Gateway=20Put=20Integrati?= =?UTF-8?q?on=20Response=20-=20Adding=20support=20f=E2=80=A6=20(#3058)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enhancement : API-Gateway Put Integration Response - Adding support for contentHandling. * Added tests where the contentHandling is None also gets tested. * Linting Co-authored-by: usmankb Co-authored-by: Bert Blommers --- moto/apigateway/models.py | 17 +++++-- moto/apigateway/responses.py | 2 + tests/test_apigateway/test_apigateway.py | 58 ++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 4513c75ab..fbd525df1 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -56,13 +56,21 @@ class Deployment(BaseModel, dict): class IntegrationResponse(BaseModel, dict): - def __init__(self, status_code, selection_pattern=None, response_templates=None): + def __init__( + self, + status_code, + selection_pattern=None, + response_templates=None, + content_handling=None, + ): if response_templates is None: response_templates = {"application/json": None} self["responseTemplates"] = response_templates self["statusCode"] = status_code if selection_pattern: self["selectionPattern"] = selection_pattern + if content_handling: + self["contentHandling"] = content_handling class Integration(BaseModel, dict): @@ -75,12 +83,12 @@ class Integration(BaseModel, dict): self["integrationResponses"] = {"200": IntegrationResponse(200)} def create_integration_response( - self, status_code, selection_pattern, response_templates + self, status_code, selection_pattern, response_templates, content_handling ): if response_templates == {}: response_templates = None integration_response = IntegrationResponse( - status_code, selection_pattern, response_templates + status_code, selection_pattern, response_templates, content_handling ) self["integrationResponses"][status_code] = integration_response return integration_response @@ -959,12 +967,13 @@ class APIGatewayBackend(BaseBackend): status_code, selection_pattern, response_templates, + content_handling, ): if response_templates is None: raise InvalidRequestInput() integration = self.get_integration(function_id, resource_id, method_type) integration_response = integration.create_integration_response( - status_code, selection_pattern, response_templates + status_code, selection_pattern, response_templates, content_handling ) return integration_response diff --git a/moto/apigateway/responses.py b/moto/apigateway/responses.py index 1a7689d28..e3951192b 100644 --- a/moto/apigateway/responses.py +++ b/moto/apigateway/responses.py @@ -387,6 +387,7 @@ class APIGatewayResponse(BaseResponse): elif self.method == "PUT": selection_pattern = self._get_param("selectionPattern") response_templates = self._get_param("responseTemplates") + content_handling = self._get_param("contentHandling") integration_response = self.backend.create_integration_response( function_id, resource_id, @@ -394,6 +395,7 @@ class APIGatewayResponse(BaseResponse): status_code, selection_pattern, response_templates, + content_handling, ) elif self.method == "DELETE": integration_response = self.backend.delete_integration_response( diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 295cd1c54..1c7f6d385 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -544,6 +544,7 @@ def test_integration_response(): selectionPattern="foobar", responseTemplates={}, ) + # this is hard to match against, so remove it response["ResponseMetadata"].pop("HTTPHeaders", None) response["ResponseMetadata"].pop("RetryAttempts", None) @@ -592,6 +593,63 @@ def test_integration_response(): response = client.get_method(restApiId=api_id, resourceId=root_id, httpMethod="GET") response["methodIntegration"]["integrationResponses"].should.equal({}) + # adding a new method and perfomring put intergration with contentHandling as CONVERT_TO_BINARY + client.put_method( + restApiId=api_id, resourceId=root_id, httpMethod="PUT", authorizationType="none" + ) + + client.put_method_response( + restApiId=api_id, resourceId=root_id, httpMethod="PUT", statusCode="200" + ) + + client.put_integration( + restApiId=api_id, + resourceId=root_id, + httpMethod="PUT", + type="HTTP", + uri="http://httpbin.org/robots.txt", + integrationHttpMethod="POST", + ) + + response = client.put_integration_response( + restApiId=api_id, + resourceId=root_id, + httpMethod="PUT", + statusCode="200", + selectionPattern="foobar", + responseTemplates={}, + contentHandling="CONVERT_TO_BINARY", + ) + + # this is hard to match against, so remove it + response["ResponseMetadata"].pop("HTTPHeaders", None) + response["ResponseMetadata"].pop("RetryAttempts", None) + response.should.equal( + { + "statusCode": "200", + "selectionPattern": "foobar", + "ResponseMetadata": {"HTTPStatusCode": 200}, + "responseTemplates": {"application/json": None}, + "contentHandling": "CONVERT_TO_BINARY", + } + ) + + response = client.get_integration_response( + restApiId=api_id, resourceId=root_id, httpMethod="PUT", statusCode="200" + ) + # this is hard to match against, so remove it + response["ResponseMetadata"].pop("HTTPHeaders", None) + response["ResponseMetadata"].pop("RetryAttempts", None) + response.should.equal( + { + "statusCode": "200", + "selectionPattern": "foobar", + "ResponseMetadata": {"HTTPStatusCode": 200}, + "responseTemplates": {"application/json": None}, + "contentHandling": "CONVERT_TO_BINARY", + } + ) + @mock_apigateway @mock_cognitoidp