1
1
2
+ var after = require ( 'after' ) ;
2
3
var should = require ( 'should' ) ;
3
4
var express = require ( '../' )
4
5
, Route = express . Route
@@ -9,167 +10,182 @@ describe('Route', function(){
9
10
10
11
describe ( '.all' , function ( ) {
11
12
it ( 'should add handler' , function ( done ) {
13
+ var req = { method : 'GET' , url : '/' } ;
12
14
var route = new Route ( '/foo' ) ;
13
15
14
16
route . all ( function ( req , res , next ) {
15
- assert . equal ( req . a , 1 ) ;
16
- assert . equal ( res . b , 2 ) ;
17
+ req . called = true ;
17
18
next ( ) ;
18
19
} ) ;
19
20
20
- route . dispatch ( { a :1 , method : 'GET' } , { b :2 } , done ) ;
21
+ route . dispatch ( req , { } , function ( err ) {
22
+ if ( err ) return done ( err ) ;
23
+ should ( req . called ) . be . ok ;
24
+ done ( ) ;
25
+ } ) ;
21
26
} )
22
27
23
28
it ( 'should handle VERBS' , function ( done ) {
29
+ var count = 0 ;
24
30
var route = new Route ( '/foo' ) ;
31
+ var cb = after ( methods . length , function ( err ) {
32
+ if ( err ) return done ( err ) ;
33
+ count . should . equal ( methods . length ) ;
34
+ done ( ) ;
35
+ } ) ;
25
36
26
- var count = 0 ;
27
37
route . all ( function ( req , res , next ) {
28
38
count ++ ;
39
+ next ( ) ;
29
40
} ) ;
30
41
31
42
methods . forEach ( function testMethod ( method ) {
32
- route . dispatch ( { method : method } , { } ) ;
43
+ var req = { method : method , url : '/' } ;
44
+ route . dispatch ( req , { } , cb ) ;
33
45
} ) ;
34
-
35
- assert . equal ( count , methods . length ) ;
36
- done ( ) ;
37
46
} )
38
47
39
48
it ( 'should stack' , function ( done ) {
49
+ var req = { count : 0 , method : 'GET' , url : '/' } ;
40
50
var route = new Route ( '/foo' ) ;
41
51
42
- var count = 0 ;
43
52
route . all ( function ( req , res , next ) {
44
- count ++ ;
53
+ req . count ++ ;
45
54
next ( ) ;
46
55
} ) ;
47
56
48
57
route . all ( function ( req , res , next ) {
49
- count ++ ;
58
+ req . count ++ ;
50
59
next ( ) ;
51
60
} ) ;
52
61
53
- route . dispatch ( { method : 'GET' } , { } , function ( err ) {
54
- assert . ifError ( err ) ;
55
- count ++ ;
62
+ route . dispatch ( req , { } , function ( err ) {
63
+ if ( err ) return done ( err ) ;
64
+ req . count . should . equal ( 2 ) ;
65
+ done ( ) ;
56
66
} ) ;
57
-
58
- assert . equal ( count , 3 ) ;
59
- done ( ) ;
60
67
} )
61
68
} )
62
69
63
70
describe ( '.VERB' , function ( ) {
64
71
it ( 'should support .get' , function ( done ) {
72
+ var req = { method : 'GET' , url : '/' } ;
65
73
var route = new Route ( '' ) ;
66
74
67
- var count = 0 ;
68
75
route . get ( function ( req , res , next ) {
69
- count ++ ;
76
+ req . called = true ;
77
+ next ( ) ;
70
78
} )
71
79
72
- route . dispatch ( { method : 'GET' } , { } ) ;
73
- assert ( count ) ;
74
- done ( ) ;
80
+ route . dispatch ( req , { } , function ( err ) {
81
+ if ( err ) return done ( err ) ;
82
+ should ( req . called ) . be . ok ;
83
+ done ( ) ;
84
+ } ) ;
75
85
} )
76
86
77
87
it ( 'should limit to just .VERB' , function ( done ) {
88
+ var req = { method : 'POST' , url : '/' } ;
78
89
var route = new Route ( '' ) ;
79
90
80
91
route . get ( function ( req , res , next ) {
81
- assert ( false ) ;
82
- done ( ) ;
92
+ throw new Error ( 'not me!' ) ;
83
93
} )
84
94
85
95
route . post ( function ( req , res , next ) {
86
- assert ( true ) ;
96
+ req . called = true ;
97
+ next ( ) ;
87
98
} )
88
99
89
- route . dispatch ( { method : 'post' } , { } ) ;
90
- done ( ) ;
100
+ route . dispatch ( req , { } , function ( err ) {
101
+ if ( err ) return done ( err ) ;
102
+ should ( req . called ) . be . true ;
103
+ done ( ) ;
104
+ } ) ;
91
105
} )
92
106
93
107
it ( 'should allow fallthrough' , function ( done ) {
108
+ var req = { order : '' , method : 'GET' , url : '/' } ;
94
109
var route = new Route ( '' ) ;
95
110
96
- var order = '' ;
97
111
route . get ( function ( req , res , next ) {
98
- order += 'a' ;
112
+ req . order += 'a' ;
99
113
next ( ) ;
100
114
} )
101
115
102
116
route . all ( function ( req , res , next ) {
103
- order += 'b' ;
117
+ req . order += 'b' ;
104
118
next ( ) ;
105
119
} ) ;
106
120
107
121
route . get ( function ( req , res , next ) {
108
- order += 'c' ;
122
+ req . order += 'c' ;
123
+ next ( ) ;
109
124
} )
110
125
111
- route . dispatch ( { method : 'get' } , { } ) ;
112
- assert . equal ( order , 'abc' ) ;
113
- done ( ) ;
126
+ route . dispatch ( req , { } , function ( err ) {
127
+ if ( err ) return done ( err ) ;
128
+ req . order . should . equal ( 'abc' ) ;
129
+ done ( ) ;
130
+ } ) ;
114
131
} )
115
132
} )
116
133
117
134
describe ( 'errors' , function ( ) {
118
135
it ( 'should handle errors via arity 4 functions' , function ( done ) {
136
+ var req = { order : '' , method : 'GET' , url : '/' } ;
119
137
var route = new Route ( '' ) ;
120
138
121
- var order = '' ;
122
139
route . all ( function ( req , res , next ) {
123
140
next ( new Error ( 'foobar' ) ) ;
124
141
} ) ;
125
142
126
143
route . all ( function ( req , res , next ) {
127
- order += '0' ;
144
+ req . order += '0' ;
128
145
next ( ) ;
129
146
} ) ;
130
147
131
148
route . all ( function ( err , req , res , next ) {
132
- order += 'a' ;
149
+ req . order += 'a' ;
133
150
next ( err ) ;
134
151
} ) ;
135
152
136
- route . all ( function ( err , req , res , next ) {
137
- assert . equal ( err . message , 'foobar' ) ;
138
- assert . equal ( order , 'a' ) ;
153
+ route . dispatch ( req , { } , function ( err ) {
154
+ should ( err ) . be . ok ;
155
+ should ( err . message ) . equal ( 'foobar' ) ;
156
+ req . order . should . equal ( 'a' ) ;
139
157
done ( ) ;
140
158
} ) ;
141
-
142
- route . dispatch ( { method : 'get' } , { } ) ;
143
159
} )
144
160
145
161
it ( 'should handle throw' , function ( done ) {
162
+ var req = { order : '' , method : 'GET' , url : '/' } ;
146
163
var route = new Route ( '' ) ;
147
164
148
- var order = '' ;
149
165
route . all ( function ( req , res , next ) {
150
166
throw new Error ( 'foobar' ) ;
151
167
} ) ;
152
168
153
169
route . all ( function ( req , res , next ) {
154
- order += '0' ;
170
+ req . order += '0' ;
155
171
next ( ) ;
156
172
} ) ;
157
173
158
174
route . all ( function ( err , req , res , next ) {
159
- order += 'a' ;
175
+ req . order += 'a' ;
160
176
next ( err ) ;
161
177
} ) ;
162
178
163
- route . all ( function ( err , req , res , next ) {
164
- assert . equal ( err . message , 'foobar' ) ;
165
- assert . equal ( order , 'a' ) ;
179
+ route . dispatch ( req , { } , function ( err ) {
180
+ should ( err ) . be . ok ;
181
+ should ( err . message ) . equal ( 'foobar' ) ;
182
+ req . order . should . equal ( 'a' ) ;
166
183
done ( ) ;
167
184
} ) ;
168
-
169
- route . dispatch ( { method : 'get' } , { } ) ;
170
185
} ) ;
171
186
172
187
it ( 'should handle throwing inside error handlers' , function ( done ) {
188
+ var req = { method : 'GET' , url : '/' } ;
173
189
var route = new Route ( '' ) ;
174
190
175
191
route . get ( function ( req , res , next ) {
@@ -181,36 +197,42 @@ describe('Route', function(){
181
197
} ) ;
182
198
183
199
route . get ( function ( err , req , res , next ) {
184
- assert . equal ( err . message , 'oops' ) ;
185
- done ( ) ;
200
+ req . message = err . message ;
201
+ next ( ) ;
186
202
} ) ;
187
203
188
- route . dispatch ( { url : '/' , method : 'GET' } , { } ) ;
204
+ route . dispatch ( req , { } , function ( err ) {
205
+ if ( err ) return done ( err ) ;
206
+ should ( req . message ) . equal ( 'oops' ) ;
207
+ done ( ) ;
208
+ } ) ;
189
209
} ) ;
190
210
191
211
it ( 'should handle throw in .all' , function ( done ) {
212
+ var req = { method : 'GET' , url : '/' } ;
192
213
var route = new Route ( '' ) ;
193
214
194
215
route . all ( function ( req , res , next ) {
195
216
throw new Error ( 'boom!' ) ;
196
217
} ) ;
197
218
198
- route . dispatch ( { url : '/' , method : 'GET' } , { } , function ( err ) {
219
+ route . dispatch ( req , { } , function ( err ) {
199
220
should ( err ) . be . ok ;
200
221
err . message . should . equal ( 'boom!' ) ;
201
222
done ( ) ;
202
223
} ) ;
203
224
} ) ;
204
225
205
226
it ( 'should handle single error handler' , function ( done ) {
227
+ var req = { method : 'GET' , url : '/' } ;
206
228
var route = new Route ( '' ) ;
207
229
208
230
route . all ( function ( err , req , res , next ) {
209
231
// this should not execute
210
232
true . should . be . false ;
211
233
} ) ;
212
234
213
- route . dispatch ( { url : '/' , method : 'GET' } , { } , done ) ;
235
+ route . dispatch ( req , { } , done ) ;
214
236
} ) ;
215
237
} )
216
238
} )
0 commit comments