Route.js
This file
Path
test/Route.js
242 lines · 5.6 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than fa272ed point at it.
docs: fix typo in jsdoc commentHrvoje Šimićbefore adoptionfa272ed
lines 1–242why, one step back12var after = require('after');3var should = require('should');4var express = require('../')5 , Route = express.Route6 , methods = require('methods')78describe('Route', function(){9 it('should work without handlers', function(done) {10 var req = { method: 'GET', url: '/' }11 var route = new Route('/foo')12 route.dispatch(req, {}, done)13 })1415 describe('.all', function(){16 it('should add handler', function(done){17 var req = { method: 'GET', url: '/' };18 var route = new Route('/foo');1920 route.all(function(req, res, next) {21 req.called = true;22 next();23 });2425 route.dispatch(req, {}, function (err) {26 if (err) return done(err);27 should(req.called).be.ok()28 done();29 });30 })3132 it('should handle VERBS', function(done) {33 var count = 0;34 var route = new Route('/foo');35 var cb = after(methods.length, function (err) {36 if (err) return done(err);37 count.should.equal(methods.length);38 done();39 });4041 route.all(function(req, res, next) {42 count++;43 next();44 });4546 methods.forEach(function testMethod(method) {47 var req = { method: method, url: '/' };48 route.dispatch(req, {}, cb);49 });50 })5152 it('should stack', function(done) {53 var req = { count: 0, method: 'GET', url: '/' };54 var route = new Route('/foo');5556 route.all(function(req, res, next) {57 req.count++;58 next();59 });6061 route.all(function(req, res, next) {62 req.count++;63 next();64 });6566 route.dispatch(req, {}, function (err) {67 if (err) return done(err);68 req.count.should.equal(2);69 done();70 });71 })72 })7374 describe('.VERB', function(){75 it('should support .get', function(done){76 var req = { method: 'GET', url: '/' };77 var route = new Route('');7879 route.get(function(req, res, next) {80 req.called = true;81 next();82 })8384 route.dispatch(req, {}, function (err) {85 if (err) return done(err);86 should(req.called).be.ok()87 done();88 });89 })9091 it('should limit to just .VERB', function(done){92 var req = { method: 'POST', url: '/' };93 var route = new Route('');9495 route.get(function(req, res, next) {96 throw new Error('not me!');97 })9899 route.post(function(req, res, next) {100 req.called = true;101 next();102 })103104 route.dispatch(req, {}, function (err) {105 if (err) return done(err);106 should(req.called).be.true()107 done();108 });109 })110111 it('should allow fallthrough', function(done){112 var req = { order: '', method: 'GET', url: '/' };113 var route = new Route('');114115 route.get(function(req, res, next) {116 req.order += 'a';117 next();118 })119120 route.all(function(req, res, next) {121 req.order += 'b';122 next();123 });124125 route.get(function(req, res, next) {126 req.order += 'c';127 next();128 })129130 route.dispatch(req, {}, function (err) {131 if (err) return done(err);132 req.order.should.equal('abc');133 done();134 });135 })136 })137138 describe('errors', function(){139 it('should handle errors via arity 4 functions', function(done){140 var req = { order: '', method: 'GET', url: '/' };141 var route = new Route('');142143 route.all(function(req, res, next){144 next(new Error('foobar'));145 });146147 route.all(function(req, res, next){148 req.order += '0';149 next();150 });151152 route.all(function(err, req, res, next){153 req.order += 'a';154 next(err);155 });156157 route.dispatch(req, {}, function (err) {158 should(err).be.ok()159 should(err.message).equal('foobar');160 req.order.should.equal('a');161 done();162 });163 })164165 it('should handle throw', function(done) {166 var req = { order: '', method: 'GET', url: '/' };167 var route = new Route('');168169 route.all(function(req, res, next){170 throw new Error('foobar');171 });172173 route.all(function(req, res, next){174 req.order += '0';175 next();176 });177178 route.all(function(err, req, res, next){179 req.order += 'a';180 next(err);181 });182183 route.dispatch(req, {}, function (err) {184 should(err).be.ok()185 should(err.message).equal('foobar');186 req.order.should.equal('a');187 done();188 });189 });190191 it('should handle throwing inside error handlers', function(done) {192 var req = { method: 'GET', url: '/' };193 var route = new Route('');194195 route.get(function(req, res, next){196 throw new Error('boom!');197 });198199 route.get(function(err, req, res, next){200 throw new Error('oops');201 });202203 route.get(function(err, req, res, next){204 req.message = err.message;205 next();206 });207208 route.dispatch(req, {}, function (err) {209 if (err) return done(err);210 should(req.message).equal('oops');211 done();212 });213 });214215 it('should handle throw in .all', function(done) {216 var req = { method: 'GET', url: '/' };217 var route = new Route('');218219 route.all(function(req, res, next){220 throw new Error('boom!');221 });222223 route.dispatch(req, {}, function(err){224 should(err).be.ok()225 err.message.should.equal('boom!');226 done();227 });228 });229230 it('should handle single error handler', function(done) {231 var req = { method: 'GET', url: '/' };232 var route = new Route('');233234 route.all(function(err, req, res, next){235 // this should not execute236 true.should.be.false()237 });238239 route.dispatch(req, {}, done);240 });241 })242})