summaryrefslogtreecommitdiff
path: root/test/parallel/test-url-pathtofileurl.js
blob: 395447ab4cef0d6d9550c0aac85480b59a8ec7ef (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'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'));
}

{
  let testCases;
  if (isWindows) {
    testCases = [
      // Lowercase ascii alpha
      { path: 'C:\\foo', expected: 'file:///C:/foo' },
      // Uppercase ascii alpha
      { path: 'C:\\FOO', expected: 'file:///C:/FOO' },
      // dir
      { path: 'C:\\dir\\foo', expected: 'file:///C:/dir/foo' },
      // trailing separator
      { path: 'C:\\dir\\', expected: 'file:///C:/dir/' },
      // dot
      { path: 'C:\\foo.mjs', expected: 'file:///C:/foo.mjs' },
      // space
      { path: 'C:\\foo bar', expected: 'file:///C:/foo%20bar' },
      // question mark
      { path: 'C:\\foo?bar', expected: 'file:///C:/foo%3Fbar' },
      // number sign
      { path: 'C:\\foo#bar', expected: 'file:///C:/foo%23bar' },
      // ampersand
      { path: 'C:\\foo&bar', expected: 'file:///C:/foo&bar' },
      // equals
      { path: 'C:\\foo=bar', expected: 'file:///C:/foo=bar' },
      // colon
      { path: 'C:\\foo:bar', expected: 'file:///C:/foo:bar' },
      // semicolon
      { path: 'C:\\foo;bar', expected: 'file:///C:/foo;bar' },
      // percent
      { path: 'C:\\foo%bar', expected: 'file:///C:/foo%25bar' },
      // backslash
      { path: 'C:\\foo\\bar', expected: 'file:///C:/foo/bar' },
      // backspace
      { path: 'C:\\foo\bbar', expected: 'file:///C:/foo%08bar' },
      // tab
      { path: 'C:\\foo\tbar', expected: 'file:///C:/foo%09bar' },
      // newline
      { path: 'C:\\foo\nbar', expected: 'file:///C:/foo%0Abar' },
      // carriage return
      { path: 'C:\\foo\rbar', expected: 'file:///C:/foo%0Dbar' },
      // latin1
      { path: 'C:\\fóóbàr', expected: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' },
      // Euro sign (BMP code point)
      { path: 'C:\\€', expected: 'file:///C:/%E2%82%AC' },
      // Rocket emoji (non-BMP code point)
      { path: 'C:\\🚀', expected: 'file:///C:/%F0%9F%9A%80' }
    ];
  } else {
    testCases = [
      // Lowercase ascii alpha
      { path: '/foo', expected: 'file:///foo' },
      // Uppercase ascii alpha
      { path: '/FOO', expected: 'file:///FOO' },
      // dir
      { path: '/dir/foo', expected: 'file:///dir/foo' },
      // trailing separator
      { path: '/dir/', expected: 'file:///dir/' },
      // dot
      { path: '/foo.mjs', expected: 'file:///foo.mjs' },
      // space
      { path: '/foo bar', expected: 'file:///foo%20bar' },
      // question mark
      { path: '/foo?bar', expected: 'file:///foo%3Fbar' },
      // number sign
      { path: '/foo#bar', expected: 'file:///foo%23bar' },
      // ampersand
      { path: '/foo&bar', expected: 'file:///foo&bar' },
      // equals
      { path: '/foo=bar', expected: 'file:///foo=bar' },
      // colon
      { path: '/foo:bar', expected: 'file:///foo:bar' },
      // semicolon
      { path: '/foo;bar', expected: 'file:///foo;bar' },
      // percent
      { path: '/foo%bar', expected: 'file:///foo%25bar' },
      // backslash
      { path: '/foo\\bar', expected: 'file:///foo%5Cbar' },
      // backspace
      { path: '/foo\bbar', expected: 'file:///foo%08bar' },
      // tab
      { path: '/foo\tbar', expected: 'file:///foo%09bar' },
      // newline
      { path: '/foo\nbar', expected: 'file:///foo%0Abar' },
      // carriage return
      { path: '/foo\rbar', expected: 'file:///foo%0Dbar' },
      // latin1
      { path: '/fóóbàr', expected: 'file:///f%C3%B3%C3%B3b%C3%A0r' },
      // Euro sign (BMP code point)
      { path: '/€', expected: 'file:///%E2%82%AC' },
      // Rocket emoji (non-BMP code point)
      { path: '/🚀', expected: 'file:///%F0%9F%9A%80' },
    ];
  }

  for (const { path, expected } of testCases) {
    const actual = url.pathToFileURL(path).href;
    assert.strictEqual(actual, expected);
  }
}