11cb0ef41Sopenharmony_ciimport { mustCall, spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport { ok, match, notStrictEqual } from 'node:assert';
31cb0ef41Sopenharmony_ciimport { spawn as spawnAsync } from 'node:child_process';
41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cidescribe('ESM: http import via CLI', { concurrency: true }, () => {
91cb0ef41Sopenharmony_ci  const disallowedSpecifier = 'http://example.com';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  it('should throw disallowed error for insecure protocol', async () => {
121cb0ef41Sopenharmony_ci    const { code, stderr } = await spawnPromisified(execPath, [
131cb0ef41Sopenharmony_ci      '--experimental-network-imports',
141cb0ef41Sopenharmony_ci      '--input-type=module',
151cb0ef41Sopenharmony_ci      '--eval',
161cb0ef41Sopenharmony_ci      `import ${JSON.stringify(disallowedSpecifier)}`,
171cb0ef41Sopenharmony_ci    ]);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci    notStrictEqual(code, 0);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
221cb0ef41Sopenharmony_ci    //   …/[eval1] is not supported: http can only be used to load local
231cb0ef41Sopenharmony_ci    // resources (use https instead).
241cb0ef41Sopenharmony_ci    match(stderr, /ERR_NETWORK_IMPORT_DISALLOWED/);
251cb0ef41Sopenharmony_ci    ok(stderr.includes(disallowedSpecifier));
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  it('should throw disallowed error for insecure protocol in REPL', () => {
291cb0ef41Sopenharmony_ci    const child = spawnAsync(execPath, [
301cb0ef41Sopenharmony_ci      '--experimental-network-imports',
311cb0ef41Sopenharmony_ci      '--input-type=module',
321cb0ef41Sopenharmony_ci    ]);
331cb0ef41Sopenharmony_ci    child.stdin.end(`import ${JSON.stringify(disallowedSpecifier)}`);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    let stderr = '';
361cb0ef41Sopenharmony_ci    child.stderr.setEncoding('utf8');
371cb0ef41Sopenharmony_ci    child.stderr.on('data', (data) => stderr += data);
381cb0ef41Sopenharmony_ci    child.on('close', mustCall((code, _signal) => {
391cb0ef41Sopenharmony_ci      notStrictEqual(code, 0);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci      // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
421cb0ef41Sopenharmony_ci      //   …/[stdin] is not supported: http can only be used to load local
431cb0ef41Sopenharmony_ci      // resources (use https instead).
441cb0ef41Sopenharmony_ci      match(stderr, /\[ERR_NETWORK_IMPORT_DISALLOWED\]/);
451cb0ef41Sopenharmony_ci      ok(stderr.includes(disallowedSpecifier));
461cb0ef41Sopenharmony_ci    }));
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci});
49