tests: change Route tests to use callback

This commit is contained in:
Douglas Christopher Wilson 2014-07-23 17:28:14 -04:00
parent f34944c539
commit d0b6b3dfcf

View File

@ -1,4 +1,5 @@
var after = require('after');
var should = require('should');
var express = require('../')
, Route = express.Route
@ -9,167 +10,182 @@ 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) {
assert.equal(req.a, 1);
assert.equal(res.b, 2);
req.called = true;
next();
});
route.dispatch({ a:1, method: 'GET' }, { b:2 }, done);
route.dispatch(req, {}, function (err) {
if (err) return done(err);
should(req.called).be.ok;
done();
});
})
it('should handle VERBS', function(done) {
var route = new Route('/foo');
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) {
route.dispatch({ method: method }, {});
var req = { method: method, url: '/' };
route.dispatch(req, {}, cb);
});
assert.equal(count, methods.length);
done();
})
it('should stack', function(done) {
var req = { count: 0, method: 'GET', url: '/' };
var route = new Route('/foo');
var count = 0;
route.all(function(req, res, next) {
count++;
req.count++;
next();
});
route.all(function(req, res, next) {
count++;
req.count++;
next();
});
route.dispatch({ method: 'GET' }, {}, function(err) {
assert.ifError(err);
count++;
route.dispatch(req, {}, function (err) {
if (err) return done(err);
req.count.should.equal(2);
done();
});
assert.equal(count, 3);
done();
})
})
describe('.VERB', function(){
it('should support .get', function(done){
var req = { method: 'GET', url: '/' };
var route = new Route('');
var count = 0;
route.get(function(req, res, next) {
count++;
req.called = true;
next();
})
route.dispatch({ method: 'GET' }, {});
assert(count);
done();
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) {
assert(false);
done();
throw new Error('not me!');
})
route.post(function(req, res, next) {
assert(true);
req.called = true;
next();
})
route.dispatch({ method: 'post' }, {});
done();
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('');
var order = '';
route.get(function(req, res, next) {
order += 'a';
req.order += 'a';
next();
})
route.all(function(req, res, next) {
order += 'b';
req.order += 'b';
next();
});
route.get(function(req, res, next) {
order += 'c';
req.order += 'c';
next();
})
route.dispatch({ method: 'get' }, {});
assert.equal(order, 'abc');
done();
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('');
var order = '';
route.all(function(req, res, next){
next(new Error('foobar'));
});
route.all(function(req, res, next){
order += '0';
req.order += '0';
next();
});
route.all(function(err, req, res, next){
order += 'a';
req.order += 'a';
next(err);
});
route.all(function(err, req, res, next){
assert.equal(err.message, 'foobar');
assert.equal(order, 'a');
route.dispatch(req, {}, function (err) {
should(err).be.ok;
should(err.message).equal('foobar');
req.order.should.equal('a');
done();
});
route.dispatch({ method: 'get' }, {});
})
it('should handle throw', function(done) {
var req = { order: '', method: 'GET', url: '/' };
var route = new Route('');
var order = '';
route.all(function(req, res, next){
throw new Error('foobar');
});
route.all(function(req, res, next){
order += '0';
req.order += '0';
next();
});
route.all(function(err, req, res, next){
order += 'a';
req.order += 'a';
next(err);
});
route.all(function(err, req, res, next){
assert.equal(err.message, 'foobar');
assert.equal(order, 'a');
route.dispatch(req, {}, function (err) {
should(err).be.ok;
should(err.message).equal('foobar');
req.order.should.equal('a');
done();
});
route.dispatch({ method: 'get' }, {});
});
it('should handle throwing inside error handlers', function(done) {
var req = { method: 'GET', url: '/' };
var route = new Route('');
route.get(function(req, res, next){
@ -181,21 +197,26 @@ describe('Route', function(){
});
route.get(function(err, req, res, next){
assert.equal(err.message, 'oops');
done();
req.message = err.message;
next();
});
route.dispatch({ url: '/', method: 'GET' }, {});
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({ url: '/', method: 'GET' }, {}, function(err){
route.dispatch(req, {}, function(err){
should(err).be.ok;
err.message.should.equal('boom!');
done();
@ -203,6 +224,7 @@ describe('Route', function(){
});
it('should handle single error handler', function(done) {
var req = { method: 'GET', url: '/' };
var route = new Route('');
route.all(function(err, req, res, next){
@ -210,7 +232,7 @@ describe('Route', function(){
true.should.be.false;
});
route.dispatch({ url: '/', method: 'GET' }, {}, done);
route.dispatch(req, {}, done);
});
})
})