From b8aa6ddaea81762e5c8f574f915d31ae50171579 Mon Sep 17 00:00:00 2001 From: usmankb Date: Sun, 3 May 2020 08:28:20 +0530 Subject: [PATCH 1/3] Fix response_parameter being ignored in put_integration_response --- moto/apigateway/models.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index d39b719d6..d1b430068 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -56,8 +56,10 @@ class Deployment(BaseModel, dict): class IntegrationResponse(BaseModel, dict): - def __init__(self, status_code, selection_pattern=None): - self["responseTemplates"] = {"application/json": None} + def __init__(self, status_code, selection_pattern=None, response_templates=None): + if response_templates == None: + response_templates = {"application/json": None} + self["responseTemplates"] = response_templates self["statusCode"] = status_code if selection_pattern: self["selectionPattern"] = selection_pattern @@ -72,8 +74,10 @@ class Integration(BaseModel, dict): self["requestTemplates"] = request_templates self["integrationResponses"] = {"200": IntegrationResponse(200)} - def create_integration_response(self, status_code, selection_pattern): - integration_response = IntegrationResponse(status_code, selection_pattern) + def create_integration_response(self, status_code, selection_pattern, response_templates): + if response_templates == {}: + response_templates = None + integration_response = IntegrationResponse(status_code, selection_pattern, response_templates) self["integrationResponses"][status_code] = integration_response return integration_response @@ -956,7 +960,7 @@ class APIGatewayBackend(BaseBackend): raise InvalidRequestInput() integration = self.get_integration(function_id, resource_id, method_type) integration_response = integration.create_integration_response( - status_code, selection_pattern + status_code, selection_pattern, response_templates ) return integration_response From 1cda64e9a3a190a5caa7f08b5af7b783d335c562 Mon Sep 17 00:00:00 2001 From: usmankb Date: Sun, 3 May 2020 08:31:46 +0530 Subject: [PATCH 2/3] Added tests --- tests/test_apigateway/test_apigateway.py | 58 ++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 7495372d2..0ad815972 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import json import boto3 from freezegun import freeze_time @@ -1229,6 +1230,63 @@ def test_put_integration_response_requires_responseTemplate(): responseTemplates={}, ) +@mock_apigateway +def test_put_integration_response_with_response_template(): + 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" + ) + client.put_integration( + restApiId=api_id, + resourceId=root_id, + httpMethod="GET", + type="HTTP", + uri="http://httpbin.org/robots.txt", + integrationHttpMethod="POST", + ) + + with assert_raises(ClientError) as ex: + client.put_integration_response( + restApiId=api_id, resourceId=root_id, httpMethod="GET", statusCode="200" + ) + + ex.exception.response["Error"]["Code"].should.equal("BadRequestException") + ex.exception.response["Error"]["Message"].should.equal("Invalid request input") + + client.put_integration_response( + restApiId=api_id, + resourceId=root_id, + httpMethod="GET", + statusCode="200", + selectionPattern= "foobar", + responseTemplates={"application/json": json.dumps({"data":"test"})}) + + + response = client.get_integration_response( + restApiId=api_id, resourceId=root_id, httpMethod="GET", 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": json.dumps({"data":"test"})}, + } + ) @mock_apigateway def test_put_integration_validation(): From d6875c25cc369c6704e3d5560045d2d6e080b7f8 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Mon, 4 May 2020 09:27:57 +0100 Subject: [PATCH 3/3] Linting --- moto/apigateway/models.py | 10 +++++++--- tests/test_apigateway/test_apigateway.py | 10 ++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index d1b430068..4513c75ab 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -57,7 +57,7 @@ class Deployment(BaseModel, dict): class IntegrationResponse(BaseModel, dict): def __init__(self, status_code, selection_pattern=None, response_templates=None): - if response_templates == None: + if response_templates is None: response_templates = {"application/json": None} self["responseTemplates"] = response_templates self["statusCode"] = status_code @@ -74,10 +74,14 @@ class Integration(BaseModel, dict): self["requestTemplates"] = request_templates self["integrationResponses"] = {"200": IntegrationResponse(200)} - def create_integration_response(self, status_code, selection_pattern, response_templates): + def create_integration_response( + self, status_code, selection_pattern, response_templates + ): if response_templates == {}: response_templates = None - integration_response = IntegrationResponse(status_code, selection_pattern, response_templates) + integration_response = IntegrationResponse( + status_code, selection_pattern, response_templates + ) self["integrationResponses"][status_code] = integration_response return integration_response diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 0ad815972..295cd1c54 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -1230,6 +1230,7 @@ def test_put_integration_response_requires_responseTemplate(): responseTemplates={}, ) + @mock_apigateway def test_put_integration_response_with_response_template(): client = boto3.client("apigateway", region_name="us-west-2") @@ -1268,9 +1269,9 @@ def test_put_integration_response_with_response_template(): resourceId=root_id, httpMethod="GET", statusCode="200", - selectionPattern= "foobar", - responseTemplates={"application/json": json.dumps({"data":"test"})}) - + selectionPattern="foobar", + responseTemplates={"application/json": json.dumps({"data": "test"})}, + ) response = client.get_integration_response( restApiId=api_id, resourceId=root_id, httpMethod="GET", statusCode="200" @@ -1284,10 +1285,11 @@ def test_put_integration_response_with_response_template(): "statusCode": "200", "selectionPattern": "foobar", "ResponseMetadata": {"HTTPStatusCode": 200}, - "responseTemplates": {"application/json": json.dumps({"data":"test"})}, + "responseTemplates": {"application/json": json.dumps({"data": "test"})}, } ) + @mock_apigateway def test_put_integration_validation(): client = boto3.client("apigateway", region_name="us-west-2")