Skip to content
Myr

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

1 file+77 added−55 removed

modified:test/Route.js+77 added-55 removed
12var after = require('after');3var should = require('should');4var express = require('../')5 , Route = express.Route6 , methods = require('methods')
2 unmodified lines
9describe('Route', function(){1011 describe('.all', function(){12 it('should add handler', function(done){13 var req = { method: 'GET', url: '/' };14 var route = new Route('/foo');1516 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 });2020 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 })2728 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 });3626 var count = 0;37 route.all(function(req, res, next) {38 count++;39 next();40 });4142 methods.forEach(function testMethod(method) {32 route.dispatch({ method: method }, {});43 var req = { method: method, url: '/' };44 route.dispatch(req, {}, cb);45 });3435 assert.equal(count, methods.length);36 done();46 })4748 it('should stack', function(done) {49 var req = { count: 0, method: 'GET', url: '/' };50 var route = new Route('/foo');5142 var count = 0;52 route.all(function(req, res, next) {44 count++;53 req.count++;54 next();55 });5657 route.all(function(req, res, next) {49 count++;58 req.count++;59 next();60 });6153 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 });5758 assert.equal(count, 3);59 done();67 })68 })6970 describe('.VERB', function(){71 it('should support .get', function(done){72 var req = { method: 'GET', url: '/' };73 var route = new Route('');7467 var count = 0;75 route.get(function(req, res, next) {69 count++;76 req.called = true;77 next();78 })7972 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 })8687 it('should limit to just .VERB', function(done){88 var req = { method: 'POST', url: '/' };89 var route = new Route('');9091 route.get(function(req, res, next) {81 assert(false);82 done();92 throw new Error('not me!');93 })9495 route.post(function(req, res, next) {86 assert(true);96 req.called = true;97 next();98 })9989 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 })106107 it('should allow fallthrough', function(done){108 var req = { order: '', method: 'GET', url: '/' };109 var route = new Route('');11096 var order = '';111 route.get(function(req, res, next) {98 order += 'a';112 req.order += 'a';113 next();114 })115116 route.all(function(req, res, next) {103 order += 'b';117 req.order += 'b';118 next();119 });120121 route.get(function(req, res, next) {108 order += 'c';122 req.order += 'c';123 next();124 })125111 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 })133134 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('');138121 var order = '';139 route.all(function(req, res, next){140 next(new Error('foobar'));141 });142143 route.all(function(req, res, next){127 order += '0';144 req.order += '0';145 next();146 });147148 route.all(function(err, req, res, next){132 order += 'a';149 req.order += 'a';150 next(err);151 });152136 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 });141142 route.dispatch({ method: 'get' }, {});159 })160161 it('should handle throw', function(done) {162 var req = { order: '', method: 'GET', url: '/' };163 var route = new Route('');164148 var order = '';165 route.all(function(req, res, next){166 throw new Error('foobar');167 });168169 route.all(function(req, res, next){154 order += '0';170 req.order += '0';171 next();172 });173174 route.all(function(err, req, res, next){159 order += 'a';175 req.order += 'a';176 next(err);177 });178163 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 });168169 route.dispatch({ method: 'get' }, {});185 });186187 it('should handle throwing inside error handlers', function(done) {188 var req = { method: 'GET', url: '/' };189 var route = new Route('');190191 route.get(function(req, res, next){192 throw new Error('boom!');
3 unmodified lines
196 throw new Error('oops');197 });198199 route.get(function(err, req, res, next){184 assert.equal(err.message, 'oops');185 done();200 req.message = err.message;201 next();202 });203188 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 });210211 it('should handle throw in .all', function(done) {212 var req = { method: 'GET', url: '/' };213 var route = new Route('');214215 route.all(function(req, res, next){216 throw new Error('boom!');217 });218198 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 });225226 it('should handle single error handler', function(done) {227 var req = { method: 'GET', url: '/' };228 var route = new Route('');229230 route.all(function(err, req, res, next){231 // this should not execute232 true.should.be.false;233 });234213 route.dispatch({ url: '/', method: 'GET' }, {}, done);235 route.dispatch(req, {}, done);236 });237 })238})