forked from rubensworks/jsonld-context-parser.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupJest.js
88 lines (82 loc) · 2.79 KB
/
setupJest.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
require('cross-fetch/polyfill');
// Mock fetch to our local files
global.fetch = jest.fn().mockImplementation((url) => {
let data;
const filePath = url.replace('http://example.org/', __dirname + '/test/http/');
let headers = { 'Content-Type': 'application/ld+json' };
if (filePath.indexOf('nocontenttype') >= 0) {
headers = {};
}
if (filePath.indexOf('charset') >= 0) {
headers = { 'Content-Type': 'application/ld+json; charset=utf-8' };
}
try {
data = JSON.parse(require('fs').readFileSync(filePath, 'utf8'));
} catch (e) {
// Add two alternate links to a specific HTML files for error validation
if (filePath.endsWith('two-alts.html')) {
headers['Content-Type'] = 'text/html';
headers['Link'] = `<not-exists-1.jsonld>; rel="alternate"; type="application/ld+json", <not-exists-2.jsonld>; rel="alternate"; type="application/ld+json"`;
return Promise.resolve({
json: null,
ok: true,
headers: new Headers(headers),
statusText: 'REDIRECT (setupJest.js)',
});
}
// Add two alternate links to a specific HTML files for error validation
if (filePath.endsWith('unknown-rel.html')) {
headers['Content-Type'] = 'text/html';
headers['Link'] = `<not-exists-1.jsonld>; rel="unknown"; type="application/ld+json"`;
return Promise.resolve({
json: null,
ok: true,
headers: new Headers(headers),
statusText: 'REDIRECT (setupJest.js)',
});
}
// Add alternate links to all HTML files to their corresponding JSON-LD counterpart
if (filePath.endsWith('.html')) {
headers['Content-Type'] = 'text/html';
const lastSlashPos = filePath.lastIndexOf('/');
headers['Link'] = `<${filePath.substr(lastSlashPos + 1, filePath.lastIndexOf('.') - lastSlashPos - 1)}.jsonld>; rel="alternate"; type="application/ld+json"`;
return Promise.resolve({
json: null,
ok: true,
headers: new Headers(headers),
statusText: 'REDIRECT (setupJest.js)',
});
}
// Reply with turtle
if (filePath.endsWith('.ttl')) {
headers['Content-Type'] = 'text/turtle';
return Promise.resolve({
json: null,
ok: true,
headers: new Headers(headers),
statusText: 'TTL (setupJest.js)',
});
}
// Fail with status code
if (filePath.indexOf('statusCode') >= 0) {
return Promise.resolve({
json: null,
ok: false,
headers: new Headers(headers),
status: 500,
});
}
return Promise.resolve({
json: null,
ok: false,
headers: new Headers(headers),
statusText: 'FAIL (setupJest.js)',
});
}
return Promise.resolve({
json: () => data,
ok: true,
headers: new Headers(headers),
statusText: 'All is fine (setupJest.js)',
});
});