Router.js
This file
Path
test/Router.js
640 lines · 16.9 KiB · JavaScript
Last changed by
Fix routing requests without methodDouglas Christopher Wilsonbefore adoption74beeac
History of this fileTake it
1'use strict'23var after = require('after');4var express = require('../')5 , Router = express.Router6 , methods = require('methods')7 , assert = require('assert');89describe('Router', function(){10 it('should return a function with router methods', function() {11 var router = new Router();12 assert(typeof router === 'function')1314 assert(typeof router.get === 'function')15 assert(typeof router.handle === 'function')16 assert(typeof router.use === 'function')17 });1819 it('should support .use of other routers', function(done){20 var router = new Router();21 var another = new Router();2223 another.get('/bar', function(req, res){24 res.end();25 });26 router.use('/foo', another);2728 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });29 });3031 it('should support dynamic routes', function(done){32 var router = new Router();33 var another = new Router();3435 another.get('/:bar', function(req, res){36 assert.strictEqual(req.params.bar, 'route')37 res.end();38 });39 router.use('/:foo', another);4041 router.handle({ url: '/test/route', method: 'GET' }, { end: done });42 });4344 it('should handle blank URL', function(done){45 var router = new Router();4647 router.use(function (req, res) {48 throw new Error('should not be called')49 });5051 router.handle({ url: '', method: 'GET' }, {}, done);52 });5354 it('should handle missing URL', function (done) {55 var router = new Router()5657 router.use(function (req, res) {58 throw new Error('should not be called')59 })6061 router.handle({ method: 'GET' }, {}, done)62 })6364 it('handle missing method', function (done) {65 var all = false66 var router = new Router()67 var route = router.route('/foo')68 var use = false6970 route.post(function (req, res, next) { next(new Error('should not run')) })71 route.all(function (req, res, next) {72 all = true73 next()74 })75 route.get(function (req, res, next) { next(new Error('should not run')) })7677 router.get('/foo', function (req, res, next) { next(new Error('should not run')) })78 router.use(function (req, res, next) {79 use = true80 next()81 })8283 router.handle({ url: '/foo' }, {}, function (err) {84 if (err) return done(err)85 assert.ok(all)86 assert.ok(use)87 done()88 })89 })9091 it('should not stack overflow with many registered routes', function(done){92 this.timeout(5000) // long-running test9394 var handler = function(req, res){ res.end(new Error('wrong handler')) };95 var router = new Router();9697 for (var i = 0; i < 6000; i++) {98 router.get('/thing' + i, handler)99 }100101 router.get('/', function (req, res) {102 res.end();103 });104105 router.handle({ url: '/', method: 'GET' }, { end: done });106 });107108 it('should not stack overflow with a large sync route stack', function (done) {109 this.timeout(5000) // long-running test110111 var router = new Router()112113 router.get('/foo', function (req, res, next) {114 req.counter = 0115 next()116 })117118 for (var i = 0; i < 6000; i++) {119 router.get('/foo', function (req, res, next) {120 req.counter++121 next()122 })123 }124125 router.get('/foo', function (req, res) {126 assert.strictEqual(req.counter, 6000)127 res.end()128 })129130 router.handle({ url: '/foo', method: 'GET' }, { end: done })131 })132133 it('should not stack overflow with a large sync middleware stack', function (done) {134 this.timeout(5000) // long-running test135136 var router = new Router()137138 router.use(function (req, res, next) {139 req.counter = 0140 next()141 })142143 for (var i = 0; i < 6000; i++) {144 router.use(function (req, res, next) {145 req.counter++146 next()147 })148 }149150 router.use(function (req, res) {151 assert.strictEqual(req.counter, 6000)152 res.end()153 })154155 router.handle({ url: '/', method: 'GET' }, { end: done })156 })157158 describe('.handle', function(){159 it('should dispatch', function(done){160 var router = new Router();161162 router.route('/foo').get(function(req, res){163 res.send('foo');164 });165166 var res = {167 send: function(val) {168 assert.strictEqual(val, 'foo')169 done();170 }171 }172 router.handle({ url: '/foo', method: 'GET' }, res);173 })174 })175176 describe('.multiple callbacks', function(){177 it('should throw if a callback is null', function(){178 assert.throws(function () {179 var router = new Router();180 router.route('/foo').all(null);181 })182 })183184 it('should throw if a callback is undefined', function(){185 assert.throws(function () {186 var router = new Router();187 router.route('/foo').all(undefined);188 })189 })190191 it('should throw if a callback is not a function', function(){192 assert.throws(function () {193 var router = new Router();194 router.route('/foo').all('not a function');195 })196 })197198 it('should not throw if all callbacks are functions', function(){199 var router = new Router();200 router.route('/foo').all(function(){}).all(function(){});201 })202 })203204 describe('error', function(){205 it('should skip non error middleware', function(done){206 var router = new Router();207208 router.get('/foo', function(req, res, next){209 next(new Error('foo'));210 });211212 router.get('/bar', function(req, res, next){213 next(new Error('bar'));214 });215216 router.use(function(req, res, next){217 assert(false);218 });219220 router.use(function(err, req, res, next){221 assert.equal(err.message, 'foo');222 done();223 });224225 router.handle({ url: '/foo', method: 'GET' }, {}, done);226 });227228 it('should handle throwing inside routes with params', function(done) {229 var router = new Router();230231 router.get('/foo/:id', function () {232 throw new Error('foo');233 });234235 router.use(function(req, res, next){236 assert(false);237 });238239 router.use(function(err, req, res, next){240 assert.equal(err.message, 'foo');241 done();242 });243244 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});245 });246247 it('should handle throwing in handler after async param', function(done) {248 var router = new Router();249250 router.param('user', function(req, res, next, val){251 process.nextTick(function(){252 req.user = val;253 next();254 });255 });256257 router.use('/:user', function(req, res, next){258 throw new Error('oh no!');259 });260261 router.use(function(err, req, res, next){262 assert.equal(err.message, 'oh no!');263 done();264 });265266 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});267 });268269 it('should handle throwing inside error handlers', function(done) {270 var router = new Router();271272 router.use(function(req, res, next){273 throw new Error('boom!');274 });275276 router.use(function(err, req, res, next){277 throw new Error('oops');278 });279280 router.use(function(err, req, res, next){281 assert.equal(err.message, 'oops');282 done();283 });284285 router.handle({ url: '/', method: 'GET' }, {}, done);286 });287 })288289 describe('FQDN', function () {290 it('should not obscure FQDNs', function (done) {291 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };292 var router = new Router();293294 router.use(function (req, res, next) {295 assert.equal(req.hit++, 0);296 assert.equal(req.url, 'http://example.com/foo');297 next();298 });299300 router.handle(request, {}, function (err) {301 if (err) return done(err);302 assert.equal(request.hit, 1);303 done();304 });305 });306307 it('should ignore FQDN in search', function (done) {308 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };309 var router = new Router();310311 router.use('/proxy', function (req, res, next) {312 assert.equal(req.hit++, 0);313 assert.equal(req.url, '/?url=http://example.com/blog/post/1');314 next();315 });316317 router.handle(request, {}, function (err) {318 if (err) return done(err);319 assert.equal(request.hit, 1);320 done();321 });322 });323324 it('should ignore FQDN in path', function (done) {325 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };326 var router = new Router();327328 router.use('/proxy', function (req, res, next) {329 assert.equal(req.hit++, 0);330 assert.equal(req.url, '/http://example.com/blog/post/1');331 next();332 });333334 router.handle(request, {}, function (err) {335 if (err) return done(err);336 assert.equal(request.hit, 1);337 done();338 });339 });340341 it('should adjust FQDN req.url', function (done) {342 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };343 var router = new Router();344345 router.use('/blog', function (req, res, next) {346 assert.equal(req.hit++, 0);347 assert.equal(req.url, 'http://example.com/post/1');348 next();349 });350351 router.handle(request, {}, function (err) {352 if (err) return done(err);353 assert.equal(request.hit, 1);354 done();355 });356 });357358 it('should adjust FQDN req.url with multiple handlers', function (done) {359 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };360 var router = new Router();361362 router.use(function (req, res, next) {363 assert.equal(req.hit++, 0);364 assert.equal(req.url, 'http://example.com/blog/post/1');365 next();366 });367368 router.use('/blog', function (req, res, next) {369 assert.equal(req.hit++, 1);370 assert.equal(req.url, 'http://example.com/post/1');371 next();372 });373374 router.handle(request, {}, function (err) {375 if (err) return done(err);376 assert.equal(request.hit, 2);377 done();378 });379 });380381 it('should adjust FQDN req.url with multiple routed handlers', function (done) {382 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };383 var router = new Router();384385 router.use('/blog', function (req, res, next) {386 assert.equal(req.hit++, 0);387 assert.equal(req.url, 'http://example.com/post/1');388 next();389 });390391 router.use('/blog', function (req, res, next) {392 assert.equal(req.hit++, 1);393 assert.equal(req.url, 'http://example.com/post/1');394 next();395 });396397 router.use(function (req, res, next) {398 assert.equal(req.hit++, 2);399 assert.equal(req.url, 'http://example.com/blog/post/1');400 next();401 });402403 router.handle(request, {}, function (err) {404 if (err) return done(err);405 assert.equal(request.hit, 3);406 done();407 });408 });409 })410411 describe('.all', function() {412 it('should support using .all to capture all http verbs', function(done){413 var router = new Router();414415 var count = 0;416 router.all('/foo', function(){ count++; });417418 var url = '/foo?bar=baz';419420 methods.forEach(function testMethod(method) {421 router.handle({ url: url, method: method }, {}, function() {});422 });423424 assert.equal(count, methods.length);425 done();426 })427428 it('should be called for any URL when "*"', function (done) {429 var cb = after(4, done)430 var router = new Router()431432 function no () {433 throw new Error('should not be called')434 }435436 router.all('*', function (req, res) {437 res.end()438 })439440 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)441 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)442 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)443 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)444 })445 })446447 describe('.use', function() {448 it('should require middleware', function () {449 var router = new Router()450 assert.throws(function () { router.use('/') }, /requires a middleware function/)451 })452453 it('should reject string as middleware', function () {454 var router = new Router()455 assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)456 })457458 it('should reject number as middleware', function () {459 var router = new Router()460 assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)461 })462463 it('should reject null as middleware', function () {464 var router = new Router()465 assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)466 })467468 it('should reject Date as middleware', function () {469 var router = new Router()470 assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)471 })472473 it('should be called for any URL', function (done) {474 var cb = after(4, done)475 var router = new Router()476477 function no () {478 throw new Error('should not be called')479 }480481 router.use(function (req, res) {482 res.end()483 })484485 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)486 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)487 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)488 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)489 })490491 it('should accept array of middleware', function(done){492 var count = 0;493 var router = new Router();494495 function fn1(req, res, next){496 assert.equal(++count, 1);497 next();498 }499500 function fn2(req, res, next){501 assert.equal(++count, 2);502 next();503 }504505 router.use([fn1, fn2], function(req, res){506 assert.equal(++count, 3);507 done();508 });509510 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});511 })512 })513514 describe('.param', function() {515 it('should call param function when routing VERBS', function(done) {516 var router = new Router();517518 router.param('id', function(req, res, next, id) {519 assert.equal(id, '123');520 next();521 });522523 router.get('/foo/:id/bar', function(req, res, next) {524 assert.equal(req.params.id, '123');525 next();526 });527528 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);529 });530531 it('should call param function when routing middleware', function(done) {532 var router = new Router();533534 router.param('id', function(req, res, next, id) {535 assert.equal(id, '123');536 next();537 });538539 router.use('/foo/:id/bar', function(req, res, next) {540 assert.equal(req.params.id, '123');541 assert.equal(req.url, '/baz');542 next();543 });544545 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);546 });547548 it('should only call once per request', function(done) {549 var count = 0;550 var req = { url: '/foo/bob/bar', method: 'get' };551 var router = new Router();552 var sub = new Router();553554 sub.get('/bar', function(req, res, next) {555 next();556 });557558 router.param('user', function(req, res, next, user) {559 count++;560 req.user = user;561 next();562 });563564 router.use('/foo/:user/', new Router());565 router.use('/foo/:user/', sub);566567 router.handle(req, {}, function(err) {568 if (err) return done(err);569 assert.equal(count, 1);570 assert.equal(req.user, 'bob');571 done();572 });573 });574575 it('should call when values differ', function(done) {576 var count = 0;577 var req = { url: '/foo/bob/bar', method: 'get' };578 var router = new Router();579 var sub = new Router();580581 sub.get('/bar', function(req, res, next) {582 next();583 });584585 router.param('user', function(req, res, next, user) {586 count++;587 req.user = user;588 next();589 });590591 router.use('/foo/:user/', new Router());592 router.use('/:user/bob/', sub);593594 router.handle(req, {}, function(err) {595 if (err) return done(err);596 assert.equal(count, 2);597 assert.equal(req.user, 'foo');598 done();599 });600 });601 });602603 describe('parallel requests', function() {604 it('should not mix requests', function(done) {605 var req1 = { url: '/foo/50/bar', method: 'get' };606 var req2 = { url: '/foo/10/bar', method: 'get' };607 var router = new Router();608 var sub = new Router();609610 done = after(2, done);611612 sub.get('/bar', function(req, res, next) {613 next();614 });615616 router.param('ms', function(req, res, next, ms) {617 ms = parseInt(ms, 10);618 req.ms = ms;619 setTimeout(next, ms);620 });621622 router.use('/foo/:ms/', new Router());623 router.use('/foo/:ms/', sub);624625 router.handle(req1, {}, function(err) {626 assert.ifError(err);627 assert.equal(req1.ms, 50);628 assert.equal(req1.originalUrl, '/foo/50/bar');629 done();630 });631632 router.handle(req2, {}, function(err) {633 assert.ifError(err);634 assert.equal(req2.ms, 10);635 assert.equal(req2.originalUrl, '/foo/10/bar');636 done();637 });638 });639 });640})