Skip to content

Commit

Permalink
fix(apig): Move 'selectionPattern to integrationResponses` (aws#1636)
Browse files Browse the repository at this point in the history
The property was modeled in the wrong place, rendering it unusable.
Moved it to the correct location instead, and added a validation test.

Fixes aws#1608
  • Loading branch information
RomainMuller authored and moofish32 committed Feb 5, 2019
1 parent 2cbd2c0 commit e3f5ad3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/@aws-cdk/aws-apigateway/lib/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

0 comments on commit e3f5ad3

Please sign in to comment.