Repository navigation
Expand file tree
/
Copy pathredirects.test.js
More file actions
263 lines (221 loc) · 12 KB
/
Copy pathredirects.test.js
File metadata and controls
263 lines (221 loc) · 12 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Unit tests for the CloudFront viewer-request function in redirects.js.
// Run with: node --test redirects.test.js
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
// CloudFront Functions use a synchronous, top-level `handler` function rather
// than module exports. Load the source and evaluate it in a fresh context to
// get a reference we can call.
const source = fs.readFileSync(path.join(__dirname, "redirects.js"), "utf8");
const context = vm.createContext({});
vm.runInContext(source + "\n;this.handler = handler;", context);
const handler = context.handler;
function request(uri, acceptHeader) {
const headers = {};
if (acceptHeader !== undefined) {
headers["accept"] = { value: acceptHeader };
}
return { request: { uri, headers } };
}
function assertRedirect(result, expectedLocation, expectedCacheControl) {
assert.equal(result.statusCode, 301);
assert.equal(result.headers.location.value, expectedLocation);
assert.equal(
result.headers["cache-control"].value,
expectedCacheControl || "max-age=604800",
);
}
function assertPassThrough(result, expectedUri) {
// Pass-through returns the request object (possibly with rewritten uri).
assert.ok(result.uri !== undefined, "expected a request, got a response");
assert.equal(result.uri, expectedUri);
}
test("legacy exact redirect: aws-v6 oidc guide", () => {
const result = handler(request("/registry/packages/aws-v6/how-to-guides/aws-py-oidc-provider-pulumi-cloud/index.html"));
assertRedirect(result, "/docs/esc/guides/configuring-oidc/aws/");
});
test("legacy prefix redirect: aws-v6 -> aws@6.x", () => {
const result = handler(request("/registry/packages/aws-v6/resources/s3/bucket/"));
assertRedirect(result, "/registry/packages/aws@6.x/resources/s3/bucket/");
});
test("legacy prefix redirect: azure-native-v2 -> azure-native@2.x", () => {
const result = handler(request("/registry/packages/azure-native-v2/"));
assertRedirect(result, "/registry/packages/azure-native@2.x/");
});
test("removed install-config page: trailing slash -> package Overview", () => {
const result = handler(request("/registry/packages/aiven/installation-configuration/"));
assertRedirect(result, "/registry/packages/aiven/");
});
test("removed install-config page: no trailing slash -> package Overview", () => {
const result = handler(request("/registry/packages/tls/installation-configuration"));
assertRedirect(result, "/registry/packages/tls/");
});
test("removed install-config page: /index.html -> package Overview", () => {
const result = handler(request("/registry/packages/digitalocean/installation-configuration/index.html"));
assertRedirect(result, "/registry/packages/digitalocean/");
});
test("removed install-config page: /index.md -> package Overview", () => {
// The markdown variant redirects too; the client's Accept header survives
// the 301, so the Overview is served as markdown at the target.
const result = handler(request("/registry/packages/consul/installation-configuration/index.md", "text/markdown"));
assertRedirect(result, "/registry/packages/consul/");
});
test("removed install-config page: .md suffix beats the markdown rewrite", () => {
const result = handler(request("/registry/packages/random/installation-configuration.md"));
assertRedirect(result, "/registry/packages/random/");
});
test("every removed install-config package redirects to its own Overview", () => {
const packages = [
// #12225 -- wholly duplicated by the Overview.
"aiven", "alicloud", "artifactory", "auth0", "cloudinit", "consul",
"dbtcloud", "digitalocean", "random", "tailscale", "tls",
// #12226 -- substantially duplicated; unique content verified obsolete.
"azuredevops", "cloudamqp", "cloudflare", "dnsimple", "docker", "kafka",
"linode", "meraki", "nomad", "okta", "postgresql", "spotinst", "vault",
"vsphere",
// #12227 -- the install list duplicated; the credential prose beside it
// is on the Overview already, or names credentials the provider has
// since renamed, gated or retired.
"confluentcloud", "databricks", "datadog", "f5bigip", "fastly", "github",
"gitlab", "hcloud", "keycloak", "kong", "mailgun", "minio",
"mongodbatlas", "mysql", "newrelic", "ns1", "opsgenie", "pagerduty",
"rabbitmq", "rancher2", "signalfx", "snowflake", "splunk", "venafi",
// Merged into the Overview.
"pulumiservice",
];
for (const pkg of packages) {
const result = handler(request(`/registry/packages/${pkg}/installation-configuration/`));
assertRedirect(result, `/registry/packages/${pkg}/`);
}
});
test("install-config page of a package that still has one -> untouched", () => {
// Only the packages listed in the rule were deleted; every other package's
// Install & config page must still serve normally.
const result = handler(request("/registry/packages/aws/installation-configuration/"));
assertPassThrough(result, "/registry/packages/aws/installation-configuration/");
});
test("removed-package rule does not match a look-alike prefix", () => {
// "randomly" must not be caught by the "random" alternative.
const result = handler(request("/registry/packages/randomly/installation-configuration/"));
assertPassThrough(result, "/registry/packages/randomly/installation-configuration/");
});
test("removed-package rule does not match a hyphenated sibling package", () => {
// "docker-build" and "kafka-connect" are real packages that still have an
// Install & config page. The rule's alternatives are followed by "/", so
// the "docker" and "kafka" entries must not swallow them.
for (const pkg of ["docker-build", "kafka-connect"]) {
const result = handler(request(`/registry/packages/${pkg}/installation-configuration/`));
assertPassThrough(result, `/registry/packages/${pkg}/installation-configuration/`);
}
});
test("removed-package rule does not match a look-alike page name", () => {
const result = handler(request("/registry/packages/random/installation-configuration-old/"));
assertPassThrough(result, "/registry/packages/random/installation-configuration-old/");
});
test("removed-package rule leaves the package Overview alone", () => {
const result = handler(request("/registry/packages/random/"));
assertPassThrough(result, "/registry/packages/random/");
});
test("removed-package rule leaves that package's api-docs alone", () => {
const result = handler(request("/registry/packages/random/api-docs/randompet/"));
assertPassThrough(result, "/registry/packages/random/api-docs/randompet/");
});
test("Accept: text/markdown on directory URL -> URI rewrite to /index.md", () => {
const result = handler(request("/registry/packages/aws/", "text/markdown"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("Accept: text/markdown on /index.html -> URI rewrite to /index.md", () => {
const result = handler(request("/registry/packages/aws/index.html", "text/markdown"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("Accept: text/markdown with q-value still matches", () => {
const result = handler(request("/registry/packages/aws/", "text/html, text/markdown;q=0.9"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("/foo/index.md -> pass through (served directly from origin)", () => {
const result = handler(request("/registry/packages/aws/index.md"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("/foo/index.md + Accept:md -> no double rewrite", () => {
const result = handler(request("/registry/packages/aws/index.md", "text/markdown"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("/foo.md -> internal rewrite to /foo/index.md", () => {
const result = handler(request("/registry/packages/aws.md"));
assertPassThrough(result, "/registry/packages/aws/index.md");
});
test("nested leaf: .md suffix on a how-to-guide", () => {
const result = handler(request("/registry/packages/aws/guides/6-0-migration.md"));
assertPassThrough(result, "/registry/packages/aws/guides/6-0-migration/index.md");
});
test("registry root: /registry/ + Accept -> URI rewrite to /registry/index.md", () => {
const result = handler(request("/registry/", "text/markdown"));
assertPassThrough(result, "/registry/index.md");
});
test("registry root: /registry.md -> internal rewrite to /registry/index.md", () => {
const result = handler(request("/registry.md"));
assertPassThrough(result, "/registry/index.md");
});
test("api-docs directory + Accept: text/markdown -> untouched", () => {
const result = handler(request("/registry/packages/aws/api-docs/s3/bucket/", "text/markdown"));
assertPassThrough(result, "/registry/packages/aws/api-docs/s3/bucket/");
});
test("api-docs .md hand-typed -> untouched (origin will 404)", () => {
const result = handler(request("/registry/packages/aws/api-docs/s3/bucket.md"));
assertPassThrough(result, "/registry/packages/aws/api-docs/s3/bucket.md");
});
test("no Accept, no .md suffix -> untouched", () => {
const result = handler(request("/registry/packages/aws/"));
assertPassThrough(result, "/registry/packages/aws/");
});
test("Accept: application/markdown (non-standard) -> no rewrite", () => {
const result = handler(request("/registry/packages/aws/", "application/markdown"));
assertPassThrough(result, "/registry/packages/aws/");
});
test("Accept: text/html only -> no rewrite", () => {
const result = handler(request("/registry/packages/aws/", "text/html"));
assertPassThrough(result, "/registry/packages/aws/");
});
test("missing Accept header on directory URL -> no rewrite", () => {
const result = handler(request("/registry/packages/aws/"));
assertPassThrough(result, "/registry/packages/aws/");
});
test("out-of-scope path /uploads/foo.md -> untouched", () => {
// Markdown logic scoped to /registry/*; other cache behaviors should pass through.
const result = handler(request("/uploads/something.md"));
assertPassThrough(result, "/uploads/something.md");
});
test("out-of-scope path /fingerprinted/x + Accept -> untouched", () => {
const result = handler(request("/fingerprinted/logos/aws.abc123.svg", "text/markdown"));
assertPassThrough(result, "/fingerprinted/logos/aws.abc123.svg");
});
test("legacy redirect wins over markdown rewrite", () => {
// A legacy aws-v6 .md URL should hit the prefix 301, not the .md rewrite.
const result = handler(request("/registry/packages/aws-v6/index.md"));
assertRedirect(result, "/registry/packages/aws@6.x/index.md");
});
test("Accept normalization: text/markdown bucket collapses to 'text/markdown'", () => {
const event = request("/registry/packages/aws/", "text/html, text/markdown;q=0.9");
handler(event);
assert.equal(event.request.headers["accept"].value, "text/markdown");
});
test("Accept normalization: non-markdown Accept is stripped", () => {
const event = request("/registry/packages/aws/", "text/html,application/xhtml+xml");
handler(event);
assert.equal(event.request.headers["accept"], undefined);
});
test("Accept normalization: missing Accept stays missing", () => {
const event = request("/registry/packages/aws/");
handler(event);
assert.equal(event.request.headers["accept"], undefined);
});
// index.ts ships this file verbatim as CloudFront Function code. CloudFront
// rejects functions over 10 KB, but only at `pulumi up`, which runs post-merge
// after the full site build. 9,000 bytes keeps a margin under the stricter
// reading of "10 KB" (10,000 bytes).
test("redirects.js stays under the CloudFront Function size limit", () => {
const bytes = Buffer.byteLength(source, "utf8");
assert.ok(bytes <= 9000, `redirects.js is ${bytes} bytes; keep it at or under 9,000 (CloudFront caps function code at 10 KB)`);
});