3 var after
= require('after');
4 var assert
= require('assert')
5 var express
= require('../')
6 , Route
= express
.Route
7 , methods
= require('methods')
9 describe('Route', function(){
10 it('should work without handlers', function(done
) {
11 var req
= { method
: 'GET', url
: '/' }
12 var route
= new Route('/foo')
13 route
.dispatch(req
, {}, done
)
16 it('should not stack overflow with a large sync stack', function (done
) {
17 this.timeout(5000) // long-running test
19 var req
= { method
: 'GET', url
: '/' }
20 var route
= new Route('/foo')
22 route
.get(function (req
, res
, next
) {
27 for (var i
= 0; i
< 6000; i
++) {
28 route
.all(function (req
, res
, next
) {
34 route
.get(function (req
, res
, next
) {
39 route
.dispatch(req
, {}, function (err
) {
40 if (err
) return done(err
)
42 assert
.strictEqual(req
.counter
, 6000)
47 describe('.all', function(){
48 it('should add handler', function(done
){
49 var req
= { method
: 'GET', url
: '/' };
50 var route
= new Route('/foo');
52 route
.all(function(req
, res
, next
) {
57 route
.dispatch(req
, {}, function (err
) {
58 if (err
) return done(err
);
64 it('should handle VERBS', function(done
) {
66 var route
= new Route('/foo');
67 var cb
= after(methods
.length
, function (err
) {
68 if (err
) return done(err
);
69 assert
.strictEqual(count
, methods
.length
)
73 route
.all(function(req
, res
, next
) {
78 methods
.forEach(function testMethod(method
) {
79 var req
= { method
: method
, url
: '/' };
80 route
.dispatch(req
, {}, cb
);
84 it('should stack', function(done
) {
85 var req
= { count
: 0, method
: 'GET', url
: '/' };
86 var route
= new Route('/foo');
88 route
.all(function(req
, res
, next
) {
93 route
.all(function(req
, res
, next
) {
98 route
.dispatch(req
, {}, function (err
) {
99 if (err
) return done(err
);
100 assert
.strictEqual(req
.count
, 2)
106 describe('.VERB', function(){
107 it('should support .get', function(done
){
108 var req
= { method
: 'GET', url
: '/' };
109 var route
= new Route('');
111 route
.get(function(req
, res
, next
) {
116 route
.dispatch(req
, {}, function (err
) {
117 if (err
) return done(err
);
118 assert
.ok(req
.called
)
123 it('should limit to just .VERB', function(done
){
124 var req
= { method
: 'POST', url
: '/' };
125 var route
= new Route('');
127 route
.get(function () {
128 throw new Error('not me!');
131 route
.post(function(req
, res
, next
) {
136 route
.dispatch(req
, {}, function (err
) {
137 if (err
) return done(err
);
138 assert
.ok(req
.called
)
143 it('should allow fallthrough', function(done
){
144 var req
= { order
: '', method
: 'GET', url
: '/' };
145 var route
= new Route('');
147 route
.get(function(req
, res
, next
) {
152 route
.all(function(req
, res
, next
) {
157 route
.get(function(req
, res
, next
) {
162 route
.dispatch(req
, {}, function (err
) {
163 if (err
) return done(err
);
164 assert
.strictEqual(req
.order
, 'abc')
170 describe('errors', function(){
171 it('should handle errors via arity 4 functions', function(done
){
172 var req
= { order
: '', method
: 'GET', url
: '/' };
173 var route
= new Route('');
175 route
.all(function(req
, res
, next
){
176 next(new Error('foobar'));
179 route
.all(function(req
, res
, next
){
184 route
.all(function(err
, req
, res
, next
){
189 route
.dispatch(req
, {}, function (err
) {
191 assert
.strictEqual(err
.message
, 'foobar')
192 assert
.strictEqual(req
.order
, 'a')
197 it('should handle throw', function(done
) {
198 var req
= { order
: '', method
: 'GET', url
: '/' };
199 var route
= new Route('');
201 route
.all(function () {
202 throw new Error('foobar');
205 route
.all(function(req
, res
, next
){
210 route
.all(function(err
, req
, res
, next
){
215 route
.dispatch(req
, {}, function (err
) {
217 assert
.strictEqual(err
.message
, 'foobar')
218 assert
.strictEqual(req
.order
, 'a')
223 it('should handle throwing inside error handlers', function(done
) {
224 var req
= { method
: 'GET', url
: '/' };
225 var route
= new Route('');
227 route
.get(function () {
228 throw new Error('boom!');
231 route
.get(function(err
, req
, res
, next
){
232 throw new Error('oops');
235 route
.get(function(err
, req
, res
, next
){
236 req
.message
= err
.message
;
240 route
.dispatch(req
, {}, function (err
) {
241 if (err
) return done(err
);
242 assert
.strictEqual(req
.message
, 'oops')
247 it('should handle throw in .all', function(done
) {
248 var req
= { method
: 'GET', url
: '/' };
249 var route
= new Route('');
251 route
.all(function(req
, res
, next
){
252 throw new Error('boom!');
255 route
.dispatch(req
, {}, function(err
){
257 assert
.strictEqual(err
.message
, 'boom!')
262 it('should handle single error handler', function(done
) {
263 var req
= { method
: 'GET', url
: '/' };
264 var route
= new Route('');
266 route
.all(function(err
, req
, res
, next
){
267 // this should not execute
268 throw new Error('should not be called')
271 route
.dispatch(req
, {}, done
);