socketioMEANseed/server/node_modules/path-to-regexp/test.js
server/node_modules/path-to-regexp/test.jsRaw
1var pathToRegExp = require('./');
2var assert = require('assert');
3
4describe('path-to-regexp', function () {
5 describe('strings', function () {
6 it('should match simple paths', function () {
7 var params = [];
8 var m = pathToRegExp('/test', params).exec('/test');
9
10 assert.equal(params.length, 0);
11
12 assert.equal(m.length, 1);
13 assert.equal(m[0], '/test');
14 });
15
16 it('should match express format params', function () {
17 var params = [];
18 var m = pathToRegExp('/:test', params).exec('/pathname');
19
20 assert.equal(params.length, 1);
21 assert.equal(params[0].name, 'test');
22 assert.equal(params[0].optional, false);
23
24 assert.equal(m.length, 2);
25 assert.equal(m[0], '/pathname');
26 assert.equal(m[1], 'pathname');
27 });
28
29 it('should do strict matches', function () {
30 var params = [];
31 var re = pathToRegExp('/:test', params, { strict: true });
32 var m;
33
34 assert.equal(params.length, 1);
35 assert.equal(params[0].name, 'test');
36 assert.equal(params[0].optional, false);
37
38 m = re.exec('/route');
39
40 assert.equal(m.length, 2);
41 assert.equal(m[0], '/route');
42 assert.equal(m[1], 'route');
43
44 m = re.exec('/route/');
45
46 assert.ok(!m);
47 });
48
49 it('should do strict matches with trailing slashes', function () {
50 var params = [];
51 var re = pathToRegExp('/:test/', params, { strict: true });
52 var m;
53
54 assert.equal(params.length, 1);
55 assert.equal(params[0].name, 'test');
56 assert.equal(params[0].optional, false);
57
58 m = re.exec('/route');
59
60 assert.ok(!m);
61
62 m = re.exec('/route/');
63
64 assert.equal(m.length, 2);
65 assert.equal(m[0], '/route/');
66 assert.equal(m[1], 'route');
67
68 m = re.exec('/route//');
69
70 assert.ok(!m);
71 });
72
73 it('should allow optional express format params', function () {
74 var params = [];
75 var re = pathToRegExp('/:test?', params);
76 var m;
77
78 assert.equal(params.length, 1);
79 assert.equal(params[0].name, 'test');
80 assert.equal(params[0].optional, true);
81
82 m = re.exec('/route');
83
84 assert.equal(m.length, 2);
85 assert.equal(m[0], '/route');
86 assert.equal(m[1], 'route');
87
88 m = re.exec('/');
89
90 assert.equal(m.length, 2);
91 assert.equal(m[0], '/');
92 assert.equal(m[1], undefined);
93 });
94
95 it('should allow express format param regexps', function () {
96 var params = [];
97 var m = pathToRegExp('/:page(\\d+)', params).exec('/56');
98
99 assert.equal(params.length, 1);
100 assert.equal(params[0].name, 'page');
101 assert.equal(params[0].optional, false);
102
103 assert.equal(m.length, 2);
104 assert.equal(m[0], '/56');
105 assert.equal(m[1], '56');
106 });
107
108 it('should match without a prefixed slash', function () {
109 var params = [];
110 var m = pathToRegExp(':test', params).exec('string');
111
112 assert.equal(params.length, 1);
113 assert.equal(params[0].name, 'test');
114 assert.equal(params[0].optional, false);
115
116 assert.equal(m.length, 2);
117 assert.equal(m[0], 'string');
118 assert.equal(m[1], 'string');
119 });
120
121 it('should not match format parts', function () {
122 var params = [];
123 var m = pathToRegExp('/:test.json', params).exec('/route.json');
124
125 assert.equal(params.length, 1);
126 assert.equal(params[0].name, 'test');
127 assert.equal(params[0].optional, false);
128
129 assert.equal(m.length, 2);
130 assert.equal(m[0], '/route.json');
131 assert.equal(m[1], 'route');
132 });
133
134 it('should match format parts', function () {
135 var params = [];
136 var re = pathToRegExp('/:test.:format', params);
137 var m;
138
139 assert.equal(params.length, 2);
140 assert.equal(params[0].name, 'test');
141 assert.equal(params[0].optional, false);
142 assert.equal(params[1].name, 'format');
143 assert.equal(params[1].optional, false);
144
145 m = re.exec('/route.json');
146
147 assert.equal(m.length, 3);
148 assert.equal(m[0], '/route.json');
149 assert.equal(m[1], 'route');
150 assert.equal(m[2], 'json');
151
152 m = re.exec('/route');
153
154 assert.ok(!m);
155 });
156
157 it('should match route parts with a trailing format', function () {
158 var params = [];
159 var m = pathToRegExp('/:test.json', params).exec('/route.json');
160
161 assert.equal(params.length, 1);
162 assert.equal(params[0].name, 'test');
163 assert.equal(params[0].optional, false);
164
165 assert.equal(m.length, 2);
166 assert.equal(m[0], '/route.json');
167 assert.equal(m[1], 'route');
168 });
169
170 it('should match optional trailing routes', function () {
171 var params = [];
172 var m = pathToRegExp('/test*', params).exec('/test/route');
173
174 assert.equal(params.length, 0);
175
176 assert.equal(m.length, 2);
177 assert.equal(m[0], '/test/route');
178 assert.equal(m[1], '/route');
179 });
180
181 it('should match optional trailing routes after a param', function () {
182 var params = [];
183 var re = pathToRegExp('/:test*', params);
184 var m;
185
186 assert.equal(params.length, 1);
187 assert.equal(params[0].name, 'test');
188 assert.equal(params[0].optional, false);
189
190 m = re.exec('/test/route');
191
192 assert.equal(m.length, 3);
193 assert.equal(m[0], '/test/route');
194 assert.equal(m[1], 'test');
195 assert.equal(m[2], '/route');
196
197 m = re.exec('/testing');
198
199 assert.equal(m.length, 3);
200 assert.equal(m[0], '/testing');
201 assert.equal(m[1], 'testing');
202 assert.equal(m[2], '');
203 });
204
205 it('should match optional trailing routes before a format', function () {
206 var params = [];
207 var re = pathToRegExp('/test*.json', params);
208 var m;
209
210 assert.equal(params.length, 0);
211
212 m = re.exec('/test.json');
213
214 assert.equal(m.length, 2);
215 assert.equal(m[0], '/test.json');
216 assert.equal(m[1], '');
217
218 m = re.exec('/testing.json');
219
220 assert.equal(m.length, 2);
221 assert.equal(m[0], '/testing.json');
222 assert.equal(m[1], 'ing');
223
224 m = re.exec('/test/route.json');
225
226 assert.equal(m.length, 2);
227 assert.equal(m[0], '/test/route.json');
228 assert.equal(m[1], '/route');
229 });
230
231 it('should match optional trailing routes after a param and before a format', function () {
232 var params = [];
233 var re = pathToRegExp('/:test*.json', params);
234 var m;
235
236 assert.equal(params.length, 1);
237 assert.equal(params[0].name, 'test');
238 assert.equal(params[0].optional, false);
239
240 m = re.exec('/testing.json');
241
242 assert.equal(m.length, 3);
243 assert.equal(m[0], '/testing.json');
244 assert.equal(m[1], 'testing');
245 assert.equal(m[2], '');
246
247 m = re.exec('/test/route.json');
248
249 assert.equal(m.length, 3);
250 assert.equal(m[0], '/test/route.json');
251 assert.equal(m[1], 'test');
252 assert.equal(m[2], '/route');
253
254 m = re.exec('.json');
255
256 assert.ok(!m);
257 });
258
259 it('should match optional trailing routes between a normal param and a format param', function () {
260 var params = [];
261 var re = pathToRegExp('/:test*.:format', params);
262 var m;
263
264 assert.equal(params.length, 2);
265 assert.equal(params[0].name, 'test');
266 assert.equal(params[0].optional, false);
267 assert.equal(params[1].name, 'format');
268 assert.equal(params[1].optional, false);
269
270 m = re.exec('/testing.json');
271
272 assert.equal(m.length, 4);
273 assert.equal(m[0], '/testing.json');
274 assert.equal(m[1], 'testing');
275 assert.equal(m[2], '');
276 assert.equal(m[3], 'json');
277
278 m = re.exec('/test/route.json');
279
280 assert.equal(m.length, 4);
281 assert.equal(m[0], '/test/route.json');
282 assert.equal(m[1], 'test');
283 assert.equal(m[2], '/route');
284 assert.equal(m[3], 'json');
285
286 m = re.exec('/test');
287
288 assert.ok(!m);
289
290 m = re.exec('.json');
291
292 assert.ok(!m);
293 });
294
295 it('should match optional trailing routes after a param and before an optional format param', function () {
296 var params = [];
297 var re = pathToRegExp('/:test*.:format?', params);
298 var m;
299
300 assert.equal(params.length, 2);
301 assert.equal(params[0].name, 'test');
302 assert.equal(params[0].optional, false);
303 assert.equal(params[1].name, 'format');
304 assert.equal(params[1].optional, true);
305
306 m = re.exec('/testing.json');
307
308 assert.equal(m.length, 4);
309 assert.equal(m[0], '/testing.json');
310 assert.equal(m[1], 'testing');
311 assert.equal(m[2], '');
312 assert.equal(m[3], 'json');
313
314 m = re.exec('/test/route.json');
315
316 assert.equal(m.length, 4);
317 assert.equal(m[0], '/test/route.json');
318 assert.equal(m[1], 'test');
319 assert.equal(m[2], '/route');
320 assert.equal(m[3], 'json');
321
322 m = re.exec('/test');
323
324 assert.equal(m.length, 4);
325 assert.equal(m[0], '/test');
326 assert.equal(m[1], 'test');
327 assert.equal(m[2], '');
328 assert.equal(m[3], undefined);
329
330 m = re.exec('.json');
331
332 assert.ok(!m);
333 });
334
335 it('should match optional trailing routes inside optional express param', function () {
336 var params = [];
337 var re = pathToRegExp('/:test*?', params);
338 var m;
339
340 assert.equal(params.length, 1);
341 assert.equal(params[0].name, 'test');
342 assert.equal(params[0].optional, true);
343
344 m = re.exec('/test/route');
345
346 assert.equal(m.length, 3);
347 assert.equal(m[0], '/test/route');
348 assert.equal(m[1], 'test');
349 assert.equal(m[2], '/route');
350
351 m = re.exec('/test');
352
353 assert.equal(m.length, 3);
354 assert.equal(m[0], '/test');
355 assert.equal(m[1], 'test');
356 assert.equal(m[2], '');
357
358 m = re.exec('/');
359
360 assert.equal(m.length, 3);
361 assert.equal(m[0], '/');
362 assert.equal(m[1], undefined);
363 assert.equal(m[2], undefined);
364 });
365
366 it('should do case insensitive matches', function () {
367 var m = pathToRegExp('/test').exec('/TEST');
368
369 assert.equal(m[0], '/TEST');
370 });
371
372 it('should do case sensitive matches', function () {
373 var re = pathToRegExp('/test', null, { sensitive: true });
374 var m;
375
376 m = re.exec('/test');
377
378 assert.equal(m.length, 1);
379 assert.equal(m[0], '/test');
380
381 m = re.exec('/TEST');
382
383 assert.ok(!m);
384 });
385
386 it('should do non-ending matches', function () {
387 var params = [];
388 var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
389
390 assert.equal(params.length, 1);
391 assert.equal(params[0].name, 'test');
392 assert.equal(params[0].optional, false);
393
394 assert.equal(m.length, 2);
395 assert.equal(m[0], '/test');
396 assert.equal(m[1], 'test');
397 });
398
399 it('should match trailing slashes in non-ending non-strict mode', function () {
400 var params = [];
401 var re = pathToRegExp('/:test', params, { end: false });
402 var m;
403
404 assert.equal(params.length, 1);
405 assert.equal(params[0].name, 'test');
406 assert.equal(params[0].optional, false);
407
408 m = re.exec('/test/');
409
410 assert.equal(m.length, 2);
411 assert.equal(m[0], '/test/');
412 assert.equal(m[1], 'test');
413 });
414
415 it('should match trailing slashes in non-ending non-strict mode', function () {
416 var params = [];
417 var re = pathToRegExp('/route/', params, { end: false });
418 var m;
419
420 assert.equal(params.length, 0);
421
422 m = re.exec('/route/');
423
424 assert.equal(m.length, 1);
425 assert.equal(m[0], '/route/');
426
427 m = re.exec('/route/test');
428
429 assert.equal(m.length, 1);
430 assert.equal(m[0], '/route');
431
432 m = re.exec('/route');
433
434 assert.equal(m.length, 1);
435 assert.equal(m[0], '/route');
436
437 m = re.exec('/route//');
438
439 assert.equal(m.length, 1);
440 assert.equal(m[0], '/route/');
441 });
442
443 it('should match trailing slashing in non-ending strict mode', function () {
444 var params = [];
445 var re = pathToRegExp('/route/', params, { end: false, strict: true });
446
447 assert.equal(params.length, 0);
448
449 m = re.exec('/route/');
450
451 assert.equal(m.length, 1);
452 assert.equal(m[0], '/route/');
453
454 m = re.exec('/route/test');
455
456 assert.equal(m.length, 1);
457 assert.equal(m[0], '/route/');
458
459 m = re.exec('/route');
460
461 assert.ok(!m);
462
463 m = re.exec('/route//');
464
465 assert.equal(m.length, 1);
466 assert.equal(m[0], '/route/');
467 });
468
469 it('should not match trailing slashes in non-ending strict mode', function () {
470 var params = [];
471 var re = pathToRegExp('/route', params, { end: false, strict: true });
472
473 assert.equal(params.length, 0);
474
475 m = re.exec('/route');
476
477 assert.equal(m.length, 1);
478 assert.equal(m[0], '/route');
479
480 m = re.exec('/route/');
481
482 assert.ok(m.length, 1);
483 assert.equal(m[0], '/route');
484 });
485
486 it('should match text after an express param', function () {
487 var params = [];
488 var re = pathToRegExp('/(:test)route', params);
489
490 assert.equal(params.length, 1);
491 assert.equal(params[0].name, 'test');
492 assert.equal(params[0].optional, false);
493
494 m = re.exec('/route');
495
496 assert.ok(!m);
497
498 m = re.exec('/testroute');
499
500 assert.equal(m.length, 2);
501 assert.equal(m[0], '/testroute');
502 assert.equal(m[1], 'test');
503
504 m = re.exec('testroute');
505
506 assert.ok(!m);
507 });
508
509 it('should match text after an optional express param', function () {
510 var params = [];
511 var re = pathToRegExp('/(:test?)route', params);
512 var m;
513
514 assert.equal(params.length, 1);
515 assert.equal(params[0].name, 'test');
516 assert.equal(params[0].optional, true);
517
518 m = re.exec('/route');
519
520 assert.equal(m.length, 2);
521 assert.equal(m[0], '/route');
522 assert.equal(m[1], undefined);
523
524 m = re.exec('/testroute');
525
526 assert.equal(m.length, 2);
527 assert.equal(m[0], '/testroute');
528 assert.equal(m[1], 'test');
529
530 m = re.exec('route');
531
532 assert.ok(!m);
533 });
534
535 it('should match optional formats', function () {
536 var params = [];
537 var re = pathToRegExp('/:test.:format?', params);
538 var m;
539
540 assert.equal(params.length, 2);
541 assert.equal(params[0].name, 'test');
542 assert.equal(params[0].optional, false);
543 assert.equal(params[1].name, 'format');
544 assert.equal(params[1].optional, true);
545
546 m = re.exec('/route');
547
548 assert.equal(m.length, 3);
549 assert.equal(m[0], '/route');
550 assert.equal(m[1], 'route');
551 assert.equal(m[2], undefined);
552
553 m = re.exec('/route.json');
554
555 assert.equal(m.length, 3);
556 assert.equal(m[0], '/route.json');
557 assert.equal(m[1], 'route');
558 assert.equal(m[2], 'json');
559 });
560
561 it('should match full paths with format by default', function () {
562 var params = [];
563 var m = pathToRegExp('/:test', params).exec('/test.json');
564
565 assert.equal(params.length, 1);
566 assert.equal(params[0].name, 'test');
567 assert.equal(params[0].optional, false);
568
569 assert.equal(m.length, 2);
570 assert.equal(m[0], '/test.json');
571 assert.equal(m[1], 'test.json');
572 });
573 });
574
575 describe('regexps', function () {
576 it('should return the regexp', function () {
577 assert.deepEqual(pathToRegExp(/.*/), /.*/);
578 });
579 });
580
581 describe('arrays', function () {
582 it('should join arrays parts', function () {
583 var re = pathToRegExp(['/test', '/route']);
584
585 assert.ok(re.test('/test'));
586 assert.ok(re.test('/route'));
587 assert.ok(!re.test('/else'));
588 });
589
590 it('should match parts properly', function () {
591 var params = [];
592 var re = pathToRegExp(['/:test', '/test/:route'], params);
593 var m;
594
595 assert.equal(params.length, 2);
596 assert.equal(params[0].name, 'test');
597 assert.equal(params[0].optional, false);
598 assert.equal(params[1].name, 'route');
599 assert.equal(params[1].optional, false);
600
601 m = re.exec('/route');
602
603 assert.equal(m.length, 3);
604 assert.equal(m[0], '/route');
605 assert.equal(m[1], 'route');
606 assert.equal(m[2], undefined);
607
608 m = re.exec('/test/path');
609
610 assert.equal(m.length, 3);
611 assert.equal(m[0], '/test/path');
612 assert.equal(m[1], undefined);
613 assert.equal(m[2], 'path');
614 });
615 });
616});