-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathinstance.js
More file actions
127 lines (100 loc) · 3 KB
/
instance.js
File metadata and controls
127 lines (100 loc) · 3 KB
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
Tinytest.add('normal route', function(test) {
var path = "/" + Random.id();
Picker.route(path, function(params, req, res) {
res.end("done");
});
var res = HTTP.get(getPath(path));
test.equal(res.content, 'done');
});
Tinytest.add('with params', function(test) {
var id = Random.id();
var path = "/post/:id";
Picker.route(path, function(params, req, res) {
res.end(params.id);
});
var res = HTTP.get(getPath("/post/" + id));
test.equal(res.content, id);
});
Tinytest.add('filter only POST', function(test) {
var path = "/" + Random.id();
var postRoutes = Picker.filter(function(req, res) {
return req.method == "POST";
});
postRoutes.route(path, function(params, req, res) {
res.end("done");
});
var res = HTTP.get(getPath(path));
test.isFalse(res.content == "done");
var res = HTTP.post(getPath(path));
test.isTrue(res.content == "done");
});
Tinytest.add('query strings', function(test) {
var path = "/" + Random.id();
Picker.route(path, function(params, req, res) {
res.end("" + params.query.aa);
});
var res = HTTP.get(getPath(path + "?aa=10"));
test.equal(res.content, "10");
});
Tinytest.add('middlewares', function(test) {
var path = "/" + Random.id();
Picker.middleware(function(req, res, next) {
setTimeout(function() {
req.middlewarePass = "ok";
next();
}, 100);
});
Picker.route(path, function(params, req, res) {
res.end(req.middlewarePass);
});
var res = HTTP.get(getPath(path + "?aa=10"));
test.equal(res.content, "ok");
});
Tinytest.add('middlewares - with filtered routes', function(test) {
var path = "/" + Random.id() + "/coola";
var routes = Picker.filter(function(req, res) {
var matched = /coola/.test(req.url);
return matched;
});
routes.middleware(function(req, res, next) {
setTimeout(function() {
req.middlewarePass = "ok";
next();
}, 100);
});
routes.route(path, function(params, req, res) {
res.end(req.middlewarePass);
});
var res = HTTP.get(getPath(path));
test.equal(res.content, "ok");
});
Tinytest.add('middlewares - with several filtered routes', function(test) {
var path1 = "/" + Random.id() + "/coola";
var path2 = "/" + Random.id() + "/coola";
var routes1 = Picker.filter();
var routes2 = Picker.filter();
const increaseResultBy = (i) => (req, res, next) => {
setTimeout(function() {
req.result = req.result || 0;
req.result += i;
next();
}, 100);
};
routes1.middleware(increaseResultBy(1));
routes2.middleware(increaseResultBy(2));
Picker.middleware(increaseResultBy(10));
routes1.route(path1, function(params, req, res) {
res.end(req.result+'');
});
routes2.route(path2, function(params, req, res) {
res.end(req.result+'');
});
var res = HTTP.get(getPath(path1));
test.equal(res.content, "11");
var res = HTTP.get(getPath(path2));
test.equal(res.content, "12");
});
var urlResolve = Npm.require('url').resolve;
function getPath(path) {
return urlResolve(process.env.ROOT_URL, path);
}