Route.js
This file
Path
test/Route.js
238 lines · 5.5 KiB · JavaScript
Last changed by
tests: change Route tests to use callbackDouglas Christopher Wilsonbefore adoptiond0b6b3d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
var after = require('after');var should = require('should');var express = require('../') , Route = express.Route , methods = require('methods') , assert = require('assert');
describe('Route', function(){
describe('.all', function(){ it('should add handler', function(done){ var req = { method: 'GET', url: '/' }; var route = new Route('/foo');
route.all(function(req, res, next) { req.called = true; next(); });
route.dispatch(req, {}, function (err) { if (err) return done(err); should(req.called).be.ok; done(); }); })
it('should handle VERBS', function(done) { var count = 0; var route = new Route('/foo'); var cb = after(methods.length, function (err) { if (err) return done(err); count.should.equal(methods.length); done(); });
route.all(function(req, res, next) { count++; next(); });
methods.forEach(function testMethod(method) { var req = { method: method, url: '/' }; route.dispatch(req, {}, cb); }); })
it('should stack', function(done) { var req = { count: 0, method: 'GET', url: '/' }; var route = new Route('/foo');
route.all(function(req, res, next) { req.count++; next(); });
route.all(function(req, res, next) { req.count++; next(); });
route.dispatch(req, {}, function (err) { if (err) return done(err); req.count.should.equal(2); done(); }); }) })
describe('.VERB', function(){ it('should support .get', function(done){ var req = { method: 'GET', url: '/' }; var route = new Route('');
route.get(function(req, res, next) { req.called = true; next(); })
route.dispatch(req, {}, function (err) { if (err) return done(err); should(req.called).be.ok; done(); }); })
it('should limit to just .VERB', function(done){ var req = { method: 'POST', url: '/' }; var route = new Route('');
route.get(function(req, res, next) { throw new Error('not me!'); })
route.post(function(req, res, next) { req.called = true; next(); })
route.dispatch(req, {}, function (err) { if (err) return done(err); should(req.called).be.true; done(); }); })
it('should allow fallthrough', function(done){ var req = { order: '', method: 'GET', url: '/' }; var route = new Route('');
route.get(function(req, res, next) { req.order += 'a'; next(); })
route.all(function(req, res, next) { req.order += 'b'; next(); });
route.get(function(req, res, next) { req.order += 'c'; next(); })
route.dispatch(req, {}, function (err) { if (err) return done(err); req.order.should.equal('abc'); done(); }); }) })
describe('errors', function(){ it('should handle errors via arity 4 functions', function(done){ var req = { order: '', method: 'GET', url: '/' }; var route = new Route('');
route.all(function(req, res, next){ next(new Error('foobar')); });
route.all(function(req, res, next){ req.order += '0'; next(); });
route.all(function(err, req, res, next){ req.order += 'a'; next(err); });
route.dispatch(req, {}, function (err) { should(err).be.ok; should(err.message).equal('foobar'); req.order.should.equal('a'); done(); }); })
it('should handle throw', function(done) { var req = { order: '', method: 'GET', url: '/' }; var route = new Route('');
route.all(function(req, res, next){ throw new Error('foobar'); });
route.all(function(req, res, next){ req.order += '0'; next(); });
route.all(function(err, req, res, next){ req.order += 'a'; next(err); });
route.dispatch(req, {}, function (err) { should(err).be.ok; should(err.message).equal('foobar'); req.order.should.equal('a'); done(); }); });
it('should handle throwing inside error handlers', function(done) { var req = { method: 'GET', url: '/' }; var route = new Route('');
route.get(function(req, res, next){ throw new Error('boom!'); });
route.get(function(err, req, res, next){ throw new Error('oops'); });
route.get(function(err, req, res, next){ req.message = err.message; next(); });
route.dispatch(req, {}, function (err) { if (err) return done(err); should(req.message).equal('oops'); done(); }); });
it('should handle throw in .all', function(done) { var req = { method: 'GET', url: '/' }; var route = new Route('');
route.all(function(req, res, next){ throw new Error('boom!'); });
route.dispatch(req, {}, function(err){ should(err).be.ok; err.message.should.equal('boom!'); done(); }); });
it('should handle single error handler', function(done) { var req = { method: 'GET', url: '/' }; var route = new Route('');
route.all(function(err, req, res, next){ // this should not execute true.should.be.false; });
route.dispatch(req, {}, done); }); })})