Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1e6cf0

Browse files
authored
fix response structure for Integration and IntegrationResponse (#3245)
1 parent 0257177 commit a1e6cf0

File tree

2 files changed

+75
-23
lines changed

2 files changed

+75
-23
lines changed

‎localstack/services/apigateway/apigateway_starter.py

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,23 @@ def apigateway_models_resource_delete_integration(self, method_type):
5252

5353
return {}
5454

55-
def apigateway_models_Integration_init(
56-
self, integration_type, uri, http_method,
57-
request_templates=None, pass_through_behavior='WHEN_NO_MATCH', cache_key_parameters=[]
58-
):
59-
super(apigateway_models.Integration, self).__init__()
60-
self['type'] = integration_type
61-
self['uri'] = uri
62-
self['httpMethod'] = http_method
55+
def apigateway_models_Integration_init(self, integration_type, uri, http_method,
56+
request_templates=None, pass_through_behavior='WHEN_NO_MATCH', cache_key_parameters=[], *args, **kwargs):
57+
apigateway_models_Integration_init_orig(
58+
self, integration_type=integration_type, uri=uri, http_method=http_method,
59+
request_templates=request_templates, *args, **kwargs
60+
)
61+
6362
self['passthroughBehavior'] = pass_through_behavior
6463
self['cacheKeyParameters'] = cache_key_parameters
6564
self['cacheNamespace'] = short_uid()
66-
self['timeoutInMillis'] = None
67-
self['integrationResponses'] = {'200': apigateway_models.IntegrationResponse(200)}
65+
66+
# httpMethod not present in response if integration_type is None, verified against AWS
67+
if integration_type == 'MOCK':
68+
self['httpMethod'] = None
6869
if request_templates:
6970
self['requestTemplates'] = request_templates
7071

71-
apigateway_models.Integration.__init__ = apigateway_models_Integration_init
72-
7372
def apigateway_models_backend_put_rest_api(self, function_id, body):
7473
rest_api = self.get_rest_api(function_id)
7574
# Remove default root, then add paths from API spec
@@ -131,6 +130,7 @@ def apigateway_response_restapis_individual(self, request, full_url, headers):
131130

132131
def apigateway_response_resource_methods(self, request, *args, **kwargs):
133132
result = apigateway_response_resource_methods_orig(self, request, *args, **kwargs)
133+
134134
if len(result) != 3:
135135
return result
136136
authorization_type = self._get_param('authorizationType')
@@ -143,24 +143,65 @@ def apigateway_response_resource_methods(self, request, *args, **kwargs):
143143
result = result[0], result[1], json.dumps(data)
144144
return result
145145

146-
apigateway_response_integrations_orig = APIGatewayResponse.integrations
147-
148-
def apigateway_response_integrations(self, request, full_url, headers):
149-
result = apigateway_response_integrations_orig(self, request, full_url, headers)
146+
def apigateway_response_integrations(self, request, *args, **kwargs):
147+
result = apigateway_response_integrations_orig(self, request, *args, **kwargs)
150148
timeout_milliseconds = self._get_param('timeoutInMillis')
149+
request_parameters = self._get_param('requestParameters') or {}
150+
cache_key_parameters = self._get_param('cacheKeyParameters') or []
151+
content_handling = self._get_param('contentHandling')
152+
153+
if self.method == 'PUT':
154+
url_path_parts = self.path.split('/')
155+
function_id = url_path_parts[2]
156+
resource_id = url_path_parts[4]
157+
method_type = url_path_parts[6]
158+
159+
integration_response = self.backend.get_integration(function_id, resource_id, method_type)
160+
161+
integration_response['timeoutInMillis'] = timeout_milliseconds
162+
integration_response['requestParameters'] = request_parameters
163+
integration_response['cacheKeyParameters'] = cache_key_parameters
164+
integration_response['contentHandling'] = content_handling
165+
return 200, {}, json.dumps(integration_response)
166+
167+
return result
168+
169+
def apigateway_response_integration_responses(self, request, *args, **kwargs):
170+
result = apigateway_response_integration_responses_orig(self, request, *args, **kwargs)
171+
response_parameters = self._get_param('responseParameters')
172+
173+
if self.method == 'PUT' and response_parameters:
174+
url_path_parts = self.path.split('/')
175+
function_id = url_path_parts[2]
176+
resource_id = url_path_parts[4]
177+
method_type = url_path_parts[6]
178+
status_code = url_path_parts[9]
179+
180+
integration_response = self.backend.get_integration_response(
181+
function_id, resource_id, method_type, status_code
182+
)
183+
integration_response['responseParameters'] = response_parameters
184+
185+
return 200, {}, json.dumps(integration_response)
186+
187+
return result
151188

152-
if self.method == 'PUT' and timeout_milliseconds:
189+
def apigateway_response_resource_method_responses(self, request, *args, **kwargs):
190+
result = apigateway_response_resource_method_responses_orig(self, request, *args, **kwargs)
191+
response_parameters = self._get_param('responseParameters')
192+
193+
if self.method == 'PUT' and response_parameters:
153194
url_path_parts = self.path.split('/')
154195
function_id = url_path_parts[2]
155196
resource_id = url_path_parts[4]
156197
method_type = url_path_parts[6]
198+
response_code = url_path_parts[8]
199+
200+
method_response = self.backend.get_method_response(function_id, resource_id, method_type, response_code)
157201

158-
resource = self.backend.get_resource(function_id, resource_id)
159-
resource.resource_methods[method_type]['methodIntegration'] = (
160-
resource.resource_methods[method_type].get('methodIntegration') or {})
161-
resource.resource_methods[method_type]['methodIntegration']['timeoutInMillis'] = timeout_milliseconds
202+
method_response['responseParameters'] = response_parameters
162203

163-
return result[0], result[1], json.dumps(resource.resource_methods[method_type]['methodIntegration'])
204+
return 200, {}, json.dumps(method_response)
164205

165206
return result
166207

@@ -177,7 +218,14 @@ def apigateway_response_integrations(self, request, full_url, headers):
177218
apigateway_models.Resource.delete_integration = apigateway_models_resource_delete_integration
178219
apigateway_response_resource_methods_orig = APIGatewayResponse.resource_methods
179220
APIGatewayResponse.resource_methods = apigateway_response_resource_methods
221+
apigateway_response_integrations_orig = APIGatewayResponse.integrations
180222
APIGatewayResponse.integrations = apigateway_response_integrations
223+
apigateway_response_integration_responses_orig = APIGatewayResponse.integration_responses
224+
APIGatewayResponse.integration_responses = apigateway_response_integration_responses
225+
apigateway_response_resource_method_responses_orig = APIGatewayResponse.resource_method_responses
226+
APIGatewayResponse.resource_method_responses = apigateway_response_resource_method_responses
227+
apigateway_models_Integration_init_orig = apigateway_models.Integration.__init__
228+
apigateway_models.Integration.__init__ = apigateway_models_Integration_init
181229

182230

183231
def start_apigateway(port=None, backend_port=None, asynchronous=None, update_listener=None):

‎tests/integration/test_api_gateway.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,16 @@ def test_apigateway_with_lambda_integration(self):
394394
integrationHttpMethod='POST',
395395
type='AWS',
396396
uri=target_uri,
397+
timeoutInMillis=3000,
398+
contentHandling='CONVERT_TO_BINARY',
397399
requestTemplates={
398400
'application/json': '{"param1": "$input.params(\'param1\')"}'
399401
}
400402
)
403+
integration_keys = ['httpMethod', 'type', 'passthroughBehavior', 'cacheKeyParameters', 'uri', 'cacheNamespace',
404+
'timeoutInMillis', 'contentHandling', 'requestParameters']
401405
self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)
402-
for key in ['httpMethod', 'type', 'passthroughBehavior', 'cacheKeyParameters', 'uri', 'cacheNamespace']:
406+
for key in integration_keys:
403407
self.assertIn(key, rs)
404408
self.assertNotIn('responseTemplates', rs)
405409

0 commit comments

Comments
 (0)