-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportmap.test.js
More file actions
71 lines (58 loc) · 3.37 KB
/
importmap.test.js
File metadata and controls
71 lines (58 loc) · 3.37 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
import '@shgysk8zer0/polyfills';
import { test, describe } from 'node:test';
import assert from 'node:assert';
import { sri, SHA256, SHA384, SHA512 } from './hash.js';
import { Importmap } from './imap.js';
import { IMPORTMAP_EXP } from './consts.js';
import map from './importmap.json' with { type: 'json' };
describe('Test SRI hashing', () => {
const data = 'Hello, World!';
test('Check importmap.resolve()', async () => {
const importmap = new Importmap(map);
await importmap.importLocalPackage();
importmap.baseUrl = 'https://example.com/';
assert.strictEqual(importmap.resolve('@shgysk8zer0/importmap'), importmap.baseUrl + importmap.imports['@shgysk8zer0/importmap'].substring(1));
assert.strictEqual(importmap.resolve('@aegisjsproject/core/parsers/html.js'), importmap.imports['@aegisjsproject/core/'] + 'parsers/html.js');
assert.strictEqual(importmap.resolve('./foo.js'), 'https://example.com/foo.js', 'Relative paths should resolve to full URLs.');
});
test('Check SHA-256 SRI', async () => {
const integrity = await sri(data, { algo: SHA256 });
assert.strictEqual(typeof integrity, 'string');
assert.strictEqual(integrity, 'sha256-3/1gIbsr1bCvZ2KQgJ7DpTGR3YHH9wpLKGiKNiGCmG8=', 'Should match known SHA-256 hash.');
});
test('Check SHA-384 SRI', async () => {
const integrity = await sri(data, { algo: SHA384 });
assert.strictEqual(typeof integrity, 'string');
assert.strictEqual(integrity, 'sha384-VIXMmzNltDBd+06DN+ClmKV0+CQr8XKJ4N1sIKPNRKCJ3harSrMI9j5EsRcOtfUV', 'Should match known SHA-284 hash.');
});
test('Check SHA-512 SRI', async () => {
const integrity = await sri(data, { algo: SHA512 });
assert.strictEqual(typeof integrity, 'string');
assert.strictEqual(integrity, 'sha512-N015SpXNz9izWZMYX++bo2jxYNja9DLQi6nx7R5avmzGkpHg+i/gAGpSVw7xjBne9OYXwzzlLvCm5fvjGMsDhw==', 'Should match known SHA-512 hash.');
});
test('Check default SRI', async () => {
const integrity = await sri(data);
assert.strictEqual(typeof integrity, 'string');
assert.strictEqual(integrity, 'sha384-VIXMmzNltDBd+06DN+ClmKV0+CQr8XKJ4N1sIKPNRKCJ3harSrMI9j5EsRcOtfUV', 'Should match known SHA-284 hash.');
});
test('Check unsupported algorithm', async () => {
await assert.rejects(sri(data, { algo: 'unsupported' }), {
name: 'NotSupportedError',
message: 'Unrecognized algorithm name'
}, 'Should throw NotSupportedError for unsupported algorithm.');
});
test('Check parsing from HTML', () => {
const name = 'importmap';
const nonce = 'abc';
const content = '{}';
const base = `<script type="importmap">${content}</script>`;
const withName = `<script type="importmap" name="${name}">${content}</script>`;
const withNonce = `<script type="importmap" nonce="${nonce}">${content}</script>`;
const fullScript = `<script type="importmap" nonce="${nonce}" name="${name}">${content}</script>`;
const parse = script => ({ ...IMPORTMAP_EXP.exec(script)?.groups ?? {}});
assert.deepStrictEqual(parse(base), { content, name: undefined, nonce: undefined }, 'Base script should only have content.');
assert.deepStrictEqual(parse(withName), { content, nonce: undefined, name }, 'Script should only have content & name.');
assert.deepStrictEqual(parse(withNonce), { content, nonce, name: undefined }, 'Script with nonce should have nonce & content.');
assert.deepStrictEqual(parse(fullScript), { name, nonce, content }, 'Full script should have all attribbutes & content.');
});
});