You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

616 lines
16 KiB

8 years ago
  1. var pathToRegExp = require('./');
  2. var assert = require('assert');
  3. describe('path-to-regexp', function () {
  4. describe('strings', function () {
  5. it('should match simple paths', function () {
  6. var params = [];
  7. var m = pathToRegExp('/test', params).exec('/test');
  8. assert.equal(params.length, 0);
  9. assert.equal(m.length, 1);
  10. assert.equal(m[0], '/test');
  11. });
  12. it('should match express format params', function () {
  13. var params = [];
  14. var m = pathToRegExp('/:test', params).exec('/pathname');
  15. assert.equal(params.length, 1);
  16. assert.equal(params[0].name, 'test');
  17. assert.equal(params[0].optional, false);
  18. assert.equal(m.length, 2);
  19. assert.equal(m[0], '/pathname');
  20. assert.equal(m[1], 'pathname');
  21. });
  22. it('should do strict matches', function () {
  23. var params = [];
  24. var re = pathToRegExp('/:test', params, { strict: true });
  25. var m;
  26. assert.equal(params.length, 1);
  27. assert.equal(params[0].name, 'test');
  28. assert.equal(params[0].optional, false);
  29. m = re.exec('/route');
  30. assert.equal(m.length, 2);
  31. assert.equal(m[0], '/route');
  32. assert.equal(m[1], 'route');
  33. m = re.exec('/route/');
  34. assert.ok(!m);
  35. });
  36. it('should do strict matches with trailing slashes', function () {
  37. var params = [];
  38. var re = pathToRegExp('/:test/', params, { strict: true });
  39. var m;
  40. assert.equal(params.length, 1);
  41. assert.equal(params[0].name, 'test');
  42. assert.equal(params[0].optional, false);
  43. m = re.exec('/route');
  44. assert.ok(!m);
  45. m = re.exec('/route/');
  46. assert.equal(m.length, 2);
  47. assert.equal(m[0], '/route/');
  48. assert.equal(m[1], 'route');
  49. m = re.exec('/route//');
  50. assert.ok(!m);
  51. });
  52. it('should allow optional express format params', function () {
  53. var params = [];
  54. var re = pathToRegExp('/:test?', params);
  55. var m;
  56. assert.equal(params.length, 1);
  57. assert.equal(params[0].name, 'test');
  58. assert.equal(params[0].optional, true);
  59. m = re.exec('/route');
  60. assert.equal(m.length, 2);
  61. assert.equal(m[0], '/route');
  62. assert.equal(m[1], 'route');
  63. m = re.exec('/');
  64. assert.equal(m.length, 2);
  65. assert.equal(m[0], '/');
  66. assert.equal(m[1], undefined);
  67. });
  68. it('should allow express format param regexps', function () {
  69. var params = [];
  70. var m = pathToRegExp('/:page(\\d+)', params).exec('/56');
  71. assert.equal(params.length, 1);
  72. assert.equal(params[0].name, 'page');
  73. assert.equal(params[0].optional, false);
  74. assert.equal(m.length, 2);
  75. assert.equal(m[0], '/56');
  76. assert.equal(m[1], '56');
  77. });
  78. it('should match without a prefixed slash', function () {
  79. var params = [];
  80. var m = pathToRegExp(':test', params).exec('string');
  81. assert.equal(params.length, 1);
  82. assert.equal(params[0].name, 'test');
  83. assert.equal(params[0].optional, false);
  84. assert.equal(m.length, 2);
  85. assert.equal(m[0], 'string');
  86. assert.equal(m[1], 'string');
  87. });
  88. it('should not match format parts', function () {
  89. var params = [];
  90. var m = pathToRegExp('/:test.json', params).exec('/route.json');
  91. assert.equal(params.length, 1);
  92. assert.equal(params[0].name, 'test');
  93. assert.equal(params[0].optional, false);
  94. assert.equal(m.length, 2);
  95. assert.equal(m[0], '/route.json');
  96. assert.equal(m[1], 'route');
  97. });
  98. it('should match format parts', function () {
  99. var params = [];
  100. var re = pathToRegExp('/:test.:format', params);
  101. var m;
  102. assert.equal(params.length, 2);
  103. assert.equal(params[0].name, 'test');
  104. assert.equal(params[0].optional, false);
  105. assert.equal(params[1].name, 'format');
  106. assert.equal(params[1].optional, false);
  107. m = re.exec('/route.json');
  108. assert.equal(m.length, 3);
  109. assert.equal(m[0], '/route.json');
  110. assert.equal(m[1], 'route');
  111. assert.equal(m[2], 'json');
  112. m = re.exec('/route');
  113. assert.ok(!m);
  114. });
  115. it('should match route parts with a trailing format', function () {
  116. var params = [];
  117. var m = pathToRegExp('/:test.json', params).exec('/route.json');
  118. assert.equal(params.length, 1);
  119. assert.equal(params[0].name, 'test');
  120. assert.equal(params[0].optional, false);
  121. assert.equal(m.length, 2);
  122. assert.equal(m[0], '/route.json');
  123. assert.equal(m[1], 'route');
  124. });
  125. it('should match optional trailing routes', function () {
  126. var params = [];
  127. var m = pathToRegExp('/test*', params).exec('/test/route');
  128. assert.equal(params.length, 0);
  129. assert.equal(m.length, 2);
  130. assert.equal(m[0], '/test/route');
  131. assert.equal(m[1], '/route');
  132. });
  133. it('should match optional trailing routes after a param', function () {
  134. var params = [];
  135. var re = pathToRegExp('/:test*', params);
  136. var m;
  137. assert.equal(params.length, 1);
  138. assert.equal(params[0].name, 'test');
  139. assert.equal(params[0].optional, false);
  140. m = re.exec('/test/route');
  141. assert.equal(m.length, 3);
  142. assert.equal(m[0], '/test/route');
  143. assert.equal(m[1], 'test');
  144. assert.equal(m[2], '/route');
  145. m = re.exec('/testing');
  146. assert.equal(m.length, 3);
  147. assert.equal(m[0], '/testing');
  148. assert.equal(m[1], 'testing');
  149. assert.equal(m[2], '');
  150. });
  151. it('should match optional trailing routes before a format', function () {
  152. var params = [];
  153. var re = pathToRegExp('/test*.json', params);
  154. var m;
  155. assert.equal(params.length, 0);
  156. m = re.exec('/test.json');
  157. assert.equal(m.length, 2);
  158. assert.equal(m[0], '/test.json');
  159. assert.equal(m[1], '');
  160. m = re.exec('/testing.json');
  161. assert.equal(m.length, 2);
  162. assert.equal(m[0], '/testing.json');
  163. assert.equal(m[1], 'ing');
  164. m = re.exec('/test/route.json');
  165. assert.equal(m.length, 2);
  166. assert.equal(m[0], '/test/route.json');
  167. assert.equal(m[1], '/route');
  168. });
  169. it('should match optional trailing routes after a param and before a format', function () {
  170. var params = [];
  171. var re = pathToRegExp('/:test*.json', params);
  172. var m;
  173. assert.equal(params.length, 1);
  174. assert.equal(params[0].name, 'test');
  175. assert.equal(params[0].optional, false);
  176. m = re.exec('/testing.json');
  177. assert.equal(m.length, 3);
  178. assert.equal(m[0], '/testing.json');
  179. assert.equal(m[1], 'testing');
  180. assert.equal(m[2], '');
  181. m = re.exec('/test/route.json');
  182. assert.equal(m.length, 3);
  183. assert.equal(m[0], '/test/route.json');
  184. assert.equal(m[1], 'test');
  185. assert.equal(m[2], '/route');
  186. m = re.exec('.json');
  187. assert.ok(!m);
  188. });
  189. it('should match optional trailing routes between a normal param and a format param', function () {
  190. var params = [];
  191. var re = pathToRegExp('/:test*.:format', params);
  192. var m;
  193. assert.equal(params.length, 2);
  194. assert.equal(params[0].name, 'test');
  195. assert.equal(params[0].optional, false);
  196. assert.equal(params[1].name, 'format');
  197. assert.equal(params[1].optional, false);
  198. m = re.exec('/testing.json');
  199. assert.equal(m.length, 4);
  200. assert.equal(m[0], '/testing.json');
  201. assert.equal(m[1], 'testing');
  202. assert.equal(m[2], '');
  203. assert.equal(m[3], 'json');
  204. m = re.exec('/test/route.json');
  205. assert.equal(m.length, 4);
  206. assert.equal(m[0], '/test/route.json');
  207. assert.equal(m[1], 'test');
  208. assert.equal(m[2], '/route');
  209. assert.equal(m[3], 'json');
  210. m = re.exec('/test');
  211. assert.ok(!m);
  212. m = re.exec('.json');
  213. assert.ok(!m);
  214. });
  215. it('should match optional trailing routes after a param and before an optional format param', function () {
  216. var params = [];
  217. var re = pathToRegExp('/:test*.:format?', params);
  218. var m;
  219. assert.equal(params.length, 2);
  220. assert.equal(params[0].name, 'test');
  221. assert.equal(params[0].optional, false);
  222. assert.equal(params[1].name, 'format');
  223. assert.equal(params[1].optional, true);
  224. m = re.exec('/testing.json');
  225. assert.equal(m.length, 4);
  226. assert.equal(m[0], '/testing.json');
  227. assert.equal(m[1], 'testing');
  228. assert.equal(m[2], '');
  229. assert.equal(m[3], 'json');
  230. m = re.exec('/test/route.json');
  231. assert.equal(m.length, 4);
  232. assert.equal(m[0], '/test/route.json');
  233. assert.equal(m[1], 'test');
  234. assert.equal(m[2], '/route');
  235. assert.equal(m[3], 'json');
  236. m = re.exec('/test');
  237. assert.equal(m.length, 4);
  238. assert.equal(m[0], '/test');
  239. assert.equal(m[1], 'test');
  240. assert.equal(m[2], '');
  241. assert.equal(m[3], undefined);
  242. m = re.exec('.json');
  243. assert.ok(!m);
  244. });
  245. it('should match optional trailing routes inside optional express param', function () {
  246. var params = [];
  247. var re = pathToRegExp('/:test*?', params);
  248. var m;
  249. assert.equal(params.length, 1);
  250. assert.equal(params[0].name, 'test');
  251. assert.equal(params[0].optional, true);
  252. m = re.exec('/test/route');
  253. assert.equal(m.length, 3);
  254. assert.equal(m[0], '/test/route');
  255. assert.equal(m[1], 'test');
  256. assert.equal(m[2], '/route');
  257. m = re.exec('/test');
  258. assert.equal(m.length, 3);
  259. assert.equal(m[0], '/test');
  260. assert.equal(m[1], 'test');
  261. assert.equal(m[2], '');
  262. m = re.exec('/');
  263. assert.equal(m.length, 3);
  264. assert.equal(m[0], '/');
  265. assert.equal(m[1], undefined);
  266. assert.equal(m[2], undefined);
  267. });
  268. it('should do case insensitive matches', function () {
  269. var m = pathToRegExp('/test').exec('/TEST');
  270. assert.equal(m[0], '/TEST');
  271. });
  272. it('should do case sensitive matches', function () {
  273. var re = pathToRegExp('/test', null, { sensitive: true });
  274. var m;
  275. m = re.exec('/test');
  276. assert.equal(m.length, 1);
  277. assert.equal(m[0], '/test');
  278. m = re.exec('/TEST');
  279. assert.ok(!m);
  280. });
  281. it('should do non-ending matches', function () {
  282. var params = [];
  283. var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
  284. assert.equal(params.length, 1);
  285. assert.equal(params[0].name, 'test');
  286. assert.equal(params[0].optional, false);
  287. assert.equal(m.length, 2);
  288. assert.equal(m[0], '/test');
  289. assert.equal(m[1], 'test');
  290. });
  291. it('should match trailing slashes in non-ending non-strict mode', function () {
  292. var params = [];
  293. var re = pathToRegExp('/:test', params, { end: false });
  294. var m;
  295. assert.equal(params.length, 1);
  296. assert.equal(params[0].name, 'test');
  297. assert.equal(params[0].optional, false);
  298. m = re.exec('/test/');
  299. assert.equal(m.length, 2);
  300. assert.equal(m[0], '/test/');
  301. assert.equal(m[1], 'test');
  302. });
  303. it('should match trailing slashes in non-ending non-strict mode', function () {
  304. var params = [];
  305. var re = pathToRegExp('/route/', params, { end: false });
  306. var m;
  307. assert.equal(params.length, 0);
  308. m = re.exec('/route/');
  309. assert.equal(m.length, 1);
  310. assert.equal(m[0], '/route/');
  311. m = re.exec('/route/test');
  312. assert.equal(m.length, 1);
  313. assert.equal(m[0], '/route');
  314. m = re.exec('/route');
  315. assert.equal(m.length, 1);
  316. assert.equal(m[0], '/route');
  317. m = re.exec('/route//');
  318. assert.equal(m.length, 1);
  319. assert.equal(m[0], '/route/');
  320. });
  321. it('should match trailing slashing in non-ending strict mode', function () {
  322. var params = [];
  323. var re = pathToRegExp('/route/', params, { end: false, strict: true });
  324. assert.equal(params.length, 0);
  325. m = re.exec('/route/');
  326. assert.equal(m.length, 1);
  327. assert.equal(m[0], '/route/');
  328. m = re.exec('/route/test');
  329. assert.equal(m.length, 1);
  330. assert.equal(m[0], '/route/');
  331. m = re.exec('/route');
  332. assert.ok(!m);
  333. m = re.exec('/route//');
  334. assert.equal(m.length, 1);
  335. assert.equal(m[0], '/route/');
  336. });
  337. it('should not match trailing slashes in non-ending strict mode', function () {
  338. var params = [];
  339. var re = pathToRegExp('/route', params, { end: false, strict: true });
  340. assert.equal(params.length, 0);
  341. m = re.exec('/route');
  342. assert.equal(m.length, 1);
  343. assert.equal(m[0], '/route');
  344. m = re.exec('/route/');
  345. assert.ok(m.length, 1);
  346. assert.equal(m[0], '/route');
  347. });
  348. it('should match text after an express param', function () {
  349. var params = [];
  350. var re = pathToRegExp('/(:test)route', params);
  351. assert.equal(params.length, 1);
  352. assert.equal(params[0].name, 'test');
  353. assert.equal(params[0].optional, false);
  354. m = re.exec('/route');
  355. assert.ok(!m);
  356. m = re.exec('/testroute');
  357. assert.equal(m.length, 2);
  358. assert.equal(m[0], '/testroute');
  359. assert.equal(m[1], 'test');
  360. m = re.exec('testroute');
  361. assert.ok(!m);
  362. });
  363. it('should match text after an optional express param', function () {
  364. var params = [];
  365. var re = pathToRegExp('/(:test?)route', params);
  366. var m;
  367. assert.equal(params.length, 1);
  368. assert.equal(params[0].name, 'test');
  369. assert.equal(params[0].optional, true);
  370. m = re.exec('/route');
  371. assert.equal(m.length, 2);
  372. assert.equal(m[0], '/route');
  373. assert.equal(m[1], undefined);
  374. m = re.exec('/testroute');
  375. assert.equal(m.length, 2);
  376. assert.equal(m[0], '/testroute');
  377. assert.equal(m[1], 'test');
  378. m = re.exec('route');
  379. assert.ok(!m);
  380. });
  381. it('should match optional formats', function () {
  382. var params = [];
  383. var re = pathToRegExp('/:test.:format?', params);
  384. var m;
  385. assert.equal(params.length, 2);
  386. assert.equal(params[0].name, 'test');
  387. assert.equal(params[0].optional, false);
  388. assert.equal(params[1].name, 'format');
  389. assert.equal(params[1].optional, true);
  390. m = re.exec('/route');
  391. assert.equal(m.length, 3);
  392. assert.equal(m[0], '/route');
  393. assert.equal(m[1], 'route');
  394. assert.equal(m[2], undefined);
  395. m = re.exec('/route.json');
  396. assert.equal(m.length, 3);
  397. assert.equal(m[0], '/route.json');
  398. assert.equal(m[1], 'route');
  399. assert.equal(m[2], 'json');
  400. });
  401. it('should match full paths with format by default', function () {
  402. var params = [];
  403. var m = pathToRegExp('/:test', params).exec('/test.json');
  404. assert.equal(params.length, 1);
  405. assert.equal(params[0].name, 'test');
  406. assert.equal(params[0].optional, false);
  407. assert.equal(m.length, 2);
  408. assert.equal(m[0], '/test.json');
  409. assert.equal(m[1], 'test.json');
  410. });
  411. });
  412. describe('regexps', function () {
  413. it('should return the regexp', function () {
  414. assert.deepEqual(pathToRegExp(/.*/), /.*/);
  415. });
  416. });
  417. describe('arrays', function () {
  418. it('should join arrays parts', function () {
  419. var re = pathToRegExp(['/test', '/route']);
  420. assert.ok(re.test('/test'));
  421. assert.ok(re.test('/route'));
  422. assert.ok(!re.test('/else'));
  423. });
  424. it('should match parts properly', function () {
  425. var params = [];
  426. var re = pathToRegExp(['/:test', '/test/:route'], params);
  427. var m;
  428. assert.equal(params.length, 2);
  429. assert.equal(params[0].name, 'test');
  430. assert.equal(params[0].optional, false);
  431. assert.equal(params[1].name, 'route');
  432. assert.equal(params[1].optional, false);
  433. m = re.exec('/route');
  434. assert.equal(m.length, 3);
  435. assert.equal(m[0], '/route');
  436. assert.equal(m[1], 'route');
  437. assert.equal(m[2], undefined);
  438. m = re.exec('/test/path');
  439. assert.equal(m.length, 3);
  440. assert.equal(m[0], '/test/path');
  441. assert.equal(m[1], undefined);
  442. assert.equal(m[2], 'path');
  443. });
  444. });
  445. });