|
1 | 1 | import assert from 'node:assert/strict';
|
| 2 | +import fs from 'node:fs/promises'; |
2 | 3 |
|
3 | 4 | const baseUri = 'http://localhost:8788';
|
4 | 5 |
|
@@ -92,12 +93,24 @@ const baseUri = 'http://localhost:8788';
|
92 | 93 |
|
93 | 94 | // Test _redirects
|
94 | 95 | (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 | + ); |
103 | 116 | })();
|
0 commit comments