-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathtest-redirects.js
116 lines (108 loc) · 4.33 KB
/
test-redirects.js
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
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
const baseUri = 'http://localhost:8788';
// Test _headers
(async () => {
// test json-ld media type
const url = `${baseUri}/contexts/event.jsonld`;
const resp = await fetch(url);
assert(resp.headers.get('Content-Type') === 'application/ld+json',
`Content-Type for ${url} should be application/ld+json.`);
})();
(async () => {
// test json-ld media type on URLs without extensions
const urls = [
`${baseUri}/contexts/event`,
`${baseUri}/contexts/person`,
`${baseUri}/contexts/place`,
`${baseUri}/contexts/recipe`,
];
Promise.all(urls.map(async (url) => {
const resp = await fetch(url, {redirect: 'manual'});
const contentType = resp.headers.get('Content-Type');
assert(contentType === 'application/ld+json',
`Content-Type for ${url} should be application/ld+json; got ${contentType}.`);
const cors = resp.headers.get('Access-Control-Allow-Origin');
assert(cors === '"*"',
`Header 'Access-Control-Allow-Origin' should be '*'; got ${cors}.`);
}));
})();
(async () => {
// test json-ld media type
const urls = [
`${baseUri}/contexts/event`,
`${baseUri}/contexts/person`,
`${baseUri}/contexts/place`,
`${baseUri}/contexts/recipe`,
];
Promise.all(urls.map(async (url) => {
const resp = await fetch(url, {redirect: 'manual'});
const contentType = resp.headers.get('Content-Type');
assert(contentType === 'application/ld+json',
`Content-Type for ${url} should be application/ld+json; got ${contentType}.`);
const cors = resp.headers.get('Access-Control-Allow-Origin');
assert(cors === '"*"',
`Header 'Access-Control-Allow-Origin' should be '*'; got ${cors}.`);
}));
})();
(async () => {
// test media type for `*.jldte`
const url = `${baseUri}/test-suite/tests/remote-doc-0003-in.jldt`;
const resp = await fetch(url);
const contentType = resp.headers.get('Content-Type');
assert(contentType === 'application/jldTest+json',
`Content-Type for ${url} should be application/jldTest+json; got ${contentType}.`);
})();
(async () => {
// test media type for `*.jldte`
const url = `${baseUri}/test-suite/tests/remote-doc-0004-in.jldte`;
const resp = await fetch(url);
const contentType = resp.headers.get('Content-Type');
assert(contentType === 'application/jldTest',
`Content-Type for ${url} should be application/jldTest; got ${contentType}.`);
})();
(async () => {
// test link headers
const urlsToLinkHeaderValues = {
'/test-suite/tests/remote-doc-0009-in.jsonld':
`<remote-doc-0009-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"`,
'/test-suite/tests/remote-doc-0010-in.json':
`<remote-doc-0010-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"`,
'/test-suite/tests/remote-doc-0011-in.jldt':
`<remote-doc-0011-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"`,
'/test-suite/tests/remote-doc-0012-in.json':
`<remote-doc-0012-context1.jsonld>; rel="http://www.w3.org/ns/json-ld#context", <remote-doc-0012-context2.jsonld>; rel="http://www.w3.org/ns/json-ld#context"`
};
Promise.all(Object.entries(urlsToLinkHeaderValues)
.map(async ([absoultePath, intendedHeaderValue]) => {
const url = `${baseUri}${absoultePath}`;
const resp = await fetch(url);
const actualLinkValue = resp.headers.get('Link');
assert(actualLinkValue === intendedHeaderValue,
`Link header for ${url} should be ${intendedHeaderValue}; got ${actualLinkValue}.`);
})
);
})();
// Test _redirects
(async () => {
const _redirects = await fs.readFile('./_redirects', 'utf-8');
Promise.all(_redirects.split('\n')
.filter((v) => (v[0] !== '#') ? v : null)
.map(async (line) => {
let [source, destination, code] = line.split(/\s/);
if(source.endsWith('*')) {
// remove *
source = source.slice(0, source.length - 1);
// remove :splat
destination = destination.slice(0, destination.length - 6);
}
const url = `${baseUri}${source}`;
const resp = await fetch(url, {redirect: 'manual'});
assert(resp.status === code || 302,
`Should be a 302 redirect.`);
const location = resp.headers.get('Location');
assert(location === destination,
`Old ${source} should redirect to ${destination}, got ${location}`);
})
);
})();