-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
isUrlRequest.test.js
193 lines (174 loc) · 5.21 KB
/
isUrlRequest.test.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
const loaderUtils = require('../');
function ExpectedError(regex) {
this.regex = regex;
}
ExpectedError.prototype.matches = function(err) {
return this.regex.test(err.message);
};
describe('isUrlRequest()', () => {
[
// without root
[['//google.com'], false, 'should be negative for scheme-agnostic urls'],
[['https://google.com'], false, 'should be negative for http urls'],
[['HTTP:https://google.com'], false, 'should be negative for http urls'],
[['https://google.com'], false, 'should be negative for https urls'],
[['HTTPS:https://google.com'], false, 'should be negative for https urls'],
[['chrome-extension:https://'], false, 'should be negative for nonstandard urls'],
[['moz-extension:https://'], false, 'should be negative for nonstandard urls'],
[
['ms-browser-extension:https://'],
false,
'should be negative for nonstandard urls',
],
[['custom-extension:https://'], false, 'should be negative for nonstandard urls'],
[['path/to/thing'], true, 'should be positive for implicit relative urls'],
[['./img.png'], true, 'should be positive for implicit relative urls'],
[['../img.png'], true, 'should be positive for implicit relative urls'],
[
['./img.png?foo=bar#hash'],
true,
'should be positive for implicit relative urls',
],
[
['./path/to/thing'],
true,
'should be positive for explicit relative urls',
],
[['~path/to/thing'], true, 'should be positive for module urls (with ~)'],
[
['some/other/stuff/and/then~path/to/thing'],
true,
'should be positive for module urls with path prefix',
],
[
['./some/other/stuff/and/then~path/to/thing'],
true,
'should be positive for module urls with relative path prefix',
],
[['C:/thing'], true, 'should be positive for linux path with driver'],
[['C:\\thing'], true, 'should be positive for windows path with driver'],
[
['directory/things'],
true,
'should be positive for relative path (linux)',
],
[
['directory\\things'],
true,
'should be positive for relative path (windows)',
],
// with root (normal path)
[
['path/to/thing', 'root/dir'],
true,
'should be positive with root if implicit relative url',
],
[
['./path/to/thing', 'root/dir'],
true,
'should be positive with root if explicit relative url',
],
[
['/path/to/thing', 'root/dir'],
true,
'should be positive with root if root-relative url',
],
// with root (boolean)
[
['/path/to/thing', true],
true,
'should be positive for root-relative if root = `true`',
],
// with root (boolean) on Windows
[
['C:\\path\\to\\thing'],
true,
'should be positive for Windows absolute paths with drive letter',
],
[
['\\\\?\\UNC\\ComputerName\\path\\to\\thing'],
true,
'should be positive for Windows absolute UNC paths',
],
// with root (module)
[
['/path/to/thing', '~'],
true,
'should be positive for module url if root = ~',
],
// with root (module path)
[
['/path/to/thing', '~module'],
true,
'should be positive for module prefixes when root starts with ~',
],
[
['/path/to/thing', '~module/'],
true,
'should be positive for module prefixes (with trailing slash) when root starts with ~',
],
// error cases
[
['/path/to/thing', 1],
new ExpectedError(/unexpected parameters/i),
'should throw an error on invalid root',
],
// empty url
[[''], true, 'should be positive if url is empty'],
// about url
[['about:blank'], false, 'should be negative for about:blank'],
// hash
[['#gradient'], false, 'should be negative for hash url'],
// url
[['//sindresorhus.com'], false, 'should ignore noscheme url'],
[
['//at.alicdn.com/t/font_515771_emcns5054x3whfr.eot'],
false,
'should ignore noscheme url with path',
],
[
['https://example.com/././foo'],
false,
'should ignore absolute url with relative',
],
// non standard protocols
[
['file:https://sindresorhus.com'],
false,
'should ignore non standard protocols (file)',
],
[
['mailto:[email protected]'],
false,
'should ignore non standard protocols (mailto)',
],
[
['data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'],
false,
'should ignore non standard protocols (data)',
],
[
['DATA:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'],
false,
'should ignore non standard protocols (data)',
],
// root-relative url
[['/'], false, 'ignore root-relative url'],
[['//'], false, 'ignore root-relative url 1'],
].forEach((test) => {
it(test[2], () => {
const expected = test[1];
try {
const request = loaderUtils.isUrlRequest.apply(loaderUtils, test[0]);
expect(request).toBe(expected);
} catch (e) {
if (expected instanceof ExpectedError) {
expect(expected.matches(e)).toBe(true);
} else {
throw new Error('should not have thrown an error: ' + e.message);
}
}
});
});
});