Repository navigation
Expand file tree
/
Copy pathuri-pattern.test.js
More file actions
222 lines (176 loc) · 8.18 KB
/
Copy pathuri-pattern.test.js
File metadata and controls
222 lines (176 loc) · 8.18 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import URIPattern from '../../lib/atom/uri-pattern';
describe('URIPattern', function() {
describe('exact', function() {
let exact;
beforeEach(function() {
exact = new URIPattern('atom-github://exact/match');
});
it('matches exact URIs', function() {
assert.isTrue(exact.matches('atom-github://exact/match').ok());
assert.isTrue(exact.matches('atom-github://exact/match/').ok());
});
it('does not match any other URIs', function() {
assert.isFalse(exact.matches('atom-github://exactbutnot').ok());
assert.isFalse(exact.matches('atom-github://exact').ok());
assert.isFalse(exact.matches('https://exact').ok());
assert.isFalse(exact.matches('atom-github://exact/but/not').ok());
assert.isFalse(exact.matches('atom-github://exact/match?no=no').ok());
});
it('does not match undefined or null', function() {
assert.isFalse(exact.matches(undefined).ok());
assert.isFalse(exact.matches(null).ok());
});
it('matches username and password', function() {
const pattern = new URIPattern('proto://user:pass@host/some/path');
assert.isTrue(pattern.matches('proto://user:pass@host/some/path').ok());
assert.isFalse(pattern.matches('proto://other:pass@host/some/path').ok());
assert.isFalse(pattern.matches('proto://user:wrong@host/some/path').ok());
});
it('matches a hash', function() {
const pattern = new URIPattern('proto://host/foo#exact');
assert.isTrue(pattern.matches('proto://host/foo#exact').ok());
assert.isFalse(pattern.matches('proto://host/foo#nope').ok());
});
it('escapes and unescapes dashes', function() {
assert.isTrue(
new URIPattern('atom-github://with-many-dashes')
.matches('atom-github://with-many-dashes')
.ok(),
);
});
});
describe('parameter placeholders', function() {
it('matches a protocol placeholder', function() {
const pattern = new URIPattern('{proto}://host/some/path');
const m = pattern.matches('something://host/some/path');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {proto: 'something'});
});
it('matches an auth username placeholder', function() {
const pattern = new URIPattern('proto://{user}@host/some/path');
const m = pattern.matches('proto://me@host/some/path');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {user: 'me'});
});
it('matches an auth password placeholder', function() {
const pattern = new URIPattern('proto://me:{password}@host/some/path');
const m = pattern.matches('proto://me:swordfish@host/some/path');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {password: 'swordfish'});
});
it('matches a hostname placeholder', function() {
const pattern = new URIPattern('proto://{host}/some/path');
const m = pattern.matches('proto://somewhere.com/some/path');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {host: 'somewhere.com'});
});
it('matches each path placeholder to one path segment', function() {
const pattern = new URIPattern('atom-github://base/exact/{id}');
const m0 = pattern.matches('atom-github://base/exact/0');
assert.isTrue(m0.ok());
assert.deepEqual(m0.getParams(), {id: '0'});
const m1 = pattern.matches('atom-github://base/exact/1');
assert.isTrue(m1.ok());
assert.deepEqual(m1.getParams(), {id: '1'});
assert.isFalse(pattern.matches('atom-github://base/exact/0/more').ok());
});
it('does not match if the expected path segment is absent', function() {
const pattern = new URIPattern('atom-github://base/exact/{id}');
assert.isFalse(pattern.matches('atom-github://base/exact/').ok());
});
it('matches multiple path segments with a splat', function() {
const pattern = new URIPattern('proto://host/root/{rest...}');
const m0 = pattern.matches('proto://host/root');
assert.isTrue(m0.ok());
assert.deepEqual(m0.getParams(), {rest: []});
const m1 = pattern.matches('proto://host/root/a');
assert.isTrue(m1.ok());
assert.deepEqual(m1.getParams(), {rest: ['a']});
const m2 = pattern.matches('proto://host/root/a/b/c');
assert.isTrue(m2.ok());
assert.deepEqual(m2.getParams(), {rest: ['a', 'b', 'c']});
});
it('matches a query string placeholder', function() {
const pattern = new URIPattern('proto://host?p0={zero}&p1={one}');
const m0 = pattern.matches('proto://host?p0=aaa&p1=bbb');
assert.isTrue(m0.ok());
assert.deepEqual(m0.getParams(), {zero: 'aaa', one: 'bbb'});
const m1 = pattern.matches('proto://host?p1=no&p0=yes');
assert.isTrue(m1.ok());
assert.deepEqual(m1.getParams(), {zero: 'yes', one: 'no'});
const m2 = pattern.matches('proto://host?p0=&p1=');
assert.isTrue(m2.ok());
assert.deepEqual(m2.getParams(), {zero: '', one: ''});
assert.isFalse(pattern.matches('proto://host?p0=no').ok());
});
it('does not match a single query string parameter against multiple occurrences', function() {
const pattern = new URIPattern('proto://host?p={single}');
assert.isFalse(pattern.matches('proto://host?p=0&p=1&p=2').ok());
});
it('matches multiple query string parameters with a splat', function() {
const pattern = new URIPattern('proto://host?ps={multi...}');
const m0 = pattern.matches('proto://host');
assert.isTrue(m0.ok());
assert.deepEqual(m0.getParams(), {multi: []});
const m1 = pattern.matches('proto://host?ps=0');
assert.isTrue(m1.ok());
assert.deepEqual(m1.getParams(), {multi: ['0']});
const m2 = pattern.matches('proto://host?ps=0&ps=1&ps=2');
assert.isTrue(m2.ok());
assert.deepEqual(m2.getParams(), {multi: ['0', '1', '2']});
});
it('captures a hash', function() {
const pattern = new URIPattern('proto://host/root#{hash}');
const m = pattern.matches('proto://host/root#value');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {hash: 'value'});
});
it('URI-decodes matched parameters', function() {
const pattern = new URIPattern('proto://host/root/{child}?q={search}');
const m = pattern.matches('proto://host/root/hooray%3E%20for%3C%20encodings?q=%3F%26%3F!');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {child: 'hooray> for< encodings', search: '?&?!'});
});
it('ignores the value of an empty capture', function() {
const pattern = new URIPattern('proto://host/root/{}?q={}#{}');
const m = pattern.matches('proto://host/root/anything?q=at#all');
assert.isTrue(m.ok());
assert.deepEqual(m.getParams(), {});
});
});
it('prints itself as a string for debugging', function() {
assert.strictEqual(
new URIPattern('proto://host/exact').toString(),
'<URIPattern proto://host/exact>',
);
assert.strictEqual(
new URIPattern('proto://host/{param}?q={value}').toString(),
'<URIPattern proto://host/{param}?q={value}>',
);
});
describe('match objects', function() {
it('prints itself as a string for debugging', function() {
const pattern = new URIPattern('proto://host/exact');
const m = pattern.matches('proto://host/exact');
assert.strictEqual(m.toString(), '<URIMatch ok>');
});
it('prints captured values', function() {
const pattern = new URIPattern('proto://host/{capture0}/{capture1}');
const m = pattern.matches('proto://host/first/and%20escaped');
assert.strictEqual(m.toString(), '<URIMatch ok capture0="first" capture1="and escaped">');
});
it('remembers the matched URI', function() {
const pattern = new URIPattern('proto://host/{capture0}/{capture1}');
const m = pattern.matches('proto://host/first/and%20escaped');
assert.strictEqual(m.getURI(), 'proto://host/first/and%20escaped');
});
it('behaves like a nonURIMatch', function() {
const pattern = new URIPattern('proto://yes');
const m = pattern.matches('proto://no');
assert.isFalse(m.ok());
assert.isUndefined(m.getURI());
assert.deepEqual(m.getParams(), {});
assert.strictEqual(m.toString(), '<nonURIMatch>');
});
});
});