Route.js
This file
Path
test/Route.js
243 lines · 5.7 KiB · JavaScript
Last changed by
tests: add test for dispatching to empty routeDouglas Christopher Wilsonbefore adoption31dd549
History of this fileTake it
The walk stopped after 200 commits: lines older than 2ac2509 point at it.
4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 1–9why, one step back12var after = require('after');3var should = require('should');4var express = require('../')5 , Route = express.Route6 , methods = require('methods')7 , assert = require('assert');89describe('Route', function(){tests: add test for dispatching to empty routeDouglas Christopher Wilsonbefore adoption31dd549
lines 10–14why, one step back10 it('should work without handlers', function(done) {11 var req = { method: 'GET', url: '/' }12 var route = new Route('/foo')13 route.dispatch(req, {}, done)14 })4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 15–243why, one step back1516 describe('.all', function(){17 it('should add handler', function(done){18 var req = { method: 'GET', url: '/' };19 var route = new Route('/foo');2021 route.all(function(req, res, next) {22 req.called = true;23 next();24 });2526 route.dispatch(req, {}, function (err) {27 if (err) return done(err);28 should(req.called).be.ok;29 done();30 });31 })3233 it('should handle VERBS', function(done) {34 var count = 0;35 var route = new Route('/foo');36 var cb = after(methods.length, function (err) {37 if (err) return done(err);38 count.should.equal(methods.length);39 done();40 });4142 route.all(function(req, res, next) {43 count++;44 next();45 });4647 methods.forEach(function testMethod(method) {48 var req = { method: method, url: '/' };49 route.dispatch(req, {}, cb);50 });51 })5253 it('should stack', function(done) {54 var req = { count: 0, method: 'GET', url: '/' };55 var route = new Route('/foo');5657 route.all(function(req, res, next) {58 req.count++;59 next();60 });6162 route.all(function(req, res, next) {63 req.count++;64 next();65 });6667 route.dispatch(req, {}, function (err) {68 if (err) return done(err);69 req.count.should.equal(2);70 done();71 });72 })73 })7475 describe('.VERB', function(){76 it('should support .get', function(done){77 var req = { method: 'GET', url: '/' };78 var route = new Route('');7980 route.get(function(req, res, next) {81 req.called = true;82 next();83 })8485 route.dispatch(req, {}, function (err) {86 if (err) return done(err);87 should(req.called).be.ok;88 done();89 });90 })9192 it('should limit to just .VERB', function(done){93 var req = { method: 'POST', url: '/' };94 var route = new Route('');9596 route.get(function(req, res, next) {97 throw new Error('not me!');98 })99100 route.post(function(req, res, next) {101 req.called = true;102 next();103 })104105 route.dispatch(req, {}, function (err) {106 if (err) return done(err);107 should(req.called).be.true;108 done();109 });110 })111112 it('should allow fallthrough', function(done){113 var req = { order: '', method: 'GET', url: '/' };114 var route = new Route('');115116 route.get(function(req, res, next) {117 req.order += 'a';118 next();119 })120121 route.all(function(req, res, next) {122 req.order += 'b';123 next();124 });125126 route.get(function(req, res, next) {127 req.order += 'c';128 next();129 })130131 route.dispatch(req, {}, function (err) {132 if (err) return done(err);133 req.order.should.equal('abc');134 done();135 });136 })137 })138139 describe('errors', function(){140 it('should handle errors via arity 4 functions', function(done){141 var req = { order: '', method: 'GET', url: '/' };142 var route = new Route('');143144 route.all(function(req, res, next){145 next(new Error('foobar'));146 });147148 route.all(function(req, res, next){149 req.order += '0';150 next();151 });152153 route.all(function(err, req, res, next){154 req.order += 'a';155 next(err);156 });157158 route.dispatch(req, {}, function (err) {159 should(err).be.ok;160 should(err.message).equal('foobar');161 req.order.should.equal('a');162 done();163 });164 })165166 it('should handle throw', function(done) {167 var req = { order: '', method: 'GET', url: '/' };168 var route = new Route('');169170 route.all(function(req, res, next){171 throw new Error('foobar');172 });173174 route.all(function(req, res, next){175 req.order += '0';176 next();177 });178179 route.all(function(err, req, res, next){180 req.order += 'a';181 next(err);182 });183184 route.dispatch(req, {}, function (err) {185 should(err).be.ok;186 should(err.message).equal('foobar');187 req.order.should.equal('a');188 done();189 });190 });191192 it('should handle throwing inside error handlers', function(done) {193 var req = { method: 'GET', url: '/' };194 var route = new Route('');195196 route.get(function(req, res, next){197 throw new Error('boom!');198 });199200 route.get(function(err, req, res, next){201 throw new Error('oops');202 });203204 route.get(function(err, req, res, next){205 req.message = err.message;206 next();207 });208209 route.dispatch(req, {}, function (err) {210 if (err) return done(err);211 should(req.message).equal('oops');212 done();213 });214 });215216 it('should handle throw in .all', function(done) {217 var req = { method: 'GET', url: '/' };218 var route = new Route('');219220 route.all(function(req, res, next){221 throw new Error('boom!');222 });223224 route.dispatch(req, {}, function(err){225 should(err).be.ok;226 err.message.should.equal('boom!');227 done();228 });229 });230231 it('should handle single error handler', function(done) {232 var req = { method: 'GET', url: '/' };233 var route = new Route('');234235 route.all(function(err, req, res, next){236 // this should not execute237 true.should.be.false;238 });239240 route.dispatch(req, {}, done);241 });242 })243})