diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/index.ts b/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/index.ts index 04a64da0c7540..9c6035e3957d4 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/index.ts +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/index.ts @@ -1 +1,2 @@ export * from './lambda'; +export * from './mock'; diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/mock.ts b/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/mock.ts new file mode 100644 index 0000000000000..9c7a83ece4538 --- /dev/null +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/lib/websocket/mock.ts @@ -0,0 +1,27 @@ +import { + WebSocketRouteIntegration, + WebSocketIntegrationType, + WebSocketRouteIntegrationConfig, + WebSocketRouteIntegrationBindOptions, +} from '@aws-cdk/aws-apigatewayv2'; + +/** + * Mock WebSocket Integration + */ +export class WebSocketMockIntegration extends WebSocketRouteIntegration { + + /** + * @param id id of the underlying integration construct + */ + constructor(id: string) { + super(id); + } + + bind(options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfig { + options; + return { + type: WebSocketIntegrationType.MOCK, + uri: '', + }; + } +} diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.expected.json new file mode 100644 index 0000000000000..dede3af2298b4 --- /dev/null +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.expected.json @@ -0,0 +1,108 @@ +{ + "Resources": { + "mywsapi32E6CE11": { + "Type": "AWS::ApiGatewayV2::Api", + "Properties": { + "Name": "mywsapi", + "ProtocolType": "WEBSOCKET", + "RouteSelectionExpression": "$request.body.action" + } + }, + "mywsapidefaultRouteDefaultIntegrationFFCB3BA9": { + "Type": "AWS::ApiGatewayV2::Integration", + "Properties": { + "ApiId": { + "Ref": "mywsapi32E6CE11" + }, + "IntegrationType": "MOCK", + "IntegrationUri": "" + } + }, + "mywsapidefaultRouteE9382DF8": { + "Type": "AWS::ApiGatewayV2::Route", + "Properties": { + "ApiId": { + "Ref": "mywsapi32E6CE11" + }, + "RouteKey": "$default", + "AuthorizationType": "NONE", + "Target": { + "Fn::Join": [ + "", + [ + "integrations/", + { + "Ref": "mywsapidefaultRouteDefaultIntegrationFFCB3BA9" + } + ] + ] + } + } + }, + "mywsapisendmessageRouteSendMessageIntegrationD29E12F9": { + "Type": "AWS::ApiGatewayV2::Integration", + "Properties": { + "ApiId": { + "Ref": "mywsapi32E6CE11" + }, + "IntegrationType": "MOCK", + "IntegrationUri": "" + } + }, + "mywsapisendmessageRouteAE873328": { + "Type": "AWS::ApiGatewayV2::Route", + "Properties": { + "ApiId": { + "Ref": "mywsapi32E6CE11" + }, + "RouteKey": "sendmessage", + "AuthorizationType": "NONE", + "Target": { + "Fn::Join": [ + "", + [ + "integrations/", + { + "Ref": "mywsapisendmessageRouteSendMessageIntegrationD29E12F9" + } + ] + ] + } + } + }, + "mystage114C35EC": { + "Type": "AWS::ApiGatewayV2::Stage", + "Properties": { + "ApiId": { + "Ref": "mywsapi32E6CE11" + }, + "StageName": "dev", + "AutoDeploy": true + } + } + }, + "Outputs": { + "ApiEndpoint": { + "Value": { + "Fn::Join": [ + "", + [ + "wss://", + { + "Ref": "mywsapi32E6CE11" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".", + { + "Ref": "AWS::URLSuffix" + }, + "/dev" + ] + ] + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.ts b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.ts new file mode 100644 index 0000000000000..672378b42d375 --- /dev/null +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/integ.mock.ts @@ -0,0 +1,24 @@ +import { WebSocketApi, WebSocketStage } from '@aws-cdk/aws-apigatewayv2'; +import { App, CfnOutput, Stack } from '@aws-cdk/core'; +import { WebSocketMockIntegration } from '../../lib'; + +/* + * Stack verification steps: + * 1. Verify manually that the integration has type "MOCK" + */ + +const app = new App(); +const stack = new Stack(app, 'integ-mock-websocket-integration'); + +const webSocketApi = new WebSocketApi(stack, 'mywsapi', { + defaultRouteOptions: { integration: new WebSocketMockIntegration('DefaultIntegration') }, +}); +const stage = new WebSocketStage(stack, 'mystage', { + webSocketApi, + stageName: 'dev', + autoDeploy: true, +}); + +webSocketApi.addRoute('sendmessage', { integration: new WebSocketMockIntegration('SendMessageIntegration') }); + +new CfnOutput(stack, 'ApiEndpoint', { value: stage.url }); diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/mock.test.ts b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/mock.test.ts new file mode 100644 index 0000000000000..4bd7eccd9fc7b --- /dev/null +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/websocket/mock.test.ts @@ -0,0 +1,23 @@ +import { Template } from '@aws-cdk/assertions'; +import { WebSocketApi } from '@aws-cdk/aws-apigatewayv2'; +import { Stack } from '@aws-cdk/core'; +import { WebSocketMockIntegration } from '../../lib'; + + +describe('MockWebSocketIntegration', () => { + test('default', () => { + // GIVEN + const stack = new Stack(); + + // WHEN + new WebSocketApi(stack, 'Api', { + defaultRouteOptions: { integration: new WebSocketMockIntegration('DefaultIntegration') }, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Integration', { + IntegrationType: 'MOCK', + IntegrationUri: '', + }); + }); +}); diff --git a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/integration.ts b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/integration.ts index b5366be83f2ba..028dfd07b7a97 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/integration.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/lib/websocket/integration.ts @@ -24,7 +24,11 @@ export enum WebSocketIntegrationType { /** * AWS Proxy Integration Type */ - AWS_PROXY = 'AWS_PROXY' + AWS_PROXY = 'AWS_PROXY', + /** + * Mock Integration Type + */ + MOCK = 'MOCK' } /**