Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigatewayv2): support for mock integration type #18129

Merged
merged 6 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lambda';
export * from './mock';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
WebSocketRouteIntegration,
WebSocketIntegrationType,
WebSocketRouteIntegrationConfig,
WebSocketRouteIntegrationBindOptions,
} from '@aws-cdk/aws-apigatewayv2';

/**
* Mock WebSocket Integration
*/
export class MockWebSocketIntegration extends WebSocketRouteIntegration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the naming convention we've established, this class should be called WebSocketMockIntegration.


/**
* @param id id of the underlying integration construct
*/
constructor(id: string) {
super(id);
}

bind(options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfig {
options;
return {
type: WebSocketIntegrationType.MOCK,
uri: '',
};
}
}
Original file line number Diff line number Diff line change
@@ -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"
]
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { WebSocketApi, WebSocketStage } from '@aws-cdk/aws-apigatewayv2';
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import { MockWebSocketIntegration } 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 MockWebSocketIntegration('DefaultIntegration') },
});
const stage = new WebSocketStage(stack, 'mystage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
});

webSocketApi.addRoute('sendmessage', { integration: new MockWebSocketIntegration('SendMessageIntegration') });

new CfnOutput(stack, 'ApiEndpoint', { value: stage.url });
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Template } from '@aws-cdk/assertions';
import { WebSocketApi } from '@aws-cdk/aws-apigatewayv2';
import { Stack } from '@aws-cdk/core';
import { MockWebSocketIntegration } from '../../lib';


describe('MockWebSocketIntegration', () => {
test('default', () => {
// GIVEN
const stack = new Stack();

// WHEN
new WebSocketApi(stack, 'Api', {
defaultRouteOptions: { integration: new MockWebSocketIntegration('DefaultIntegration') },
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Integration', {
IntegrationType: 'MOCK',
IntegrationUri: '',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export enum WebSocketIntegrationType {
/**
* AWS Proxy Integration Type
*/
AWS_PROXY = 'AWS_PROXY'
AWS_PROXY = 'AWS_PROXY',
/**
* Mock Integration Type
*/
MOCK = 'MOCK'
}

/**
Expand Down