From ffc1641eff2199828b3bd7d34e319d7180af7e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BC=E2=80=8D=F0=9F=92=BB=20Romain=20M?= =?UTF-8?q?arcadier-Muller?= Date: Wed, 30 Jan 2019 10:43:27 +0100 Subject: [PATCH] fix(apig): Move 'selectionPattern` to `integrationResponses` The property was modeled in the wrong place, rendering it unusable. Moved it to the correct location instead, and added a validation test. Fixes #1608 --- .../aws-apigateway/lib/integration.ts | 20 ++++---- .../aws-apigateway/test/test.method.ts | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/@aws-cdk/aws-apigateway/lib/integration.ts b/packages/@aws-cdk/aws-apigateway/lib/integration.ts index 3214f931da67a..1f47868e186f1 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/integration.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/integration.ts @@ -86,15 +86,6 @@ export interface IntegrationOptions { */ integrationResponses?: IntegrationResponse[]; - /** - * The templates that are used to transform the integration response body. - * Specify templates as key-value pairs (string-to-string mappings), with a - * content type as the key and a template as the value. - * - * @see http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html - */ - selectionPattern?: string; - /** * The type of network connection to the integration endpoint. * @default ConnectionType.Internet @@ -243,6 +234,17 @@ export enum ConnectionType { } export interface IntegrationResponse { + /** + * Specifies the regular expression (regex) pattern used to choose an integration response based on the response from + * the back end. For example, if the success response returns nothing and the error response returns some string, you + * could use the ``.+`` regex to match error response. However, make sure that the error response does not contain any + * newline (``\n``) character in such cases. If the back end is an AWS Lambda function, the AWS Lambda function error + * header is matched. For all other HTTP and AWS back ends, the HTTP status code is matched. + * + * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-integration-settings-integration-response.html + */ + selectionPattern?: string; + /** * The status code that API Gateway uses to map the integration response to * a MethodResponse status code. diff --git a/packages/@aws-cdk/aws-apigateway/test/test.method.ts b/packages/@aws-cdk/aws-apigateway/test/test.method.ts index 645d1f9f9f398..c1e0ee124b1d3 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.method.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.method.ts @@ -302,5 +302,51 @@ export = { // THEN test.throws(() => api.root.addMethod('GET', integration), /cannot set 'vpcLink' where 'connectionType' is INTERNET/); test.done(); + }, + + 'multiple integration responses can be used'(test: Test) { // @see https://github.com/awslabs/aws-cdk/issues/1608 + // GIVEN + const stack = new cdk.Stack(); + const api = new apigateway.RestApi(stack, 'test-api', { deploy: false }); + + // WHEN + api.root.addMethod('GET', new apigateway.AwsIntegration({ + service: 'foo-service', + action: 'BarAction', + options: { + integrationResponses: [ + { + statusCode: '200', + responseTemplates: { 'application/json': JSON.stringify({ success: true }) }, + }, + { + selectionPattern: 'Invalid', + statusCode: '503', + responseTemplates: { 'application/json': JSON.stringify({ success: false, message: 'Invalid Request' }) }, + } + ], + } + })); + + // THEN + expect(stack).to(haveResource('AWS::ApiGateway::Method', { + Integration: { + IntegrationHttpMethod: 'POST', + IntegrationResponses: [ + { + ResponseTemplates: { 'application/json': '{"success":true}' }, + StatusCode: '200', + }, + { + ResponseTemplates: { 'application/json': '{"success":false,"message":"Invalid Request"}' }, + SelectionPattern: 'Invalid', + StatusCode: '503', + } + ], + Type: 'AWS', + Uri: { 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':apigateway:', { Ref: 'AWS::Region' }, ':foo-service:action/BarAction']]} + } + })); + test.done(); } };