Route.js
This file
Path
test/Route.js
190 lines · 4.0 KiB · JavaScript
Last changed by
tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
History of this fileTake it
remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 1–142why, one step back12var express = require('../')3 , Route = express.Route4 , methods = require('methods')5 , assert = require('assert');67describe('Route', function(){89 describe('.all', function(){10 it('should add handler', function(done){11 var route = new Route('/foo');1213 route.all(function(req, res, next) {14 assert.equal(req.a, 1);15 assert.equal(res.b, 2);16 next();17 });1819 route.dispatch({ a:1, method: 'GET' }, { b:2 }, done);20 })2122 it('should handle VERBS', function(done) {23 var route = new Route('/foo');2425 var count = 0;26 route.all(function(req, res, next) {27 count++;28 });2930 methods.forEach(function testMethod(method) {31 route.dispatch({ method: method }, {});32 });3334 assert.equal(count, methods.length);35 done();36 })3738 it('should stack', function(done) {39 var route = new Route('/foo');4041 var count = 0;42 route.all(function(req, res, next) {43 count++;44 next();45 });4647 route.all(function(req, res, next) {48 count++;49 next();50 });5152 route.dispatch({ method: 'GET' }, {}, function(err) {53 assert.ifError(err);54 count++;55 });5657 assert.equal(count, 3);58 done();59 })60 })6162 describe('.VERB', function(){63 it('should support .get', function(done){64 var route = new Route('');6566 var count = 0;67 route.get(function(req, res, next) {68 count++;69 })7071 route.dispatch({ method: 'GET' }, {});72 assert(count);73 done();74 })7576 it('should limit to just .VERB', function(done){77 var route = new Route('');7879 route.get(function(req, res, next) {80 assert(false);81 done();82 })8384 route.post(function(req, res, next) {85 assert(true);86 })8788 route.dispatch({ method: 'post' }, {});89 done();90 })9192 it('should allow fallthrough', function(done){93 var route = new Route('');9495 var order = '';96 route.get(function(req, res, next) {97 order += 'a';98 next();99 })100101 route.all(function(req, res, next) {102 order += 'b';103 next();104 });105106 route.get(function(req, res, next) {107 order += 'c';108 })109110 route.dispatch({ method: 'get' }, {});111 assert.equal(order, 'abc');112 done();113 })114 })115116 describe('errors', function(){117 it('should handle errors via arity 4 functions', function(done){118 var route = new Route('');119120 var order = '';121 route.all(function(req, res, next){122 next(new Error('foobar'));123 });124125 route.all(function(req, res, next){126 order += '0';127 next();128 });129130 route.all(function(err, req, res, next){131 order += 'a';132 next(err);133 });134135 route.all(function(err, req, res, next){136 assert.equal(err.message, 'foobar');137 assert.equal(order, 'a');138 done();139 });140141 route.dispatch({ method: 'get' }, {});142 })handle thrown errors inside RouteRoman Shtylmanbefore adoptiond72f279
lines 143–169why, one step back143144 it('should handle throw', function(done) {145 var route = new Route('');146147 var order = '';148 route.all(function(req, res, next){149 throw new Error('foobar');150 });151152 route.all(function(req, res, next){153 order += '0';154 next();155 });156157 route.all(function(err, req, res, next){158 order += 'a';159 next(err);160 });161162 route.all(function(err, req, res, next){163 assert.equal(err.message, 'foobar');164 assert.equal(order, 'a');165 done();166 });167168 route.dispatch({ method: 'get' }, {});169 });tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
lines 170–188why, one step back170171 it('should handle throwing inside error handlers', function(done) {172 var route = new Route('');173174 route.get(function(req, res, next){175 throw new Error('boom!');176 });177178 route.get(function(err, req, res, next){179 throw new Error('oops');180 });181182 route.get(function(err, req, res, next){183 assert.equal(err.message, 'oops');184 done();185 });186187 route.dispatch({ url: '/', method: 'GET' }, {}, done);188 });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 189–190why, one step back189 })190})