Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
1
/*
2
* mount-test.js: Tests for mounting and normalizing routes into a Router instance.
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var assert = require('assert'),
10
vows = require('vows'),
11
director = require('../../../lib/director');
12
13
function assertRoute (fn, path, route) {
14
if (path.length === 1) {
15
return assert.strictEqual(route[path.shift()], fn);
16
}
17
18
route = route[path.shift()];
19
assert.isObject(route);
20
assertRoute(fn, path, route);
21
}
22
23
vows.describe('director/core/mount').addBatch({
24
"An instance of director.Router": {
25
"with no preconfigured params": {
26
topic: new director.Router(),
27
"the mount() method": {
28
"should sanitize the routes correctly": function (router) {
29
function foobar () { }
30
function foostar () { }
31
function foobazzbuzz () { }
32
function foodog () { }
33
function root () {}
34
var fnArray = [foobar, foostar];
35
36
router.mount({
37
'/': {
38
before: root,
39
on: root,
40
after: root,
41
'/nesting': {
42
on: foobar,
43
'/deep': foostar
44
}
45
},
46
'/foo': {
47
'/bar': foobar,
48
'/*': foostar,
49
'/jitsu/then': {
50
before: foobar
51
}
52
},
53
'/foo/bazz': {
54
'/buzz': foobazzbuzz
55
},
56
'/foo/jitsu': {
57
'/then': fnArray
58
},
59
'/foo/jitsu/then/now': foostar,
60
'/foo/:dog': foodog
61
});
62
63
assertRoute(root, ['on'], router.routes);
64
assertRoute(root, ['after'], router.routes);
65
assertRoute(root, ['before'], router.routes);
66
assertRoute(foobar, ['nesting', 'on'], router.routes);
67
assertRoute(foostar, ['nesting', 'deep', 'on'], router.routes);
68
assertRoute(foobar, [ 'foo', 'bar', 'on'], router.routes);
69
assertRoute(foostar, ['foo', '([_.()!\\ %@&a-zA-Z0-9-]+)', 'on'], router.routes);
70
assertRoute(fnArray, ['foo', 'jitsu', 'then', 'on'], router.routes);
71
assertRoute(foobar, ['foo', 'jitsu', 'then', 'before'], router.routes);
72
assertRoute(foobazzbuzz, ['foo', 'bazz', 'buzz', 'on'], router.routes);
73
assertRoute(foostar, ['foo', 'jitsu', 'then', 'now', 'on'], router.routes);
74
assertRoute(foodog, ['foo', '([._a-zA-Z0-9-]+)', 'on'], router.routes);
75
},
76
77
"should accept string path": function(router) {
78
function dogs () { }
79
80
router.mount({
81
'/dogs': {
82
on: dogs
83
}
84
},
85
'/api');
86
87
assertRoute(dogs, ['api', 'dogs', 'on'], router.routes);
88
}
89
}
90
},
91
"with preconfigured params": {
92
topic: function () {
93
var router = new director.Router();
94
router.param('city', '([\\w\\-]+)');
95
router.param(':country', /([A-Z][A-Za-z]+)/);
96
router.param(':zip', /([\d]{5})/);
97
return router;
98
},
99
"should sanitize the routes correctly": function (router) {
100
function usaCityZip () { }
101
function countryCityZip () { }
102
103
router.mount({
104
'/usa/:city/:zip': usaCityZip,
105
'/world': {
106
'/:country': {
107
'/:city/:zip': countryCityZip
108
}
109
}
110
});
111
112
assertRoute(usaCityZip, ['usa', '([\\w\\-]+)', '([\\d]{5})', 'on'], router.routes);
113
assertRoute(countryCityZip, ['world', '([A-Z][A-Za-z]+)', '([\\w\\-]+)', '([\\d]{5})', 'on'], router.routes);
114
}
115
}
116
}
117
}).export(module);
118
119