tests: change Route tests to use callback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent f34944c · Browse files · 1 file·+77 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+77 added−55 removed
- tests:+77 added−55 removed
Provenance
- Commit
d0b6b3d- Tree
03a2bf6- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
f34944c
1 file changed+77 added−55 removed
modified:test/Route.js+77 added-55 removed
1 2+var after = require('after');3 var should = require('should');4 var express = require('../')5 , Route = express.Route6 , methods = require('methods')2 unmodified lines9 describe('Route', function(){10 11 describe('.all', function(){12 it('should add handler', function(done){13+ var req = { method: 'GET', url: '/' };14 var route = new Route('/foo');15 16 route.all(function(req, res, next) {15- assert.equal(req.a, 1);16- assert.equal(res.b, 2);17+ req.called = true;18 next();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+ });26 })27 28 it('should handle VERBS', function(done) {29+ var count = 0;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+ });36 26- var count = 0;37 route.all(function(req, res, next) {38 count++;39+ next();40 });41 42 methods.forEach(function testMethod(method) {32- route.dispatch({ method: method }, {});43+ var req = { method: method, url: '/' };44+ route.dispatch(req, {}, cb);45 });34-35- assert.equal(count, methods.length);36- done();46 })47 48 it('should stack', function(done) {49+ var req = { count: 0, method: 'GET', url: '/' };50 var route = new Route('/foo');51 42- var count = 0;52 route.all(function(req, res, next) {44- count++;53+ req.count++;54 next();55 });56 57 route.all(function(req, res, next) {49- count++;58+ req.count++;59 next();60 });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();66 });57-58- assert.equal(count, 3);59- done();67 })68 })69 70 describe('.VERB', function(){71 it('should support .get', function(done){72+ var req = { method: 'GET', url: '/' };73 var route = new Route('');74 67- var count = 0;75 route.get(function(req, res, next) {69- count++;76+ req.called = true;77+ next();78 })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+ });85 })86 87 it('should limit to just .VERB', function(done){88+ var req = { method: 'POST', url: '/' };89 var route = new Route('');90 91 route.get(function(req, res, next) {81- assert(false);82- done();92+ throw new Error('not me!');93 })94 95 route.post(function(req, res, next) {86- assert(true);96+ req.called = true;97+ next();98 })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+ });105 })106 107 it('should allow fallthrough', function(done){108+ var req = { order: '', method: 'GET', url: '/' };109 var route = new Route('');110 96- var order = '';111 route.get(function(req, res, next) {98- order += 'a';112+ req.order += 'a';113 next();114 })115 116 route.all(function(req, res, next) {103- order += 'b';117+ req.order += 'b';118 next();119 });120 121 route.get(function(req, res, next) {108- order += 'c';122+ req.order += 'c';123+ next();124 })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+ });131 })132 })133 134 describe('errors', function(){135 it('should handle errors via arity 4 functions', function(done){136+ var req = { order: '', method: 'GET', url: '/' };137 var route = new Route('');138 121- var order = '';139 route.all(function(req, res, next){140 next(new Error('foobar'));141 });142 143 route.all(function(req, res, next){127- order += '0';144+ req.order += '0';145 next();146 });147 148 route.all(function(err, req, res, next){132- order += 'a';149+ req.order += 'a';150 next(err);151 });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');157 done();158 });141-142- route.dispatch({ method: 'get' }, {});159 })160 161 it('should handle throw', function(done) {162+ var req = { order: '', method: 'GET', url: '/' };163 var route = new Route('');164 148- var order = '';165 route.all(function(req, res, next){166 throw new Error('foobar');167 });168 169 route.all(function(req, res, next){154- order += '0';170+ req.order += '0';171 next();172 });173 174 route.all(function(err, req, res, next){159- order += 'a';175+ req.order += 'a';176 next(err);177 });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');183 done();184 });168-169- route.dispatch({ method: 'get' }, {});185 });186 187 it('should handle throwing inside error handlers', function(done) {188+ var req = { method: 'GET', url: '/' };189 var route = new Route('');190 191 route.get(function(req, res, next){192 throw new Error('boom!');3 unmodified lines196 throw new Error('oops');197 });198 199 route.get(function(err, req, res, next){184- assert.equal(err.message, 'oops');185- done();200+ req.message = err.message;201+ next();202 });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+ });209 });210 211 it('should handle throw in .all', function(done) {212+ var req = { method: 'GET', url: '/' };213 var route = new Route('');214 215 route.all(function(req, res, next){216 throw new Error('boom!');217 });218 198- route.dispatch({ url: '/', method: 'GET' }, {}, function(err){219+ route.dispatch(req, {}, function(err){220 should(err).be.ok;221 err.message.should.equal('boom!');222 done();223 });224 });225 226 it('should handle single error handler', function(done) {227+ var req = { method: 'GET', url: '/' };228 var route = new Route('');229 230 route.all(function(err, req, res, next){231 // this should not execute232 true.should.be.false;233 });234 213- route.dispatch({ url: '/', method: 'GET' }, {}, done);235+ route.dispatch(req, {}, done);236 });237 })238 })