Route.js
This file
Path
test/Route.js
238 lines · 5.5 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–238why, 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(){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) {17 req.called = true;18 next();19 });2021 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 });3637 route.all(function(req, res, next) {38 count++;39 next();40 });4142 methods.forEach(function testMethod(method) {43 var req = { method: method, url: '/' };44 route.dispatch(req, {}, cb);45 });46 })4748 it('should stack', function(done) {49 var req = { count: 0, method: 'GET', url: '/' };50 var route = new Route('/foo');5152 route.all(function(req, res, next) {53 req.count++;54 next();55 });5657 route.all(function(req, res, next) {58 req.count++;59 next();60 });6162 route.dispatch(req, {}, function (err) {63 if (err) return done(err);64 req.count.should.equal(2);65 done();66 });67 })68 })6970 describe('.VERB', function(){71 it('should support .get', function(done){72 var req = { method: 'GET', url: '/' };73 var route = new Route('');7475 route.get(function(req, res, next) {76 req.called = true;77 next();78 })7980 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) {92 throw new Error('not me!');93 })9495 route.post(function(req, res, next) {96 req.called = true;97 next();98 })99100 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('');110111 route.get(function(req, res, next) {112 req.order += 'a';113 next();114 })115116 route.all(function(req, res, next) {117 req.order += 'b';118 next();119 });120121 route.get(function(req, res, next) {122 req.order += 'c';123 next();124 })125126 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('');138139 route.all(function(req, res, next){140 next(new Error('foobar'));141 });142143 route.all(function(req, res, next){144 req.order += '0';145 next();146 });147148 route.all(function(err, req, res, next){149 req.order += 'a';150 next(err);151 });152153 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 });159 })160161 it('should handle throw', function(done) {162 var req = { order: '', method: 'GET', url: '/' };163 var route = new Route('');164165 route.all(function(req, res, next){166 throw new Error('foobar');167 });168169 route.all(function(req, res, next){170 req.order += '0';171 next();172 });173174 route.all(function(err, req, res, next){175 req.order += 'a';176 next(err);177 });178179 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 });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!');193 });194195 route.get(function(err, req, res, next){196 throw new Error('oops');197 });198199 route.get(function(err, req, res, next){200 req.message = err.message;201 next();202 });203204 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 });218219 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 });234235 route.dispatch(req, {}, done);236 });237 })238})