Router.js
This file
Path
test/Router.js
441 lines · 11.8 KiB · JavaScript
Last changed by
Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
History of this fileTake it
The walk stopped after 200 commits: lines older than fb2d918 point at it.
fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
lines 1–131why, one step back12var after = require('after');3var express = require('../')4 , Router = express.Router5 , methods = require('methods')6 , assert = require('assert');78describe('Router', function(){9 it('should return a function with router methods', function() {10 var router = Router();11 assert(typeof router == 'function');1213 var router = new Router();14 assert(typeof router == 'function');1516 assert(typeof router.get == 'function');17 assert(typeof router.handle == 'function');18 assert(typeof router.use == 'function');19 });2021 it('should support .use of other routers', function(done){22 var router = new Router();23 var another = new Router();2425 another.get('/bar', function(req, res){26 res.end();27 });28 router.use('/foo', another);2930 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });31 });3233 it('should support dynamic routes', function(done){34 var router = new Router();35 var another = new Router();3637 another.get('/:bar', function(req, res){38 req.params.bar.should.equal('route');39 res.end();40 });41 router.use('/:foo', another);4243 router.handle({ url: '/test/route', method: 'GET' }, { end: done });44 });4546 describe('.handle', function(){47 it('should dispatch', function(done){48 var router = new Router();4950 router.route('/foo').get(function(req, res){51 res.send('foo');52 });5354 var res = {55 send: function(val) {56 val.should.equal('foo');57 done();58 }59 }60 router.handle({ url: '/foo', method: 'GET' }, res);61 })62 })6364 describe('.multiple callbacks', function(){65 it('should throw if a callback is null', function(){66 assert.throws(function () {67 var router = new Router();68 router.route('/foo').all(null);69 })70 })7172 it('should throw if a callback is undefined', function(){73 assert.throws(function () {74 var router = new Router();75 router.route('/foo').all(undefined);76 })77 })7879 it('should throw if a callback is not a function', function(){80 assert.throws(function () {81 var router = new Router();82 router.route('/foo').all('not a function');83 })84 })8586 it('should not throw if all callbacks are functions', function(){87 var router = new Router();88 router.route('/foo').all(function(){}).all(function(){});89 })90 })9192 describe('error', function(){93 it('should skip non error middleware', function(done){94 var router = new Router();9596 router.get('/foo', function(req, res, next){97 next(new Error('foo'));98 });99100 router.get('/bar', function(req, res, next){101 next(new Error('bar'));102 });103104 router.use(function(req, res, next){105 assert(false);106 });107108 router.use(function(err, req, res, next){109 assert.equal(err.message, 'foo');110 done();111 });112113 router.handle({ url: '/foo', method: 'GET' }, {}, done);114 });115116 it('should handle throwing inside routes with params', function(done) {117 var router = new Router();118119 router.get('/foo/:id', function(req, res, next){120 throw new Error('foo');121 });122123 router.use(function(req, res, next){124 assert(false);125 });126127 router.use(function(err, req, res, next){128 assert.equal(err.message, 'foo');129 done();130 });131Fix catching errors from top-level handlersDouglas Christopher Wilsonbefore adoption4d3e0d8
lines 132–154why, one step back132 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});133 });134135 it('should handle throwing in handler after async param', function(done) {136 var router = new Router();137138 router.param('user', function(req, res, next, val){139 process.nextTick(function(){140 req.user = val;141 next();142 });143 });144145 router.use('/:user', function(req, res, next){146 throw new Error('oh no!');147 });148149 router.use(function(err, req, res, next){150 assert.equal(err.message, 'oh no!');151 done();152 });153154 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
line 155why, one step back155 });tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
lines 156–174why, one step back156157 it('should handle throwing inside error handlers', function(done) {158 var router = new Router();159160 router.use(function(req, res, next){161 throw new Error('boom!');162 });163164 router.use(function(err, req, res, next){165 throw new Error('oops');166 });167168 router.use(function(err, req, res, next){169 assert.equal(err.message, 'oops');170 done();171 });172173 router.handle({ url: '/', method: 'GET' }, {}, done);174 });fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
lines 175–176why, one step back175 })176tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c
lines 177–281why, one step back177 describe('FQDN', function () {178 it('should not obscure FQDNs', function (done) {179 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };180 var router = new Router();181182 router.use(function (req, res, next) {183 assert.equal(req.hit++, 0);184 assert.equal(req.url, 'http://example.com/foo');185 next();186 });187188 router.handle(request, {}, function (err) {189 if (err) return done(err);190 assert.equal(request.hit, 1);191 done();192 });193 });194195 it('should ignore FQDN in search', function (done) {196 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };197 var router = new Router();198199 router.use('/proxy', function (req, res, next) {200 assert.equal(req.hit++, 0);201 assert.equal(req.url, '/?url=http://example.com/blog/post/1');202 next();203 });204205 router.handle(request, {}, function (err) {206 if (err) return done(err);207 assert.equal(request.hit, 1);208 done();209 });210 });211212 it('should adjust FQDN req.url', function (done) {213 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };214 var router = new Router();215216 router.use('/blog', function (req, res, next) {217 assert.equal(req.hit++, 0);218 assert.equal(req.url, 'http://example.com/post/1');219 next();220 });221222 router.handle(request, {}, function (err) {223 if (err) return done(err);224 assert.equal(request.hit, 1);225 done();226 });227 });228229 it('should adjust FQDN req.url with multiple handlers', function (done) {230 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };231 var router = new Router();232233 router.use(function (req, res, next) {234 assert.equal(req.hit++, 0);235 assert.equal(req.url, 'http://example.com/blog/post/1');236 next();237 });238239 router.use('/blog', function (req, res, next) {240 assert.equal(req.hit++, 1);241 assert.equal(req.url, 'http://example.com/post/1');242 next();243 });244245 router.handle(request, {}, function (err) {246 if (err) return done(err);247 assert.equal(request.hit, 2);248 done();249 });250 });251252 it('should adjust FQDN req.url with multiple routed handlers', function (done) {253 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };254 var router = new Router();255256 router.use('/blog', function (req, res, next) {257 assert.equal(req.hit++, 0);258 assert.equal(req.url, 'http://example.com/post/1');259 next();260 });261262 router.use('/blog', function (req, res, next) {263 assert.equal(req.hit++, 1);264 assert.equal(req.url, 'http://example.com/post/1');265 next();266 });267268 router.use(function (req, res, next) {269 assert.equal(req.hit++, 2);270 assert.equal(req.url, 'http://example.com/blog/post/1');271 next();272 });273274 router.handle(request, {}, function (err) {275 if (err) return done(err);276 assert.equal(request.hit, 3);277 done();278 });279 });280 })281fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
lines 282–299why, one step back282 describe('.all', function() {283 it('should support using .all to capture all http verbs', function(done){284 var router = new Router();285286 var count = 0;287 router.all('/foo', function(){ count++; });288289 var url = '/foo?bar=baz';290291 methods.forEach(function testMethod(method) {292 router.handle({ url: url, method: method }, {}, function() {});293 });294295 assert.equal(count, methods.length);296 done();297 })298 })299tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 300–302why, one step back300 describe('.use', function() {301 it('should require arguments', function(){302 var router = new Router();Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
line 303why, one step back303 router.use.bind(router).should.throw(/requires middleware function/)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 304–307why, one step back304 })305306 it('should not accept non-functions', function(){307 var router = new Router();Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
lines 308–311why, one step back308 router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)309 router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)310 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)311 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 312–314why, one step back312 })313 })314fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
lines 315–441why, one step back315 describe('.param', function() {316 it('should call param function when routing VERBS', function(done) {317 var router = new Router();318319 router.param('id', function(req, res, next, id) {320 assert.equal(id, '123');321 next();322 });323324 router.get('/foo/:id/bar', function(req, res, next) {325 assert.equal(req.params.id, '123');326 next();327 });328329 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);330 });331332 it('should call param function when routing middleware', function(done) {333 var router = new Router();334335 router.param('id', function(req, res, next, id) {336 assert.equal(id, '123');337 next();338 });339340 router.use('/foo/:id/bar', function(req, res, next) {341 assert.equal(req.params.id, '123');342 assert.equal(req.url, '/baz');343 next();344 });345346 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);347 });348349 it('should only call once per request', function(done) {350 var count = 0;351 var req = { url: '/foo/bob/bar', method: 'get' };352 var router = new Router();353 var sub = new Router();354355 sub.get('/bar', function(req, res, next) {356 next();357 });358359 router.param('user', function(req, res, next, user) {360 count++;361 req.user = user;362 next();363 });364365 router.use('/foo/:user/', new Router());366 router.use('/foo/:user/', sub);367368 router.handle(req, {}, function(err) {369 if (err) return done(err);370 assert.equal(count, 1);371 assert.equal(req.user, 'bob');372 done();373 });374 });375376 it('should call when values differ', function(done) {377 var count = 0;378 var req = { url: '/foo/bob/bar', method: 'get' };379 var router = new Router();380 var sub = new Router();381382 sub.get('/bar', function(req, res, next) {383 next();384 });385386 router.param('user', function(req, res, next, user) {387 count++;388 req.user = user;389 next();390 });391392 router.use('/foo/:user/', new Router());393 router.use('/:user/bob/', sub);394395 router.handle(req, {}, function(err) {396 if (err) return done(err);397 assert.equal(count, 2);398 assert.equal(req.user, 'foo');399 done();400 });401 });402 });403404 describe('parallel requests', function() {405 it('should not mix requests', function(done) {406 var req1 = { url: '/foo/50/bar', method: 'get' };407 var req2 = { url: '/foo/10/bar', method: 'get' };408 var router = new Router();409 var sub = new Router();410411 done = after(2, done);412413 sub.get('/bar', function(req, res, next) {414 next();415 });416417 router.param('ms', function(req, res, next, ms) {418 ms = parseInt(ms, 10);419 req.ms = ms;420 setTimeout(next, ms);421 });422423 router.use('/foo/:ms/', new Router());424 router.use('/foo/:ms/', sub);425426 router.handle(req1, {}, function(err) {427 assert.ifError(err);428 assert.equal(req1.ms, 50);429 assert.equal(req1.originalUrl, '/foo/50/bar');430 done();431 });432433 router.handle(req2, {}, function(err) {434 assert.ifError(err);435 assert.equal(req2.ms, 10);436 assert.equal(req2.originalUrl, '/foo/10/bar');437 done();438 });439 });440 });441})