Skip to content

Commit

Permalink
feat(apigatewayv2): support for mock integration type (#18129)
Browse files Browse the repository at this point in the history
resolves  #15008

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
austinatchley committed Jan 12, 2022
1 parent a8094c7 commit 7779c14
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 1 deletion.
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 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: '',
};
}
}
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 { 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 });
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 { 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: '',
});
});
});
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

0 comments on commit 7779c14

Please sign in to comment.