'use strict';const { isWindows } = require('../common');const assert = require('assert');const url = require('url');
{ const fileURL = url.pathToFileURL('test/').href; assert.ok(fileURL.startsWith('file:///')); assert.ok(fileURL.endsWith('/'));}
{ const fileURL = url.pathToFileURL('test\\').href; assert.ok(fileURL.startsWith('file:///')); if (isWindows) assert.ok(fileURL.endsWith('/')); else assert.ok(fileURL.endsWith('%5C'));}
{ const fileURL = url.pathToFileURL('test/%').href; assert.ok(fileURL.includes('%25'));}
{ if (isWindows) {
assert.throws(() => url.pathToFileURL('\\\\\\no-server'), { code: 'ERR_INVALID_ARG_VALUE' });
assert.throws(() => url.pathToFileURL('\\\\host'), { code: 'ERR_INVALID_ARG_VALUE' }); } else { const fileURL = url.pathToFileURL('\\\\nas\\share\\path.txt').href; assert.match(fileURL, /file:\/\/.+%5C%5Cnas%5Cshare%5Cpath\.txt$/); }}
{ let testCases; if (isWindows) { testCases = [ { path: 'C:\\foo', expected: 'file:///C:/foo' }, { path: 'C:\\FOO', expected: 'file:///C:/FOO' }, { path: 'C:\\dir\\foo', expected: 'file:///C:/dir/foo' }, { path: 'C:\\dir\\', expected: 'file:///C:/dir/' }, { path: 'C:\\foo.mjs', expected: 'file:///C:/foo.mjs' }, { path: 'C:\\foo bar', expected: 'file:///C:/foo%20bar' }, { path: 'C:\\foo?bar', expected: 'file:///C:/foo%3Fbar' }, { path: 'C:\\foo#bar', expected: 'file:///C:/foo%23bar' }, { path: 'C:\\foo&bar', expected: 'file:///C:/foo&bar' }, { path: 'C:\\foo=bar', expected: 'file:///C:/foo=bar' }, { path: 'C:\\foo:bar', expected: 'file:///C:/foo:bar' }, { path: 'C:\\foo;bar', expected: 'file:///C:/foo;bar' }, { path: 'C:\\foo%bar', expected: 'file:///C:/foo%25bar' }, { path: 'C:\\foo\\bar', expected: 'file:///C:/foo/bar' }, { path: 'C:\\foo\bbar', expected: 'file:///C:/foo%08bar' }, { path: 'C:\\foo\tbar', expected: 'file:///C:/foo%09bar' }, { path: 'C:\\foo\nbar', expected: 'file:///C:/foo%0Abar' }, { path: 'C:\\foo\rbar', expected: 'file:///C:/foo%0Dbar' }, { path: 'C:\\fóóbàr', expected: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' }, { path: 'C:\\€', expected: 'file:///C:/%E2%82%AC' }, { path: 'C:\\🚀', expected: 'file:///C:/%F0%9F%9A%80' }, { path: '\\\\nas\\My Docs\\File.doc', expected: 'file://nas/My%20Docs/File.doc' }, ]; } else { testCases = [ { path: '/foo', expected: 'file:///foo' }, { path: '/FOO', expected: 'file:///FOO' }, { path: '/dir/foo', expected: 'file:///dir/foo' }, { path: '/dir/', expected: 'file:///dir/' }, { path: '/foo.mjs', expected: 'file:///foo.mjs' }, { path: '/foo bar', expected: 'file:///foo%20bar' }, { path: '/foo?bar', expected: 'file:///foo%3Fbar' }, { path: '/foo#bar', expected: 'file:///foo%23bar' }, { path: '/foo&bar', expected: 'file:///foo&bar' }, { path: '/foo=bar', expected: 'file:///foo=bar' }, { path: '/foo:bar', expected: 'file:///foo:bar' }, { path: '/foo;bar', expected: 'file:///foo;bar' }, { path: '/foo%bar', expected: 'file:///foo%25bar' }, { path: '/foo\\bar', expected: 'file:///foo%5Cbar' }, { path: '/foo\bbar', expected: 'file:///foo%08bar' }, { path: '/foo\tbar', expected: 'file:///foo%09bar' }, { path: '/foo\nbar', expected: 'file:///foo%0Abar' }, { path: '/foo\rbar', expected: 'file:///foo%0Dbar' }, { path: '/fóóbàr', expected: 'file:///f%C3%B3%C3%B3b%C3%A0r' }, { path: '/€', expected: 'file:///%E2%82%AC' }, { path: '/🚀', expected: 'file:///%F0%9F%9A%80' }, ]; }
for (const { path, expected } of testCases) { const actual = url.pathToFileURL(path).href; assert.strictEqual(actual, expected); }}