This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirect.test.ts
224 lines (194 loc) · 5.84 KB
/
redirect.test.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
expect as expectCDK,
countResources,
haveResourceLike,
} from "@aws-cdk/assert";
import * as ec2 from "@aws-cdk/aws-ec2";
import * as route53 from "@aws-cdk/aws-route53";
import * as cdk from "@aws-cdk/core";
import { Redirector } from "../src";
describe("createVpc", () => {
it("creates exactly one VPC", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "TestStack");
expectCDK(stack).to(countResources("AWS::EC2::VPC", 0));
Redirector.createVpc(stack);
expectCDK(stack).to(countResources("AWS::EC2::VPC", 1));
});
it("fails if called more than once on same context", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "TestStack");
Redirector.createVpc(stack);
expect(() => {
Redirector.createVpc(stack);
}).toThrow();
});
});
describe("Redirector expected resources", () => {
const initResources = (): {
vpc: ec2.Vpc;
stack: cdk.Stack;
hostedZone: route53.HostedZone;
} => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "TestStack");
const vpc = Redirector.createVpc(stack);
const hostedZone = new route53.HostedZone(stack, "HostOnlyZone", {
zoneName: "foo.bar",
});
new Redirector(stack, "HostOnlyRedirect", {
hostedZone,
redirectConfig: { host: "much.wow" },
vpc,
});
return { vpc, stack, hostedZone };
};
it("creates exactly one DNS record for the ALB", () => {
const { stack, hostedZone } = initResources();
expectCDK(stack).to(countResources("AWS::Route53::RecordSet", 1));
expectCDK(stack).to(
haveResourceLike("AWS::Route53::RecordSet", {
Type: "A",
Name: `${hostedZone.zoneName}.`,
})
);
});
it("creates exactly one certificate for the ALB", () => {
const { stack, hostedZone } = initResources();
expectCDK(stack).to(
countResources("AWS::CertificateManager::Certificate", 1)
);
expectCDK(stack).to(
haveResourceLike("AWS::CertificateManager::Certificate", {
DomainName: hostedZone.zoneName,
})
);
});
it("creates exactly one ALB", () => {
const { stack } = initResources();
expectCDK(stack).to(
countResources("AWS::ElasticLoadBalancingV2::LoadBalancer", 1)
);
expectCDK(stack).to(
haveResourceLike("AWS::ElasticLoadBalancingV2::LoadBalancer", {
IpAddressType: "ipv4",
Scheme: "internet-facing",
Type: "application",
})
);
});
it("creates two listeners on the ALB that perform the redirects", () => {
const { stack } = initResources();
expectCDK(stack).to(
countResources("AWS::ElasticLoadBalancingV2::Listener", 2)
);
expectCDK(stack).to(
haveResourceLike("AWS::ElasticLoadBalancingV2::Listener", {
Port: 80,
Protocol: "HTTP",
DefaultActions: [
{
Type: "redirect",
RedirectConfig: {
Port: "443",
Protocol: "HTTPS",
StatusCode: "HTTP_301",
},
},
],
})
);
expectCDK(stack).to(
haveResourceLike("AWS::ElasticLoadBalancingV2::Listener", {
Port: 443,
Protocol: "HTTPS",
DefaultActions: [
{
Type: "redirect",
RedirectConfig: {
Host: "much.wow",
StatusCode: "HTTP_301",
},
},
],
})
);
});
it("does not create any target groups on the ALB", () => {
const { stack } = initResources();
expectCDK(stack).to(
countResources("AWS::ElasticLoadBalancingV2::TargetGroup", 0)
);
});
});
describe("example Redirector stacks", () => {
it("creates a stack with one host-only redirect", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "ExampleStack");
const vpc = Redirector.createVpc(stack);
const hostedZone = new route53.HostedZone(stack, "HostOnlyZone", {
zoneName: "foo.bar",
});
new Redirector(stack, "HostOnlyRedirect", {
hostedZone,
redirectConfig: { host: "much.wow" },
vpc,
});
});
it("creates a stack with one full redirect", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "ExampleStack");
const vpc = Redirector.createVpc(stack);
const hostedZone = new route53.HostedZone(stack, "HostOnlyZone", {
zoneName: "foo.bar",
});
new Redirector(stack, "HostOnlyRedirect", {
hostedZone,
redirectConfig: {
protocol: "https",
host: "much.wow",
path: "/baz/wut",
query: "who=me",
},
vpc,
});
});
it("creates a stack with multiple redirects", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "ExampleStack");
// Create an Amazon VPC with the recommended settings.
const vpc = Redirector.createVpc(stack);
// This redirect only changes the domain and retains all other aspects of each request.
const hostOnlyRedirectZone = new route53.HostedZone(
stack,
"HostOnlyRedirectZone",
{
zoneName: "deprecated-domain.example.com",
}
);
new Redirector(stack, "HostOnlyRedirector", {
hostedZone: hostOnlyRedirectZone,
redirectConfig: { host: "new-domain.example.com" },
vpc,
});
// This redirect sends all requests to a single, static target.
const staticRedirectZone = new route53.HostedZone(
stack,
"StaticRedirectZone",
{
zoneName: "another-deprecated-domain.example.com",
}
);
new Redirector(stack, "StatisRedirector", {
hostedZone: staticRedirectZone,
redirectConfig: {
host: "static-target.example.com",
path: "/path/to/my/target",
query: "",
},
vpc,
});
});
});