Skip to content

Commit 4a3c021

Browse files
committed
Test all redirects in _redirects.
1 parent 37c5230 commit 4a3c021

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

test-redirects.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from 'node:assert/strict';
2+
import fs from 'node:fs/promises';
23

34
const baseUri = 'http://localhost:8788';
45

@@ -92,12 +93,24 @@ const baseUri = 'http://localhost:8788';
9293

9394
// Test _redirects
9495
(async () => {
95-
// test that /playground-dev redirects to /playground
96-
const url = `${baseUri}/playground-dev`;
97-
const resp = await fetch(url, {redirect: 'manual'});
98-
assert(resp.status === 302,
99-
`Should be a 302 redirect.`);
100-
const location = resp.headers.get('Location');
101-
assert(location === '/playground',
102-
`Old /playground-dev should redirect to /playground`);
96+
const _redirects = await fs.readFile('./_redirects', 'utf-8');
97+
Promise.all(_redirects.split('\n')
98+
.filter((v) => (v[0] !== '#') ? v : null)
99+
.map(async (line) => {
100+
let [source, destination, code] = line.split(/\s/);
101+
if(source.endsWith('*')) {
102+
// remove *
103+
source = source.slice(0, source.length - 1);
104+
// remove :splat
105+
destination = destination.slice(0, destination.length - 6);
106+
}
107+
const url = `${baseUri}${source}`;
108+
const resp = await fetch(url, {redirect: 'manual'});
109+
assert(resp.status === code || 302,
110+
`Should be a 302 redirect.`);
111+
const location = resp.headers.get('Location');
112+
assert(location === destination,
113+
`Old ${source} should redirect to ${destination}, got ${location}`);
114+
})
115+
);
103116
})();

0 commit comments

Comments
 (0)