-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
integ.domain-name.ts
210 lines (198 loc) · 6.44 KB
/
integ.domain-name.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager';
import { CfnRecordSet } from 'aws-cdk-lib/aws-route53';
import * as cdk from 'aws-cdk-lib';
import {
IntegTest,
ExpectedResult,
} from '@aws-cdk/integ-tests-alpha';
import { Construct } from 'constructs';
import * as apigw from 'aws-cdk-lib/aws-apigateway';
const domainName = process.env.CDK_INTEG_DOMAIN_NAME || process.env.DOMAIN_NAME;
const hostedZoneId = process.env.CDK_INTEG_HOSTED_ZONE_ID || process.env.HOSTED_ZONE_ID;
const certArn = process.env.CDK_INTEG_CERT_ARN || process.env.CERT_ARN;
if (!domainName || !certArn || !hostedZoneId) {
throw new Error('Env vars DOMAIN_NAME, HOSTED_ZONE_ID, and CERT_ARN must be set');
}
/**
* -------------------------------------------------------
* ------------------------- GIVEN------------------------
* -------------------------------------------------------
*/
const app = new cdk.App();
const testCase = new cdk.Stack(app, 'integ-apigw-domain-name-mapping');
interface ApiProps {
statusCode: string;
path: string;
}
class Api extends Construct {
public readonly restApi: apigw.IRestApi;
private readonly resource: apigw.Resource;
constructor(scope: Construct, id: string, props: ApiProps) {
super(scope, id);
this.restApi = new apigw.RestApi(this, 'IntegApi'+props.statusCode, {
endpointTypes: [apigw.EndpointType.REGIONAL],
});
this.resource = this.restApi.root.addResource(props.path);
const integration = this.createIntegration(props.statusCode);
const options = {
methodResponses: [{
statusCode: props.statusCode,
}],
};
this.restApi.root.addMethod('GET', integration, options);
this.resource.addMethod('GET', integration, options);
}
public addResource(path: string, statusCode: string, resource?: apigw.Resource): void {
const subResource = (resource ?? this.resource).addResource(path);
const integration = this.createIntegration(statusCode);
subResource.addMethod('GET', integration, {
methodResponses: [{ statusCode }],
});
}
public addRootResource(path: string, statusCode: string): apigw.Resource {
const subResource = this.restApi.root.addResource(path);
const integration = this.createIntegration(statusCode);
subResource.addMethod('GET', integration, {
methodResponses: [{ statusCode }],
});
return subResource;
}
private createIntegration(statusCode: string): apigw.MockIntegration {
return new apigw.MockIntegration({
requestTemplates: { 'application/json': `{ statusCode: ${Number(statusCode)} }` },
integrationResponses: [{
statusCode: statusCode,
responseTemplates: {
'application/json': JSON.stringify({ message: 'Hello, world' }),
},
}],
});
}
}
/**
* -------------------------------------------------------
* ------------------------- WHEN ------------------------
* -------------------------------------------------------
*/
const certificate = Certificate.fromCertificateArn(testCase, 'Cert', certArn);
const api1 = new Api(testCase, 'IntegApi1', {
statusCode: '201',
path: 'items',
});
const api2 = new Api(testCase, 'IntegApi2', {
statusCode: '202',
path: 'items',
});
/**
* Test 1
*
* Create an initial BasePathMapping for (none)
* Then use a mixture of `addBasePathMapping` and `addApiMapping`
* to test that they can be used together
*/
const domain = new apigw.DomainName(testCase, 'IntegDomain', {
domainName,
securityPolicy: apigw.SecurityPolicy.TLS_1_2,
certificate,
mapping: api1.restApi,
});
new CfnRecordSet(testCase, 'IntegDomainRecord', {
name: domainName,
type: 'A',
hostedZoneId,
aliasTarget: {
hostedZoneId: domain.domainNameAliasHostedZoneId,
dnsName: domain.domainNameAliasDomainName,
},
});
domain.addBasePathMapping(api1.restApi, {
basePath: 'orders',
});
domain.addApiMapping(api2.restApi.deploymentStage, {
basePath: 'orders/v2',
});
domain.addApiMapping(api1.restApi.deploymentStage, {
basePath: 'orders/v1',
});
/**
* Test 2
*
* Create an initial BasePathMapping for 'orders'
* and then add an ApiMapping for a multi-level path
*/
const secondDomain = new apigw.DomainName(testCase, 'Integ2Domain', {
domainName: `another-${domainName}`,
securityPolicy: apigw.SecurityPolicy.TLS_1_2,
certificate,
mapping: api1.restApi,
basePath: 'orders',
});
new CfnRecordSet(testCase, 'Integ2DomainRecord', {
name: `another-${domainName}`,
type: 'A',
hostedZoneId,
aliasTarget: {
hostedZoneId: secondDomain.domainNameAliasHostedZoneId,
dnsName: secondDomain.domainNameAliasDomainName,
},
});
secondDomain.addApiMapping(api2.restApi.deploymentStage, {
basePath: 'orders/v2',
});
/**
* Test 3
*
* Test that you can create an initial BasePathMapping (none)
* and then add additional base path mappings
*/
const thirdDomain = new apigw.DomainName(testCase, 'Integ3Domain', {
domainName: `yet-another-${domainName}`,
securityPolicy: apigw.SecurityPolicy.TLS_1_2,
certificate,
mapping: api1.restApi,
});
new CfnRecordSet(testCase, 'Integ3DomainRecord', {
name: `yet-another-${domainName}`,
type: 'A',
hostedZoneId,
aliasTarget: {
hostedZoneId: thirdDomain.domainNameAliasHostedZoneId,
dnsName: thirdDomain.domainNameAliasDomainName,
},
});
thirdDomain.addBasePathMapping(api2.restApi, {
basePath: 'v2',
});
/**
* -------------------------------------------------------
* ------------------------- THEN ------------------------
* -------------------------------------------------------
*/
const integ = new IntegTest(app, 'domain-name-mapping-test', {
testCases: [testCase],
enableLookups: true,
});
const api1Invoke = integ.assertions.httpApiCall(`https://${domain.domainName}/orders/v1/items`, { });
api1Invoke.expect(ExpectedResult.objectLike({
body: { message: 'Hello, world' },
ok: true,
status: 201,
}));
const api2Invoke = integ.assertions.httpApiCall(`https://${domain.domainName}/orders/v2/items`, { });
api2Invoke.expect(ExpectedResult.objectLike({
body: { message: 'Hello, world' },
ok: true,
status: 202,
}));
const domain2api1Invoke = integ.assertions.httpApiCall(`https://${secondDomain.domainName}/orders/items`, { });
domain2api1Invoke.expect(ExpectedResult.objectLike({
body: { message: 'Hello, world' },
ok: true,
status: 201,
}));
const domain2api2Invoke = integ.assertions.httpApiCall(`https://${secondDomain.domainName}/orders/v2/items`, { });
domain2api2Invoke.expect(ExpectedResult.objectLike({
body: { message: 'Hello, world' },
ok: true,
status: 202,
}));