Skip to content

Commit

Permalink
"Issue#15081: StepFunctionsRestApi added CORS enabling with testing. …
Browse files Browse the repository at this point in the history
…Unit Tests complete with 100% coverage. All tests passing."
  • Loading branch information
Saqib Dhuka committed Sep 23, 2021
1 parent 812412a commit ef59b40
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 24 deletions.
48 changes: 25 additions & 23 deletions packages/@aws-cdk/aws-apigateway/lib/integrations/stepFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ export interface StepFunctionsIntegrationOptions extends IntegrationOptions{
*/
readonly proxy?: boolean;

// /**
// * Allow invoking method from AWS Console UI (for testing purposes).
// *
// * This will add another permission to the AWS Step Functions resource policy which
// * will allow the `test-invoke-stage` stage to invoke this handler. If this
// * is set to `false`, the function will only be usable from the deployment
// * endpoint.
// *
// * @default true
// */
// readonly allowTestInvoke?: boolean;
/**
* Check if cors is enabled
* @default false
*/
readonly corsEnabled?: boolean;

}
/**
Expand All @@ -54,20 +48,28 @@ export class StepFunctionsIntegration extends AwsIntegration {
const integResponse = getIntegrationResponse();
const requestTemplate = getRequestTemplates(handler);

super({
proxy: options.proxy,
service: 'states',
action: 'StartSyncExecution',
options: {
credentialsRole: options.credentialsRole,
integrationResponses: integResponse,
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: requestTemplate,
},
if (options.corsEnabled) {
super({
proxy: options.proxy,
service: 'states',
action: 'StartSyncExecution',
options,
});
} else {
super({
proxy: options.proxy,
service: 'states',
action: 'StartSyncExecution',
options: {
credentialsRole: options.credentialsRole,
integrationResponses: integResponse,
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: requestTemplate,
},
});
}

});
this.handler = handler;
// this.enableTest = options.allowTestInvoke ?? true;
}

public bind(method: Method): IntegrationConfig {
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-apigateway/lib/stepFunctions-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ export class StepFunctionsRestApi extends RestApi {
const apiRole = getRole(scope, props);
const methodResp = getMethodResponse();

let corsEnable;

if (props.defaultCorsPreflightOptions !== undefined) {
corsEnable = true;
} else {
corsEnable = false;
}

super(scope, id, {
defaultIntegration: new StepFunctionsIntegration(props.handler, {
credentialsRole: apiRole,
proxy: false, //proxy not avaialble for Step Functions yet
corsEnabled: corsEnable,
}),
...props.options,
...props,
Expand Down
56 changes: 55 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/stepFunctions-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable jest/no-commented-out-tests */
import '@aws-cdk/assert-internal/jest';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import { StateMachine } from '@aws-cdk/aws-stepfunctions';
Expand Down Expand Up @@ -179,4 +178,59 @@ describe('Step Functions api', () => {

});

test('StepFunctionsRestApi defines a REST API with CORS enabled', () => {
const { stack, stateMachine } = givenSetup();

//WHEN
new apigw.StepFunctionsRestApi(stack, 'stepFunctions-rest-api', {
handler: stateMachine,
defaultCorsPreflightOptions: {
allowOrigins: ['https://aws.amazon.com'],
allowMethods: ['GET', 'PUT'],
},
});

//THEN
expect(stack).toHaveResource('AWS::ApiGateway::Method', {
HttpMethod: 'OPTIONS',
ResourceId: {
'Fn::GetAtt': [
'stepFunctionsrestapi72815E22',
'RootResourceId',
],
},
RestApiId: {
Ref: 'stepFunctionsrestapi72815E22',
},
AuthorizationType: 'NONE',
Integration: {
IntegrationResponses: [
{
ResponseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'",
'method.response.header.Access-Control-Allow-Origin': "'https://aws.amazon.com'",
'method.response.header.Vary': "'Origin'",
'method.response.header.Access-Control-Allow-Methods': "'GET,PUT'",
},
StatusCode: '204',
},
],
RequestTemplates: {
'application/json': '{ statusCode: 200 }',
},
Type: 'MOCK',
},
MethodResponses: [
{
ResponseParameters: {
'method.response.header.Access-Control-Allow-Headers': true,
'method.response.header.Access-Control-Allow-Origin': true,
'method.response.header.Vary': true,
'method.response.header.Access-Control-Allow-Methods': true,
},
StatusCode: '204',
},
],
});
});
});

0 comments on commit ef59b40

Please sign in to comment.