Repository navigation
Expand file tree
/
Copy pathshell-protocol.test.mjs
More file actions
87 lines (80 loc) · 2.12 KB
/
Copy pathshell-protocol.test.mjs
File metadata and controls
87 lines (80 loc) · 2.12 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createRequire } from 'node:module';
import test from 'node:test';
const require = createRequire( import.meta.url );
const {
ERROR_URL,
LOADING_URL,
SHELL_SCHEME,
installShellProtocol,
registerShellScheme,
} = require( '../lib/shell-protocol' );
test( 'registers only the required shell scheme privileges', () => {
let schemes;
registerShellScheme( {
registerSchemesAsPrivileged( value ) {
schemes = value;
},
} );
assert.deepEqual( schemes, [
{
scheme: SHELL_SCHEME,
privileges: {
secure: true,
standard: true,
},
},
] );
} );
test( 'serves only the loading and error pages with the shell CSP', async () => {
const tempRoot = mkdtempSync(
path.join( os.tmpdir(), 'cortext-shell-protocol-' )
);
const loadingPage = path.join( tempRoot, 'loading.html' );
const errorPage = path.join( tempRoot, 'error.html' );
writeFileSync( loadingPage, '<h1>Loading</h1>' );
writeFileSync( errorPage, '<h1>Error</h1>' );
let handler;
try {
installShellProtocol(
{
protocol: {
handle( scheme, installedHandler ) {
assert.equal( scheme, SHELL_SCHEME );
handler = installedHandler;
},
},
},
{ loadingPage, errorPage }
);
for ( const [ url, expected ] of [
[ LOADING_URL, '<h1>Loading</h1>' ],
[ ERROR_URL, '<h1>Error</h1>' ],
] ) {
const response = await handler( { url } );
assert.equal( response.status, 200 );
assert.equal( await response.text(), expected );
assert.equal(
response.headers.get( 'content-security-policy' ),
"default-src 'none'; style-src 'unsafe-inline'"
);
assert.equal(
response.headers.get( 'x-content-type-options' ),
'nosniff'
);
}
for ( const url of [
`${ LOADING_URL }?unexpected=1`,
`${ SHELL_SCHEME }://app/not-found`,
`${ SHELL_SCHEME }://other/loading`,
] ) {
const response = await handler( { url } );
assert.equal( response.status, 404 );
}
} finally {
rmSync( tempRoot, { recursive: true, force: true } );
}
} );