fix(deps)!: mime-types@^3.0.0 (#5882)
[express.git] / test / Route.js
blob2a37b9a483913b1aad46eb6144d190ce54a944f6
1 'use strict'
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) {
23 req.counter = 0
24 next()
27 for (var i = 0; i < 6000; i++) {
28 route.all(function (req, res, next) {
29 req.counter++
30 next()
34 route.get(function (req, res, next) {
35 req.called = true
36 next()
39 route.dispatch(req, {}, function (err) {
40 if (err) return done(err)
41 assert.ok(req.called)
42 assert.strictEqual(req.counter, 6000)
43 done()
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) {
53 req.called = true;
54 next();
55 });
57 route.dispatch(req, {}, function (err) {
58 if (err) return done(err);
59 assert.ok(req.called)
60 done();
61 });
64 it('should handle VERBS', function(done) {
65 var count = 0;
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)
70 done();
71 });
73 route.all(function(req, res, next) {
74 count++;
75 next();
76 });
78 methods.forEach(function testMethod(method) {
79 var req = { method: method, url: '/' };
80 route.dispatch(req, {}, cb);
81 });
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) {
89 req.count++;
90 next();
91 });
93 route.all(function(req, res, next) {
94 req.count++;
95 next();
96 });
98 route.dispatch(req, {}, function (err) {
99 if (err) return done(err);
100 assert.strictEqual(req.count, 2)
101 done();
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) {
112 req.called = true;
113 next();
116 route.dispatch(req, {}, function (err) {
117 if (err) return done(err);
118 assert.ok(req.called)
119 done();
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) {
132 req.called = true;
133 next();
136 route.dispatch(req, {}, function (err) {
137 if (err) return done(err);
138 assert.ok(req.called)
139 done();
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) {
148 req.order += 'a';
149 next();
152 route.all(function(req, res, next) {
153 req.order += 'b';
154 next();
157 route.get(function(req, res, next) {
158 req.order += 'c';
159 next();
162 route.dispatch(req, {}, function (err) {
163 if (err) return done(err);
164 assert.strictEqual(req.order, 'abc')
165 done();
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){
180 req.order += '0';
181 next();
184 route.all(function(err, req, res, next){
185 req.order += 'a';
186 next(err);
189 route.dispatch(req, {}, function (err) {
190 assert.ok(err)
191 assert.strictEqual(err.message, 'foobar')
192 assert.strictEqual(req.order, 'a')
193 done();
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){
206 req.order += '0';
207 next();
210 route.all(function(err, req, res, next){
211 req.order += 'a';
212 next(err);
215 route.dispatch(req, {}, function (err) {
216 assert.ok(err)
217 assert.strictEqual(err.message, 'foobar')
218 assert.strictEqual(req.order, 'a')
219 done();
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;
237 next();
240 route.dispatch(req, {}, function (err) {
241 if (err) return done(err);
242 assert.strictEqual(req.message, 'oops')
243 done();
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){
256 assert.ok(err)
257 assert.strictEqual(err.message, 'boom!')
258 done();
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);