1
+ import { Picker } from 'meteor/storyteller:picker' ;
2
+ // TODO replace HTTP with fetch
3
+ import { HTTP } from 'meteor/http' ;
4
+ import { Random } from 'meteor/random' ;
5
+ import { Meteor } from 'meteor/meteor' ;
6
+
7
+ function getPath ( path ) {
8
+ return Meteor . absoluteUrl ( path ) ;
9
+ }
10
+
1
11
Tinytest . add ( 'normal route' , function ( test ) {
2
- var path = "/" + Random . id ( ) ;
3
- Picker . route ( path , function ( params , req , res ) {
12
+ const id = Random . id ( ) ;
13
+ Picker . route ( `/ ${ id } ` , function ( params , req , res ) {
4
14
res . end ( "done" ) ;
5
15
} ) ;
6
16
7
- var res = HTTP . get ( getPath ( path ) ) ;
17
+ const res = HTTP . get ( getPath ( id ) ) ;
8
18
test . equal ( res . content , 'done' ) ;
9
19
} ) ;
10
20
11
21
Tinytest . add ( 'with params' , function ( test ) {
12
- var id = Random . id ( ) ;
13
- var path = "/post/:id" ;
22
+ const id = Random . id ( ) ;
23
+ const path = "/post/:id" ;
14
24
Picker . route ( path , function ( params , req , res ) {
15
25
res . end ( params . id ) ;
16
26
} ) ;
17
27
18
- var res = HTTP . get ( getPath ( "/ post/" + id ) ) ;
28
+ const res = HTTP . get ( getPath ( ` post/${ id } ` ) ) ;
19
29
test . equal ( res . content , id ) ;
20
30
} ) ;
21
31
22
32
Tinytest . add ( 'filter only POST' , function ( test ) {
23
- var path = "/" + Random . id ( ) ;
24
- var postRoutes = Picker . filter ( function ( req , res ) {
25
- return req . method == "POST" ;
33
+ const id = Random . id ( ) ;
34
+ const postRoutes = Picker . filter ( function ( req , res ) {
35
+ return req . method === "POST" ;
26
36
} ) ;
27
37
28
- postRoutes . route ( path , function ( params , req , res ) {
38
+ postRoutes . route ( `/ ${ id } ` , function ( params , req , res ) {
29
39
res . end ( "done" ) ;
30
40
} ) ;
31
41
32
- var res = HTTP . get ( getPath ( path ) ) ;
33
- test . isFalse ( res . content == "done" ) ;
42
+ const res1 = HTTP . get ( getPath ( `/ ${ id } ` ) ) ;
43
+ test . isFalse ( res1 . content = == "done" ) ;
34
44
35
- var res = HTTP . post ( getPath ( path ) ) ;
36
- test . isTrue ( res . content == "done" ) ;
45
+ const res2 = HTTP . post ( getPath ( `/ ${ id } ` ) ) ;
46
+ test . isTrue ( res2 . content = == "done" ) ;
37
47
} ) ;
38
48
39
49
Tinytest . add ( 'query strings' , function ( test ) {
40
- var path = "/" + Random . id ( ) ;
41
- Picker . route ( path , function ( params , req , res ) {
50
+ const id = Random . id ( ) ;
51
+ Picker . route ( `/ ${ id } ` , function ( params , req , res ) {
42
52
res . end ( "" + params . query . aa ) ;
43
53
} ) ;
44
54
45
- var res = HTTP . get ( getPath ( path + " ?aa=10" ) ) ;
55
+ const res = HTTP . get ( getPath ( `/ ${ id } ?aa=10&bb=10` ) ) ;
46
56
test . equal ( res . content , "10" ) ;
47
57
} ) ;
48
58
49
59
Tinytest . add ( 'middlewares' , function ( test ) {
50
- var path = "/" + Random . id ( ) ;
60
+ const id = Random . id ( ) ;
51
61
52
62
Picker . middleware ( function ( req , res , next ) {
53
63
setTimeout ( function ( ) {
@@ -56,19 +66,19 @@ Tinytest.add('middlewares', function(test) {
56
66
} , 100 ) ;
57
67
} ) ;
58
68
59
- Picker . route ( path , function ( params , req , res ) {
69
+ Picker . route ( `/ ${ id } ` , function ( params , req , res ) {
60
70
res . end ( req . middlewarePass ) ;
61
71
} ) ;
62
72
63
- var res = HTTP . get ( getPath ( path + " ?aa=10" ) ) ;
73
+ const res = HTTP . get ( getPath ( `/ ${ id } ?aa=10` ) ) ;
64
74
test . equal ( res . content , "ok" ) ;
65
75
} ) ;
66
76
67
77
Tinytest . add ( 'middlewares - with filtered routes' , function ( test ) {
68
- var path = "/" + Random . id ( ) + " /coola" ;
78
+ const path = ` ${ Random . id ( ) } /coola` ;
69
79
70
- var routes = Picker . filter ( function ( req , res ) {
71
- var matched = / c o o l a / . test ( req . url ) ;
80
+ const routes = Picker . filter ( function ( req , res ) {
81
+ const matched = / c o o l a / . test ( req . url ) ;
72
82
return matched ;
73
83
} ) ;
74
84
@@ -79,15 +89,45 @@ Tinytest.add('middlewares - with filtered routes', function(test) {
79
89
} , 100 ) ;
80
90
} ) ;
81
91
82
- routes . route ( path , function ( params , req , res ) {
92
+ routes . route ( `/ ${ path } ` , function ( params , req , res ) {
83
93
res . end ( req . middlewarePass ) ;
84
94
} ) ;
85
95
86
- var res = HTTP . get ( getPath ( path ) ) ;
96
+ const res = HTTP . get ( getPath ( path ) ) ;
87
97
test . equal ( res . content , "ok" ) ;
88
98
} ) ;
89
99
90
- var urlResolve = Npm . require ( 'url' ) . resolve ;
91
- function getPath ( path ) {
92
- return urlResolve ( process . env . ROOT_URL , path ) ;
93
- }
100
+ /*
101
+ Tinytest.add('middlewares - with several filtered routes', function(test) {
102
+ const path1 = `${Random.id()}/coola`;
103
+ const path2 = `${Random.id()}/coola`;
104
+
105
+ const routes1 = Picker.filter();
106
+ const routes2 = Picker.filter();
107
+
108
+ const increaseResultBy = (i) => (req, res, next) => {
109
+ setTimeout(function() {
110
+ req.result = req.result || 0;
111
+ req.result += i;
112
+ next();
113
+ }, 100);
114
+ };
115
+
116
+ routes1.middleware(increaseResultBy(1));
117
+ routes2.middleware(increaseResultBy(2));
118
+
119
+ Picker.middleware(increaseResultBy(10));
120
+
121
+ routes1.route(`/${path1}`, function(params, req, res) {
122
+ res.end(req.result+'');
123
+ });
124
+ routes2.route(`/${path2}`, function(params, req, res) {
125
+ res.end(req.result+'');
126
+ });
127
+
128
+ const res1 = HTTP.get(getPath(path1));
129
+ test.equal(res1.content, "11");
130
+
131
+ const res2 = HTTP.get(getPath(path2));
132
+ test.equal(res2.content, "12");
133
+ });*/
0 commit comments