Route.js
This file
Path
test/Route.js
216 lines · 4.6 KiB · JavaScript
Last changed by
Fix handling when route.all is only routeDouglas Christopher Wilsonbefore adoption7f26cfc
History of this fileTake it
The walk stopped after 200 commits: lines older than f41d09a point at it.
remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 1why, one step back1Fix handling when route.all is only routeDouglas Christopher Wilsonbefore adoption7f26cfc
line 2why, one step back2var should = require('should');remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 3–143why, one step back3var express = require('../')4 , Route = express.Route5 , methods = require('methods')6 , assert = require('assert');78describe('Route', function(){910 describe('.all', function(){11 it('should add handler', function(done){12 var route = new Route('/foo');1314 route.all(function(req, res, next) {15 assert.equal(req.a, 1);16 assert.equal(res.b, 2);17 next();18 });1920 route.dispatch({ a:1, method: 'GET' }, { b:2 }, done);21 })2223 it('should handle VERBS', function(done) {24 var route = new Route('/foo');2526 var count = 0;27 route.all(function(req, res, next) {28 count++;29 });3031 methods.forEach(function testMethod(method) {32 route.dispatch({ method: method }, {});33 });3435 assert.equal(count, methods.length);36 done();37 })3839 it('should stack', function(done) {40 var route = new Route('/foo');4142 var count = 0;43 route.all(function(req, res, next) {44 count++;45 next();46 });4748 route.all(function(req, res, next) {49 count++;50 next();51 });5253 route.dispatch({ method: 'GET' }, {}, function(err) {54 assert.ifError(err);55 count++;56 });5758 assert.equal(count, 3);59 done();60 })61 })6263 describe('.VERB', function(){64 it('should support .get', function(done){65 var route = new Route('');6667 var count = 0;68 route.get(function(req, res, next) {69 count++;70 })7172 route.dispatch({ method: 'GET' }, {});73 assert(count);74 done();75 })7677 it('should limit to just .VERB', function(done){78 var route = new Route('');7980 route.get(function(req, res, next) {81 assert(false);82 done();83 })8485 route.post(function(req, res, next) {86 assert(true);87 })8889 route.dispatch({ method: 'post' }, {});90 done();91 })9293 it('should allow fallthrough', function(done){94 var route = new Route('');9596 var order = '';97 route.get(function(req, res, next) {98 order += 'a';99 next();100 })101102 route.all(function(req, res, next) {103 order += 'b';104 next();105 });106107 route.get(function(req, res, next) {108 order += 'c';109 })110111 route.dispatch({ method: 'get' }, {});112 assert.equal(order, 'abc');113 done();114 })115 })116117 describe('errors', function(){118 it('should handle errors via arity 4 functions', function(done){119 var route = new Route('');120121 var order = '';122 route.all(function(req, res, next){123 next(new Error('foobar'));124 });125126 route.all(function(req, res, next){127 order += '0';128 next();129 });130131 route.all(function(err, req, res, next){132 order += 'a';133 next(err);134 });135136 route.all(function(err, req, res, next){137 assert.equal(err.message, 'foobar');138 assert.equal(order, 'a');139 done();140 });141142 route.dispatch({ method: 'get' }, {});143 })handle thrown errors inside RouteRoman Shtylmanbefore adoptiond72f279
lines 144–170why, one step back144145 it('should handle throw', function(done) {146 var route = new Route('');147148 var order = '';149 route.all(function(req, res, next){150 throw new Error('foobar');151 });152153 route.all(function(req, res, next){154 order += '0';155 next();156 });157158 route.all(function(err, req, res, next){159 order += 'a';160 next(err);161 });162163 route.all(function(err, req, res, next){164 assert.equal(err.message, 'foobar');165 assert.equal(order, 'a');166 done();167 });168169 route.dispatch({ method: 'get' }, {});170 });tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
lines 171–185why, one step back171172 it('should handle throwing inside error handlers', function(done) {173 var route = new Route('');174175 route.get(function(req, res, next){176 throw new Error('boom!');177 });178179 route.get(function(err, req, res, next){180 throw new Error('oops');181 });182183 route.get(function(err, req, res, next){184 assert.equal(err.message, 'oops');185 done();Fix handling when route.all is only routeDouglas Christopher Wilsonbefore adoption7f26cfc
lines 186–210why, one step back186 });187188 route.dispatch({ url: '/', method: 'GET' }, {});189 });190191 it('should handle throw in .all', function(done) {192 var route = new Route('');193194 route.all(function(req, res, next){195 throw new Error('boom!');196 });197198 route.dispatch({ url: '/', method: 'GET' }, {}, function(err){199 should(err).be.ok;200 err.message.should.equal('boom!');201 done();202 });203 });204205 it('should handle single error handler', function(done) {206 var route = new Route('');207208 route.all(function(err, req, res, next){209 // this should not execute210 true.should.be.false;tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
lines 211–214why, one step back211 });212213 route.dispatch({ url: '/', method: 'GET' }, {}, done);214 });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 215–216why, one step back215 })216})