Skip to content

Commit 7779c14

Browse files
feat(apigatewayv2): support for mock integration type (#18129)
resolves #15008 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a8094c7 commit 7779c14

File tree

6 files changed

+188
-1
lines changed

6 files changed

+188
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './lambda';
2+
export * from './mock';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
WebSocketRouteIntegration,
3+
WebSocketIntegrationType,
4+
WebSocketRouteIntegrationConfig,
5+
WebSocketRouteIntegrationBindOptions,
6+
} from '@aws-cdk/aws-apigatewayv2';
7+
8+
/**
9+
* Mock WebSocket Integration
10+
*/
11+
export class WebSocketMockIntegration extends WebSocketRouteIntegration {
12+
13+
/**
14+
* @param id id of the underlying integration construct
15+
*/
16+
constructor(id: string) {
17+
super(id);
18+
}
19+
20+
bind(options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfig {
21+
options;
22+
return {
23+
type: WebSocketIntegrationType.MOCK,
24+
uri: '',
25+
};
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"Resources": {
3+
"mywsapi32E6CE11": {
4+
"Type": "AWS::ApiGatewayV2::Api",
5+
"Properties": {
6+
"Name": "mywsapi",
7+
"ProtocolType": "WEBSOCKET",
8+
"RouteSelectionExpression": "$request.body.action"
9+
}
10+
},
11+
"mywsapidefaultRouteDefaultIntegrationFFCB3BA9": {
12+
"Type": "AWS::ApiGatewayV2::Integration",
13+
"Properties": {
14+
"ApiId": {
15+
"Ref": "mywsapi32E6CE11"
16+
},
17+
"IntegrationType": "MOCK",
18+
"IntegrationUri": ""
19+
}
20+
},
21+
"mywsapidefaultRouteE9382DF8": {
22+
"Type": "AWS::ApiGatewayV2::Route",
23+
"Properties": {
24+
"ApiId": {
25+
"Ref": "mywsapi32E6CE11"
26+
},
27+
"RouteKey": "$default",
28+
"AuthorizationType": "NONE",
29+
"Target": {
30+
"Fn::Join": [
31+
"",
32+
[
33+
"integrations/",
34+
{
35+
"Ref": "mywsapidefaultRouteDefaultIntegrationFFCB3BA9"
36+
}
37+
]
38+
]
39+
}
40+
}
41+
},
42+
"mywsapisendmessageRouteSendMessageIntegrationD29E12F9": {
43+
"Type": "AWS::ApiGatewayV2::Integration",
44+
"Properties": {
45+
"ApiId": {
46+
"Ref": "mywsapi32E6CE11"
47+
},
48+
"IntegrationType": "MOCK",
49+
"IntegrationUri": ""
50+
}
51+
},
52+
"mywsapisendmessageRouteAE873328": {
53+
"Type": "AWS::ApiGatewayV2::Route",
54+
"Properties": {
55+
"ApiId": {
56+
"Ref": "mywsapi32E6CE11"
57+
},
58+
"RouteKey": "sendmessage",
59+
"AuthorizationType": "NONE",
60+
"Target": {
61+
"Fn::Join": [
62+
"",
63+
[
64+
"integrations/",
65+
{
66+
"Ref": "mywsapisendmessageRouteSendMessageIntegrationD29E12F9"
67+
}
68+
]
69+
]
70+
}
71+
}
72+
},
73+
"mystage114C35EC": {
74+
"Type": "AWS::ApiGatewayV2::Stage",
75+
"Properties": {
76+
"ApiId": {
77+
"Ref": "mywsapi32E6CE11"
78+
},
79+
"StageName": "dev",
80+
"AutoDeploy": true
81+
}
82+
}
83+
},
84+
"Outputs": {
85+
"ApiEndpoint": {
86+
"Value": {
87+
"Fn::Join": [
88+
"",
89+
[
90+
"wss://",
91+
{
92+
"Ref": "mywsapi32E6CE11"
93+
},
94+
".execute-api.",
95+
{
96+
"Ref": "AWS::Region"
97+
},
98+
".",
99+
{
100+
"Ref": "AWS::URLSuffix"
101+
},
102+
"/dev"
103+
]
104+
]
105+
}
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { WebSocketApi, WebSocketStage } from '@aws-cdk/aws-apigatewayv2';
2+
import { App, CfnOutput, Stack } from '@aws-cdk/core';
3+
import { WebSocketMockIntegration } from '../../lib';
4+
5+
/*
6+
* Stack verification steps:
7+
* 1. Verify manually that the integration has type "MOCK"
8+
*/
9+
10+
const app = new App();
11+
const stack = new Stack(app, 'integ-mock-websocket-integration');
12+
13+
const webSocketApi = new WebSocketApi(stack, 'mywsapi', {
14+
defaultRouteOptions: { integration: new WebSocketMockIntegration('DefaultIntegration') },
15+
});
16+
const stage = new WebSocketStage(stack, 'mystage', {
17+
webSocketApi,
18+
stageName: 'dev',
19+
autoDeploy: true,
20+
});
21+
22+
webSocketApi.addRoute('sendmessage', { integration: new WebSocketMockIntegration('SendMessageIntegration') });
23+
24+
new CfnOutput(stack, 'ApiEndpoint', { value: stage.url });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Template } from '@aws-cdk/assertions';
2+
import { WebSocketApi } from '@aws-cdk/aws-apigatewayv2';
3+
import { Stack } from '@aws-cdk/core';
4+
import { WebSocketMockIntegration } from '../../lib';
5+
6+
7+
describe('MockWebSocketIntegration', () => {
8+
test('default', () => {
9+
// GIVEN
10+
const stack = new Stack();
11+
12+
// WHEN
13+
new WebSocketApi(stack, 'Api', {
14+
defaultRouteOptions: { integration: new WebSocketMockIntegration('DefaultIntegration') },
15+
});
16+
17+
// THEN
18+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Integration', {
19+
IntegrationType: 'MOCK',
20+
IntegrationUri: '',
21+
});
22+
});
23+
});

packages/@aws-cdk/aws-apigatewayv2/lib/websocket/integration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export enum WebSocketIntegrationType {
2424
/**
2525
* AWS Proxy Integration Type
2626
*/
27-
AWS_PROXY = 'AWS_PROXY'
27+
AWS_PROXY = 'AWS_PROXY',
28+
/**
29+
* Mock Integration Type
30+
*/
31+
MOCK = 'MOCK'
2832
}
2933

3034
/**

0 commit comments

Comments
 (0)