|
| 1 | +'use strict'; |
| 2 | +const { isWindows } = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const url = require('url'); |
| 5 | + |
| 6 | +function testInvalidArgs(...args) { |
| 7 | + for (const arg of args) { |
| 8 | + assert.throws(() => url.fileURLToPath(arg), { |
| 9 | + code: 'ERR_INVALID_ARG_TYPE' |
| 10 | + }); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +// Input must be string or URL |
| 15 | +testInvalidArgs(null, undefined, 1, {}, true); |
| 16 | + |
| 17 | +// Input must be a file URL |
| 18 | +assert.throws(() => url.fileURLToPath('https://a/b/c'), { |
| 19 | + code: 'ERR_INVALID_URL_SCHEME' |
| 20 | +}); |
| 21 | + |
| 22 | +{ |
| 23 | + const withHost = new URL('file://host/a'); |
| 24 | + |
| 25 | + if (isWindows) { |
| 26 | + assert.strictEqual(url.fileURLToPath(withHost), '\\\\host\\a'); |
| 27 | + } else { |
| 28 | + assert.throws(() => url.fileURLToPath(withHost), { |
| 29 | + code: 'ERR_INVALID_FILE_URL_HOST' |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +{ |
| 35 | + if (isWindows) { |
| 36 | + assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), { |
| 37 | + code: 'ERR_INVALID_FILE_URL_PATH' |
| 38 | + }); |
| 39 | + assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), { |
| 40 | + code: 'ERR_INVALID_FILE_URL_PATH' |
| 41 | + }); |
| 42 | + assert.throws(() => url.fileURLToPath('file:///?:/'), { |
| 43 | + code: 'ERR_INVALID_FILE_URL_PATH' |
| 44 | + }); |
| 45 | + } else { |
| 46 | + assert.throws(() => url.fileURLToPath('file:///a%2F/'), { |
| 47 | + code: 'ERR_INVALID_FILE_URL_PATH' |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +{ |
| 53 | + let testCases; |
| 54 | + if (isWindows) { |
| 55 | + testCases = [ |
| 56 | + // lowercase ascii alpha |
| 57 | + { path: 'C:\\foo', fileURL: 'file:///C:/foo' }, |
| 58 | + // uppercase ascii alpha |
| 59 | + { path: 'C:\\FOO', fileURL: 'file:///C:/FOO' }, |
| 60 | + // dir |
| 61 | + { path: 'C:\\dir\\foo', fileURL: 'file:///C:/dir/foo' }, |
| 62 | + // trailing separator |
| 63 | + { path: 'C:\\dir\\', fileURL: 'file:///C:/dir/' }, |
| 64 | + // dot |
| 65 | + { path: 'C:\\foo.mjs', fileURL: 'file:///C:/foo.mjs' }, |
| 66 | + // space |
| 67 | + { path: 'C:\\foo bar', fileURL: 'file:///C:/foo%20bar' }, |
| 68 | + // question mark |
| 69 | + { path: 'C:\\foo?bar', fileURL: 'file:///C:/foo%3Fbar' }, |
| 70 | + // number sign |
| 71 | + { path: 'C:\\foo#bar', fileURL: 'file:///C:/foo%23bar' }, |
| 72 | + // ampersand |
| 73 | + { path: 'C:\\foo&bar', fileURL: 'file:///C:/foo&bar' }, |
| 74 | + // equals |
| 75 | + { path: 'C:\\foo=bar', fileURL: 'file:///C:/foo=bar' }, |
| 76 | + // colon |
| 77 | + { path: 'C:\\foo:bar', fileURL: 'file:///C:/foo:bar' }, |
| 78 | + // semicolon |
| 79 | + { path: 'C:\\foo;bar', fileURL: 'file:///C:/foo;bar' }, |
| 80 | + // percent |
| 81 | + { path: 'C:\\foo%bar', fileURL: 'file:///C:/foo%25bar' }, |
| 82 | + // backslash |
| 83 | + { path: 'C:\\foo\\bar', fileURL: 'file:///C:/foo/bar' }, |
| 84 | + // backspace |
| 85 | + { path: 'C:\\foo\bbar', fileURL: 'file:///C:/foo%08bar' }, |
| 86 | + // tab |
| 87 | + { path: 'C:\\foo\tbar', fileURL: 'file:///C:/foo%09bar' }, |
| 88 | + // newline |
| 89 | + { path: 'C:\\foo\nbar', fileURL: 'file:///C:/foo%0Abar' }, |
| 90 | + // carriage return |
| 91 | + { path: 'C:\\foo\rbar', fileURL: 'file:///C:/foo%0Dbar' }, |
| 92 | + // latin1 |
| 93 | + { path: 'C:\\fóóbàr', fileURL: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' }, |
| 94 | + // euro sign (BMP code point) |
| 95 | + { path: 'C:\\€', fileURL: 'file:///C:/%E2%82%AC' }, |
| 96 | + // rocket emoji (non-BMP code point) |
| 97 | + { path: 'C:\\🚀', fileURL: 'file:///C:/%F0%9F%9A%80' } |
| 98 | + ]; |
| 99 | + } else { |
| 100 | + testCases = [ |
| 101 | + // lowercase ascii alpha |
| 102 | + { path: '/foo', fileURL: 'file:///foo' }, |
| 103 | + // uppercase ascii alpha |
| 104 | + { path: '/FOO', fileURL: 'file:///FOO' }, |
| 105 | + // dir |
| 106 | + { path: '/dir/foo', fileURL: 'file:///dir/foo' }, |
| 107 | + // trailing separator |
| 108 | + { path: '/dir/', fileURL: 'file:///dir/' }, |
| 109 | + // dot |
| 110 | + { path: '/foo.mjs', fileURL: 'file:///foo.mjs' }, |
| 111 | + // space |
| 112 | + { path: '/foo bar', fileURL: 'file:///foo%20bar' }, |
| 113 | + // question mark |
| 114 | + { path: '/foo?bar', fileURL: 'file:///foo%3Fbar' }, |
| 115 | + // number sign |
| 116 | + { path: '/foo#bar', fileURL: 'file:///foo%23bar' }, |
| 117 | + // ampersand |
| 118 | + { path: '/foo&bar', fileURL: 'file:///foo&bar' }, |
| 119 | + // equals |
| 120 | + { path: '/foo=bar', fileURL: 'file:///foo=bar' }, |
| 121 | + // colon |
| 122 | + { path: '/foo:bar', fileURL: 'file:///foo:bar' }, |
| 123 | + // semicolon |
| 124 | + { path: '/foo;bar', fileURL: 'file:///foo;bar' }, |
| 125 | + // percent |
| 126 | + { path: '/foo%bar', fileURL: 'file:///foo%25bar' }, |
| 127 | + // backslash |
| 128 | + { path: '/foo\\bar', fileURL: 'file:///foo%5Cbar' }, |
| 129 | + // backspace |
| 130 | + { path: '/foo\bbar', fileURL: 'file:///foo%08bar' }, |
| 131 | + // tab |
| 132 | + { path: '/foo\tbar', fileURL: 'file:///foo%09bar' }, |
| 133 | + // newline |
| 134 | + { path: '/foo\nbar', fileURL: 'file:///foo%0Abar' }, |
| 135 | + // carriage return |
| 136 | + { path: '/foo\rbar', fileURL: 'file:///foo%0Dbar' }, |
| 137 | + // latin1 |
| 138 | + { path: '/fóóbàr', fileURL: 'file:///f%C3%B3%C3%B3b%C3%A0r' }, |
| 139 | + // euro sign (BMP code point) |
| 140 | + { path: '/€', fileURL: 'file:///%E2%82%AC' }, |
| 141 | + // rocket emoji (non-BMP code point) |
| 142 | + { path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' }, |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + for (const { path, fileURL } of testCases) { |
| 147 | + const fromString = url.fileURLToPath(fileURL); |
| 148 | + assert.strictEqual(fromString, path); |
| 149 | + const fromURL = url.fileURLToPath(new URL(fileURL)); |
| 150 | + assert.strictEqual(fromURL, path); |
| 151 | + } |
| 152 | +} |
0 commit comments