Router.js
This file
Path
test/Router.js
579 lines · 15.5 KiB · JavaScript
Last changed by
tests: mark stack overflow as long runningphoenixbefore adoptionfd8e45c
History of this fileTake it
The walk stopped after 200 commits: lines older than b02d3a1 point at it.
tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137
line 1why, one step back1'use strict'docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 2–35why, one step back23var 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){tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7
line 36why, one step back36 assert.strictEqual(req.params.bar, 'route')docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 37–47why, one step back37 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) {tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7
line 48why, one step back48 throw new Error('should not be called')docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 49–64why, one step back49 });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('should not stack overflow with many registered routes', function(done){tests: mark stack overflow as long runningphoenixbefore adoptionfd8e45c
lines 65–66why, one step back65 this.timeout(5000) // long-running test66docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 67–80why, one step back67 var handler = function(req, res){ res.end(new Error('wrong handler')) };68 var router = new Router();6970 for (var i = 0; i < 6000; i++) {71 router.get('/thing' + i, handler)72 }7374 router.get('/', function (req, res) {75 res.end();76 });7778 router.handle({ url: '/', method: 'GET' }, { end: done });79 });80Fix handling very large stacks of sync middlewareDouglas Christopher Wilsonbefore adoption708ac4c
lines 81–96why, one step back81 it('should not stack overflow with a large sync stack', function (done) {82 this.timeout(5000) // long-running test8384 var router = new Router()8586 for (var i = 0; i < 6000; i++) {87 router.use(function (req, res, next) { next() })88 }8990 router.use(function (req, res) {91 res.end()92 })9394 router.handle({ url: '/', method: 'GET' }, { end: done })95 })96docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 97–106why, one step back97 describe('.handle', function(){98 it('should dispatch', function(done){99 var router = new Router();100101 router.route('/foo').get(function(req, res){102 res.send('foo');103 });104105 var res = {106 send: function(val) {tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7
line 107why, one step back107 assert.strictEqual(val, 'foo')docs: add link to contributing guideJames Georgebefore adoptionb02d3a1
lines 108–579why, one step back108 done();109 }110 }111 router.handle({ url: '/foo', method: 'GET' }, res);112 })113 })114115 describe('.multiple callbacks', function(){116 it('should throw if a callback is null', function(){117 assert.throws(function () {118 var router = new Router();119 router.route('/foo').all(null);120 })121 })122123 it('should throw if a callback is undefined', function(){124 assert.throws(function () {125 var router = new Router();126 router.route('/foo').all(undefined);127 })128 })129130 it('should throw if a callback is not a function', function(){131 assert.throws(function () {132 var router = new Router();133 router.route('/foo').all('not a function');134 })135 })136137 it('should not throw if all callbacks are functions', function(){138 var router = new Router();139 router.route('/foo').all(function(){}).all(function(){});140 })141 })142143 describe('error', function(){144 it('should skip non error middleware', function(done){145 var router = new Router();146147 router.get('/foo', function(req, res, next){148 next(new Error('foo'));149 });150151 router.get('/bar', function(req, res, next){152 next(new Error('bar'));153 });154155 router.use(function(req, res, next){156 assert(false);157 });158159 router.use(function(err, req, res, next){160 assert.equal(err.message, 'foo');161 done();162 });163164 router.handle({ url: '/foo', method: 'GET' }, {}, done);165 });166167 it('should handle throwing inside routes with params', function(done) {168 var router = new Router();169170 router.get('/foo/:id', function(req, res, next){171 throw new Error('foo');172 });173174 router.use(function(req, res, next){175 assert(false);176 });177178 router.use(function(err, req, res, next){179 assert.equal(err.message, 'foo');180 done();181 });182183 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});184 });185186 it('should handle throwing in handler after async param', function(done) {187 var router = new Router();188189 router.param('user', function(req, res, next, val){190 process.nextTick(function(){191 req.user = val;192 next();193 });194 });195196 router.use('/:user', function(req, res, next){197 throw new Error('oh no!');198 });199200 router.use(function(err, req, res, next){201 assert.equal(err.message, 'oh no!');202 done();203 });204205 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});206 });207208 it('should handle throwing inside error handlers', function(done) {209 var router = new Router();210211 router.use(function(req, res, next){212 throw new Error('boom!');213 });214215 router.use(function(err, req, res, next){216 throw new Error('oops');217 });218219 router.use(function(err, req, res, next){220 assert.equal(err.message, 'oops');221 done();222 });223224 router.handle({ url: '/', method: 'GET' }, {}, done);225 });226 })227228 describe('FQDN', function () {229 it('should not obscure FQDNs', function (done) {230 var request = { hit: 0, url: 'http://example.com/foo', 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/foo');236 next();237 });238239 router.handle(request, {}, function (err) {240 if (err) return done(err);241 assert.equal(request.hit, 1);242 done();243 });244 });245246 it('should ignore FQDN in search', function (done) {247 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };248 var router = new Router();249250 router.use('/proxy', function (req, res, next) {251 assert.equal(req.hit++, 0);252 assert.equal(req.url, '/?url=http://example.com/blog/post/1');253 next();254 });255256 router.handle(request, {}, function (err) {257 if (err) return done(err);258 assert.equal(request.hit, 1);259 done();260 });261 });262263 it('should ignore FQDN in path', function (done) {264 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };265 var router = new Router();266267 router.use('/proxy', function (req, res, next) {268 assert.equal(req.hit++, 0);269 assert.equal(req.url, '/http://example.com/blog/post/1');270 next();271 });272273 router.handle(request, {}, function (err) {274 if (err) return done(err);275 assert.equal(request.hit, 1);276 done();277 });278 });279280 it('should adjust FQDN req.url', function (done) {281 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };282 var router = new Router();283284 router.use('/blog', function (req, res, next) {285 assert.equal(req.hit++, 0);286 assert.equal(req.url, 'http://example.com/post/1');287 next();288 });289290 router.handle(request, {}, function (err) {291 if (err) return done(err);292 assert.equal(request.hit, 1);293 done();294 });295 });296297 it('should adjust FQDN req.url with multiple handlers', function (done) {298 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };299 var router = new Router();300301 router.use(function (req, res, next) {302 assert.equal(req.hit++, 0);303 assert.equal(req.url, 'http://example.com/blog/post/1');304 next();305 });306307 router.use('/blog', function (req, res, next) {308 assert.equal(req.hit++, 1);309 assert.equal(req.url, 'http://example.com/post/1');310 next();311 });312313 router.handle(request, {}, function (err) {314 if (err) return done(err);315 assert.equal(request.hit, 2);316 done();317 });318 });319320 it('should adjust FQDN req.url with multiple routed handlers', function (done) {321 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };322 var router = new Router();323324 router.use('/blog', function (req, res, next) {325 assert.equal(req.hit++, 0);326 assert.equal(req.url, 'http://example.com/post/1');327 next();328 });329330 router.use('/blog', function (req, res, next) {331 assert.equal(req.hit++, 1);332 assert.equal(req.url, 'http://example.com/post/1');333 next();334 });335336 router.use(function (req, res, next) {337 assert.equal(req.hit++, 2);338 assert.equal(req.url, 'http://example.com/blog/post/1');339 next();340 });341342 router.handle(request, {}, function (err) {343 if (err) return done(err);344 assert.equal(request.hit, 3);345 done();346 });347 });348 })349350 describe('.all', function() {351 it('should support using .all to capture all http verbs', function(done){352 var router = new Router();353354 var count = 0;355 router.all('/foo', function(){ count++; });356357 var url = '/foo?bar=baz';358359 methods.forEach(function testMethod(method) {360 router.handle({ url: url, method: method }, {}, function() {});361 });362363 assert.equal(count, methods.length);364 done();365 })366367 it('should be called for any URL when "*"', function (done) {368 var cb = after(4, done)369 var router = new Router()370371 function no () {372 throw new Error('should not be called')373 }374375 router.all('*', function (req, res) {376 res.end()377 })378379 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)380 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)381 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)382 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)383 })384 })385386 describe('.use', function() {387 it('should require middleware', function () {388 var router = new Router()389 assert.throws(function () { router.use('/') }, /requires a middleware function/)390 })391392 it('should reject string as middleware', function () {393 var router = new Router()394 assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)395 })396397 it('should reject number as middleware', function () {398 var router = new Router()399 assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)400 })401402 it('should reject null as middleware', function () {403 var router = new Router()404 assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)405 })406407 it('should reject Date as middleware', function () {408 var router = new Router()409 assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)410 })411412 it('should be called for any URL', function (done) {413 var cb = after(4, done)414 var router = new Router()415416 function no () {417 throw new Error('should not be called')418 }419420 router.use(function (req, res) {421 res.end()422 })423424 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)425 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)426 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)427 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)428 })429430 it('should accept array of middleware', function(done){431 var count = 0;432 var router = new Router();433434 function fn1(req, res, next){435 assert.equal(++count, 1);436 next();437 }438439 function fn2(req, res, next){440 assert.equal(++count, 2);441 next();442 }443444 router.use([fn1, fn2], function(req, res){445 assert.equal(++count, 3);446 done();447 });448449 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});450 })451 })452453 describe('.param', function() {454 it('should call param function when routing VERBS', function(done) {455 var router = new Router();456457 router.param('id', function(req, res, next, id) {458 assert.equal(id, '123');459 next();460 });461462 router.get('/foo/:id/bar', function(req, res, next) {463 assert.equal(req.params.id, '123');464 next();465 });466467 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);468 });469470 it('should call param function when routing middleware', function(done) {471 var router = new Router();472473 router.param('id', function(req, res, next, id) {474 assert.equal(id, '123');475 next();476 });477478 router.use('/foo/:id/bar', function(req, res, next) {479 assert.equal(req.params.id, '123');480 assert.equal(req.url, '/baz');481 next();482 });483484 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);485 });486487 it('should only call once per request', function(done) {488 var count = 0;489 var req = { url: '/foo/bob/bar', method: 'get' };490 var router = new Router();491 var sub = new Router();492493 sub.get('/bar', function(req, res, next) {494 next();495 });496497 router.param('user', function(req, res, next, user) {498 count++;499 req.user = user;500 next();501 });502503 router.use('/foo/:user/', new Router());504 router.use('/foo/:user/', sub);505506 router.handle(req, {}, function(err) {507 if (err) return done(err);508 assert.equal(count, 1);509 assert.equal(req.user, 'bob');510 done();511 });512 });513514 it('should call when values differ', function(done) {515 var count = 0;516 var req = { url: '/foo/bob/bar', method: 'get' };517 var router = new Router();518 var sub = new Router();519520 sub.get('/bar', function(req, res, next) {521 next();522 });523524 router.param('user', function(req, res, next, user) {525 count++;526 req.user = user;527 next();528 });529530 router.use('/foo/:user/', new Router());531 router.use('/:user/bob/', sub);532533 router.handle(req, {}, function(err) {534 if (err) return done(err);535 assert.equal(count, 2);536 assert.equal(req.user, 'foo');537 done();538 });539 });540 });541542 describe('parallel requests', function() {543 it('should not mix requests', function(done) {544 var req1 = { url: '/foo/50/bar', method: 'get' };545 var req2 = { url: '/foo/10/bar', method: 'get' };546 var router = new Router();547 var sub = new Router();548549 done = after(2, done);550551 sub.get('/bar', function(req, res, next) {552 next();553 });554555 router.param('ms', function(req, res, next, ms) {556 ms = parseInt(ms, 10);557 req.ms = ms;558 setTimeout(next, ms);559 });560561 router.use('/foo/:ms/', new Router());562 router.use('/foo/:ms/', sub);563564 router.handle(req1, {}, function(err) {565 assert.ifError(err);566 assert.equal(req1.ms, 50);567 assert.equal(req1.originalUrl, '/foo/50/bar');568 done();569 });570571 router.handle(req2, {}, function(err) {572 assert.ifError(err);573 assert.equal(req2.ms, 10);574 assert.equal(req2.originalUrl, '/foo/10/bar');575 done();576 });577 });578 });579})