1 var pathToRegExp = require('./');
2 var assert = require('assert');
4 describe('path-to-regexp', function () {
5 describe('strings', function () {
6 it('should match simple paths', function () {
8 var m = pathToRegExp('/test', params).exec('/test');
10 assert.equal(params.length, 0);
12 assert.equal(m.length, 1);
13 assert.equal(m[0], '/test');
16 it('should match express format params', function () {
18 var m = pathToRegExp('/:test', params).exec('/pathname');
20 assert.equal(params.length, 1);
21 assert.equal(params[0].name, 'test');
22 assert.equal(params[0].optional, false);
24 assert.equal(m.length, 2);
25 assert.equal(m[0], '/pathname');
26 assert.equal(m[1], 'pathname');
29 it('should do strict matches', function () {
31 var re = pathToRegExp('/:test', params, { strict: true });
34 assert.equal(params.length, 1);
35 assert.equal(params[0].name, 'test');
36 assert.equal(params[0].optional, false);
38 m = re.exec('/route');
40 assert.equal(m.length, 2);
41 assert.equal(m[0], '/route');
42 assert.equal(m[1], 'route');
44 m = re.exec('/route/');
49 it('should do strict matches with trailing slashes', function () {
51 var re = pathToRegExp('/:test/', params, { strict: true });
54 assert.equal(params.length, 1);
55 assert.equal(params[0].name, 'test');
56 assert.equal(params[0].optional, false);
58 m = re.exec('/route');
62 m = re.exec('/route/');
64 assert.equal(m.length, 2);
65 assert.equal(m[0], '/route/');
66 assert.equal(m[1], 'route');
68 m = re.exec('/route//');
73 it('should allow optional express format params', function () {
75 var re = pathToRegExp('/:test?', params);
78 assert.equal(params.length, 1);
79 assert.equal(params[0].name, 'test');
80 assert.equal(params[0].optional, true);
82 m = re.exec('/route');
84 assert.equal(m.length, 2);
85 assert.equal(m[0], '/route');
86 assert.equal(m[1], 'route');
90 assert.equal(m.length, 2);
91 assert.equal(m[0], '/');
92 assert.equal(m[1], undefined);
95 it('should allow express format param regexps', function () {
97 var m = pathToRegExp('/:page(\\d+)', params).exec('/56');
99 assert.equal(params.length, 1);
100 assert.equal(params[0].name, 'page');
101 assert.equal(params[0].optional, false);
103 assert.equal(m.length, 2);
104 assert.equal(m[0], '/56');
105 assert.equal(m[1], '56');
108 it('should match without a prefixed slash', function () {
110 var m = pathToRegExp(':test', params).exec('string');
112 assert.equal(params.length, 1);
113 assert.equal(params[0].name, 'test');
114 assert.equal(params[0].optional, false);
116 assert.equal(m.length, 2);
117 assert.equal(m[0], 'string');
118 assert.equal(m[1], 'string');
121 it('should not match format parts', function () {
123 var m = pathToRegExp('/:test.json', params).exec('/route.json');
125 assert.equal(params.length, 1);
126 assert.equal(params[0].name, 'test');
127 assert.equal(params[0].optional, false);
129 assert.equal(m.length, 2);
130 assert.equal(m[0], '/route.json');
131 assert.equal(m[1], 'route');
134 it('should match format parts', function () {
136 var re = pathToRegExp('/:test.:format', params);
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);
145 m = re.exec('/route.json');
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');
152 m = re.exec('/route');
157 it('should match route parts with a trailing format', function () {
159 var m = pathToRegExp('/:test.json', params).exec('/route.json');
161 assert.equal(params.length, 1);
162 assert.equal(params[0].name, 'test');
163 assert.equal(params[0].optional, false);
165 assert.equal(m.length, 2);
166 assert.equal(m[0], '/route.json');
167 assert.equal(m[1], 'route');
170 it('should match optional trailing routes', function () {
172 var m = pathToRegExp('/test*', params).exec('/test/route');
174 assert.equal(params.length, 0);
176 assert.equal(m.length, 2);
177 assert.equal(m[0], '/test/route');
178 assert.equal(m[1], '/route');
181 it('should match optional trailing routes after a param', function () {
183 var re = pathToRegExp('/:test*', params);
186 assert.equal(params.length, 1);
187 assert.equal(params[0].name, 'test');
188 assert.equal(params[0].optional, false);
190 m = re.exec('/test/route');
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');
197 m = re.exec('/testing');
199 assert.equal(m.length, 3);
200 assert.equal(m[0], '/testing');
201 assert.equal(m[1], 'testing');
202 assert.equal(m[2], '');
205 it('should match optional trailing routes before a format', function () {
207 var re = pathToRegExp('/test*.json', params);
210 assert.equal(params.length, 0);
212 m = re.exec('/test.json');
214 assert.equal(m.length, 2);
215 assert.equal(m[0], '/test.json');
216 assert.equal(m[1], '');
218 m = re.exec('/testing.json');
220 assert.equal(m.length, 2);
221 assert.equal(m[0], '/testing.json');
222 assert.equal(m[1], 'ing');
224 m = re.exec('/test/route.json');
226 assert.equal(m.length, 2);
227 assert.equal(m[0], '/test/route.json');
228 assert.equal(m[1], '/route');
231 it('should match optional trailing routes after a param and before a format', function () {
233 var re = pathToRegExp('/:test*.json', params);
236 assert.equal(params.length, 1);
237 assert.equal(params[0].name, 'test');
238 assert.equal(params[0].optional, false);
240 m = re.exec('/testing.json');
242 assert.equal(m.length, 3);
243 assert.equal(m[0], '/testing.json');
244 assert.equal(m[1], 'testing');
245 assert.equal(m[2], '');
247 m = re.exec('/test/route.json');
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');
254 m = re.exec('.json');
259 it('should match optional trailing routes between a normal param and a format param', function () {
261 var re = pathToRegExp('/:test*.:format', params);
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);
270 m = re.exec('/testing.json');
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');
278 m = re.exec('/test/route.json');
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');
286 m = re.exec('/test');
290 m = re.exec('.json');
295 it('should match optional trailing routes after a param and before an optional format param', function () {
297 var re = pathToRegExp('/:test*.:format?', params);
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);
306 m = re.exec('/testing.json');
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');
314 m = re.exec('/test/route.json');
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');
322 m = re.exec('/test');
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);
330 m = re.exec('.json');
335 it('should match optional trailing routes inside optional express param', function () {
337 var re = pathToRegExp('/:test*?', params);
340 assert.equal(params.length, 1);
341 assert.equal(params[0].name, 'test');
342 assert.equal(params[0].optional, true);
344 m = re.exec('/test/route');
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');
351 m = re.exec('/test');
353 assert.equal(m.length, 3);
354 assert.equal(m[0], '/test');
355 assert.equal(m[1], 'test');
356 assert.equal(m[2], '');
360 assert.equal(m.length, 3);
361 assert.equal(m[0], '/');
362 assert.equal(m[1], undefined);
363 assert.equal(m[2], undefined);
366 it('should do case insensitive matches', function () {
367 var m = pathToRegExp('/test').exec('/TEST');
369 assert.equal(m[0], '/TEST');
372 it('should do case sensitive matches', function () {
373 var re = pathToRegExp('/test', null, { sensitive: true });
376 m = re.exec('/test');
378 assert.equal(m.length, 1);
379 assert.equal(m[0], '/test');
381 m = re.exec('/TEST');
386 it('should do non-ending matches', function () {
388 var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
390 assert.equal(params.length, 1);
391 assert.equal(params[0].name, 'test');
392 assert.equal(params[0].optional, false);
394 assert.equal(m.length, 2);
395 assert.equal(m[0], '/test');
396 assert.equal(m[1], 'test');
399 it('should match trailing slashes in non-ending non-strict mode', function () {
401 var re = pathToRegExp('/:test', params, { end: false });
404 assert.equal(params.length, 1);
405 assert.equal(params[0].name, 'test');
406 assert.equal(params[0].optional, false);
408 m = re.exec('/test/');
410 assert.equal(m.length, 2);
411 assert.equal(m[0], '/test/');
412 assert.equal(m[1], 'test');
415 it('should match trailing slashes in non-ending non-strict mode', function () {
417 var re = pathToRegExp('/route/', params, { end: false });
420 assert.equal(params.length, 0);
422 m = re.exec('/route/');
424 assert.equal(m.length, 1);
425 assert.equal(m[0], '/route/');
427 m = re.exec('/route/test');
429 assert.equal(m.length, 1);
430 assert.equal(m[0], '/route');
432 m = re.exec('/route');
434 assert.equal(m.length, 1);
435 assert.equal(m[0], '/route');
437 m = re.exec('/route//');
439 assert.equal(m.length, 1);
440 assert.equal(m[0], '/route/');
443 it('should match trailing slashing in non-ending strict mode', function () {
445 var re = pathToRegExp('/route/', params, { end: false, strict: true });
447 assert.equal(params.length, 0);
449 m = re.exec('/route/');
451 assert.equal(m.length, 1);
452 assert.equal(m[0], '/route/');
454 m = re.exec('/route/test');
456 assert.equal(m.length, 1);
457 assert.equal(m[0], '/route/');
459 m = re.exec('/route');
463 m = re.exec('/route//');
465 assert.equal(m.length, 1);
466 assert.equal(m[0], '/route/');
469 it('should not match trailing slashes in non-ending strict mode', function () {
471 var re = pathToRegExp('/route', params, { end: false, strict: true });
473 assert.equal(params.length, 0);
475 m = re.exec('/route');
477 assert.equal(m.length, 1);
478 assert.equal(m[0], '/route');
480 m = re.exec('/route/');
482 assert.ok(m.length, 1);
483 assert.equal(m[0], '/route');
486 it('should match text after an express param', function () {
488 var re = pathToRegExp('/(:test)route', params);
490 assert.equal(params.length, 1);
491 assert.equal(params[0].name, 'test');
492 assert.equal(params[0].optional, false);
494 m = re.exec('/route');
498 m = re.exec('/testroute');
500 assert.equal(m.length, 2);
501 assert.equal(m[0], '/testroute');
502 assert.equal(m[1], 'test');
504 m = re.exec('testroute');
509 it('should match text after an optional express param', function () {
511 var re = pathToRegExp('/(:test?)route', params);
514 assert.equal(params.length, 1);
515 assert.equal(params[0].name, 'test');
516 assert.equal(params[0].optional, true);
518 m = re.exec('/route');
520 assert.equal(m.length, 2);
521 assert.equal(m[0], '/route');
522 assert.equal(m[1], undefined);
524 m = re.exec('/testroute');
526 assert.equal(m.length, 2);
527 assert.equal(m[0], '/testroute');
528 assert.equal(m[1], 'test');
530 m = re.exec('route');
535 it('should match optional formats', function () {
537 var re = pathToRegExp('/:test.:format?', params);
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);
546 m = re.exec('/route');
548 assert.equal(m.length, 3);
549 assert.equal(m[0], '/route');
550 assert.equal(m[1], 'route');
551 assert.equal(m[2], undefined);
553 m = re.exec('/route.json');
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');
561 it('should match full paths with format by default', function () {
563 var m = pathToRegExp('/:test', params).exec('/test.json');
565 assert.equal(params.length, 1);
566 assert.equal(params[0].name, 'test');
567 assert.equal(params[0].optional, false);
569 assert.equal(m.length, 2);
570 assert.equal(m[0], '/test.json');
571 assert.equal(m[1], 'test.json');
575 describe('regexps', function () {
576 it('should return the regexp', function () {
577 assert.deepEqual(pathToRegExp(/.*/), /.*/);
581 describe('arrays', function () {
582 it('should join arrays parts', function () {
583 var re = pathToRegExp(['/test', '/route']);
585 assert.ok(re.test('/test'));
586 assert.ok(re.test('/route'));
587 assert.ok(!re.test('/else'));
590 it('should match parts properly', function () {
592 var re = pathToRegExp(['/:test', '/test/:route'], params);
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);
601 m = re.exec('/route');
603 assert.equal(m.length, 3);
604 assert.equal(m[0], '/route');
605 assert.equal(m[1], 'route');
606 assert.equal(m[2], undefined);
608 m = re.exec('/test/path');
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');