0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/test/es-module/test-http-imports-cli.mjs
Jacob Smith 447635b440
test: refactor ESM tests to improve performance
PR-URL: https://github.com/nodejs/node/pull/43784
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-07-29 10:42:55 +02:00

49 lines
1.7 KiB
JavaScript

import { mustCall, spawnPromisified } from '../common/index.mjs';
import { ok, match, notStrictEqual } from 'node:assert';
import { spawn as spawnAsync } from 'node:child_process';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
describe('ESM: http import via CLI', { concurrency: true }, () => {
const disallowedSpecifier = 'http://example.com';
it('should throw disallowed error for insecure protocol', async () => {
const { code, stderr } = await spawnPromisified(execPath, [
'--experimental-network-imports',
'--input-type=module',
'--eval',
`import ${JSON.stringify(disallowedSpecifier)}`,
]);
notStrictEqual(code, 0);
// [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
// …/[eval1] is not supported: http can only be used to load local
// resources (use https instead).
match(stderr, /ERR_NETWORK_IMPORT_DISALLOWED/);
ok(stderr.includes(disallowedSpecifier));
});
it('should throw disallowed error for insecure protocol in REPL', () => {
const child = spawnAsync(execPath, [
'--experimental-network-imports',
'--input-type=module',
]);
child.stdin.end(`import ${JSON.stringify(disallowedSpecifier)}`);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => stderr += data);
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
// [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
// …/[stdin] is not supported: http can only be used to load local
// resources (use https instead).
match(stderr, /\[ERR_NETWORK_IMPORT_DISALLOWED\]/);
ok(stderr.includes(disallowedSpecifier));
}));
});
});