summaryrefslogtreecommitdiff
path: root/test/es-module/test-http-imports-cli.mjs
blob: 67cefd69ddd889c7a9b83fcd49a6c15ed9e39535 (plain)
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
import { mustCall } from '../common/index.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

{
  const child = spawn(execPath, [
    '--experimental-network-imports',
    '--input-type=module',
    '-e',
    'import "http://example.com"',
  ]);

  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
    //   …/[eval1] is not supported: http can only be used to load local
    // resources (use https instead).
    match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/);
  }));
}
{
  const child = spawn(execPath, [
    '--experimental-network-imports',
    '--input-type=module',
  ]);
  child.stdin.end('import "http://example.com"');

  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]/);
  }));
}