-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
133 lines (122 loc) · 4.9 KB
/
test.js
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
var should = require("should");
describe('ProtolusRouter', function(){
var ProtolusRouter = require('./protolus-router');
describe('addRoute(testFunction, calculateFunction) , route(url, callback)', function(){
it('Routed URL is correct', function(){
var router = new ProtolusRouter();
router.addRoute(function(url){
return url == '';
}, function(){
return 'index';
});
router.route('', function(routed){
should.equal('index', routed);
});
});
it('Routed URL is correct with group', function(){
var router = new ProtolusRouter();
router.addRoute(function(url){
return url == '';
}, 'test', function(){
return 'index';
});
router.route('', 'test', function(routed){
should.equal('index', routed);
});
});
it('Routed URL is incorrect without group', function(){
var router = new ProtolusRouter({
onMissing : function(){
should.equal(true, true);
}
});
router.addRoute(function(url){
return url == '';
}, 'test', function(){
return 'index';
});
router.route('', 'blah', function(routed){
should.fail('This route should not occur', arguments);
});
});
});
describe('addRoute(testFunction, actionFunction) , route(url)', function(){
it('Routed URL is correct', function(){
var router = new ProtolusRouter();
var a = {};
router.addRoute(function(url){
return url == '';
}, function(){
a.result = 'index';
});
router.route('');
should.equal('index', a.result);
});
});
describe('addRoute(regex, calculateFunction) , route(url, callback)', function(){
it('Routed URL is correct', function(){
var router = new ProtolusRouter();
router.addRoute( /(users)\/([A-Za-z][A-Za-z0-9]{3,16})\/([0-9]+)/i , function(){
return Array.prototype.slice.call(arguments, 0);;
});
router.route('users/khrome/42', function(a, b, c){
should.equal(a, 'users');
should.equal(b, 'khrome');
should.equal(c, '42');
});
});
it('Routed URL is correct with names', function(){
var router = new ProtolusRouter({
argumentNames : ['type', 'handle', 'id']
});
router.addRoute( /(users)\/([A-Za-z][A-Za-z0-9]{3,16})\/([0-9]+)/i , function(args){
return args;
});
router.route('users/khrome/42', function(args){
should.equal(args.type, 'users');
should.equal(args.handle, 'khrome');
should.equal(args.id, '42');
});
});
});
describe('addRoute(regex, actionFunction) , route(url)', function(){
it('Routed URL is correct', function(){
var router = new ProtolusRouter();
var result;
router.addRoute(/(users)\/([A-Za-z][A-Za-z0-9]{3,16})\/([0-9]+)/i, function(type, handle, id){
result = type+'.js?handle='+handle+'&id='+id;
});
router.route('users/khrome/42');
should.equal('users.js?handle=khrome&id=42', result);
});
it('Routed URL is correct with names', function(){
var router = new ProtolusRouter({
argumentNames : ['type', 'handle', 'id']
});
var result;
router.addRoute(/(users)\/([A-Za-z][A-Za-z0-9]{3,16})\/([0-9]+)/i, function(args){
result = args.type+'.js?handle='+args.handle+'&id='+args.id;
});
router.route('users/khrome/42');
should.equal('users.js?handle=khrome&id=42', result);
});
});
describe('INI(fileName) , route(url, callback)', function(){
var router = new ProtolusRouter().INI('test.ini');
it('Routed URL is correct', function(){
router.route('users/khrome', function(routed){
should.equal('profile?username=khrome', routed);
});
});
it('Routed URL is incorrect without group', function(){
router.route('users/khrome/upload', function(routed){
should.fail('should not route url without correct group');
});
});
it('Routed URL is correct with group', function(){
router.route('users/khrome/upload', 'post', function(routed){
should.equal('dropbox?username=khrome', routed);
});
});
});
});