Router.js
This file
Path
test/Router.js
570 lines · 15.4 KiB · JavaScript
Last changed by
Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
History of this fileTake it
The walk stopped after 200 commits: lines older than e29fa25 point at it.
Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 1–10why, 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 = new Router();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 11why, one step back11 assert(typeof router === 'function')Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 12why, one step back12Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
lines 13–15why, one step back13 assert(typeof router.get === 'function')14 assert(typeof router.handle === 'function')15 assert(typeof router.use === 'function')Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 16–26why, one step back16 });1718 it('should support .use of other routers', function(done){19 var router = new Router();20 var another = new Router();2122 another.get('/bar', function(req, res){23 res.end();24 });25 router.use('/foo', another);26Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 27why, one step back27 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }, function(){});Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 28–39why, one step back28 });2930 it('should support dynamic routes', function(done){31 var router = new Router();32 var another = new Router();3334 another.get('/:bar', function(req, res){35 req.params.bar.should.equal('route');36 res.end();37 });38 router.use('/:foo', another);39Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 40why, one step back40 router.handle({ url: '/test/route', method: 'GET' }, { end: done }, function(){});Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 41–42why, one step back41 });42Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee
lines 43–46why, one step back43 it('should handle blank URL', function(done){44 var router = new Router();4546 router.use(function (req, res) {Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 47why, one step back47 false.should.be.true()Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee
lines 48–52why, one step back48 });4950 router.handle({ url: '', method: 'GET' }, {}, done);51 });52Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 53–62why, one step back53 it('should handle missing URL', function (done) {54 var router = new Router()5556 router.use(function (req, res) {57 throw new Error('should not be called')58 })5960 router.handle({ method: 'GET' }, {}, done)61 })62Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 63–74why, one step back63 it('should not stack overflow with many registered routes', function(done){64 var handler = function(req, res){ res.end(new Error('wrong handler')) };65 var router = new Router();6667 for (var i = 0; i < 6000; i++) {68 router.get('/thing' + i, handler)69 }7071 router.get('/', function (req, res) {72 res.end();73 });74Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 75why, one step back75 router.handle({ url: '/', method: 'GET' }, { end: done }, function(){});Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 76–77why, one step back76 });77Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 78–91why, one step back78 describe('.handle', function(){79 it('should dispatch', function(done){80 var router = new Router();8182 router.route('/foo').get(function(req, res){83 res.send('foo');84 });8586 var res = {87 send: function(val) {88 val.should.equal('foo');89 done();90 }91 }Use router module for routingDouglas Christopher Wilsonbefore adoptioncec5780
line 92why, one step back92 router.handle({ url: '/foo', method: 'GET' }, res, function(){});Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 93–243why, one step back93 })94 })9596 describe('.multiple callbacks', function(){97 it('should throw if a callback is null', function(){98 assert.throws(function () {99 var router = new Router();100 router.route('/foo').all(null);101 })102 })103104 it('should throw if a callback is undefined', function(){105 assert.throws(function () {106 var router = new Router();107 router.route('/foo').all(undefined);108 })109 })110111 it('should throw if a callback is not a function', function(){112 assert.throws(function () {113 var router = new Router();114 router.route('/foo').all('not a function');115 })116 })117118 it('should not throw if all callbacks are functions', function(){119 var router = new Router();120 router.route('/foo').all(function(){}).all(function(){});121 })122 })123124 describe('error', function(){125 it('should skip non error middleware', function(done){126 var router = new Router();127128 router.get('/foo', function(req, res, next){129 next(new Error('foo'));130 });131132 router.get('/bar', function(req, res, next){133 next(new Error('bar'));134 });135136 router.use(function(req, res, next){137 assert(false);138 });139140 router.use(function(err, req, res, next){141 assert.equal(err.message, 'foo');142 done();143 });144145 router.handle({ url: '/foo', method: 'GET' }, {}, done);146 });147148 it('should handle throwing inside routes with params', function(done) {149 var router = new Router();150151 router.get('/foo/:id', function(req, res, next){152 throw new Error('foo');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/2', method: 'GET' }, {}, function() {});165 });166167 it('should handle throwing in handler after async param', function(done) {168 var router = new Router();169170 router.param('user', function(req, res, next, val){171 process.nextTick(function(){172 req.user = val;173 next();174 });175 });176177 router.use('/:user', function(req, res, next){178 throw new Error('oh no!');179 });180181 router.use(function(err, req, res, next){182 assert.equal(err.message, 'oh no!');183 done();184 });185186 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});187 });188189 it('should handle throwing inside error handlers', function(done) {190 var router = new Router();191192 router.use(function(req, res, next){193 throw new Error('boom!');194 });195196 router.use(function(err, req, res, next){197 throw new Error('oops');198 });199200 router.use(function(err, req, res, next){201 assert.equal(err.message, 'oops');202 done();203 });204205 router.handle({ url: '/', method: 'GET' }, {}, done);206 });207 })208209 describe('FQDN', function () {210 it('should not obscure FQDNs', function (done) {211 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };212 var router = new Router();213214 router.use(function (req, res, next) {215 assert.equal(req.hit++, 0);216 assert.equal(req.url, 'http://example.com/foo');217 next();218 });219220 router.handle(request, {}, function (err) {221 if (err) return done(err);222 assert.equal(request.hit, 1);223 done();224 });225 });226227 it('should ignore FQDN in search', function (done) {228 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };229 var router = new Router();230231 router.use('/proxy', function (req, res, next) {232 assert.equal(req.hit++, 0);233 assert.equal(req.url, '/?url=http://example.com/blog/post/1');234 next();235 });236237 router.handle(request, {}, function (err) {238 if (err) return done(err);239 assert.equal(request.hit, 1);240 done();241 });242 });243Fix handling of URLs containing :// in the pathAria Stewartbefore adoptioneabd456
lines 244–260why, one step back244 it('should ignore FQDN in path', function (done) {245 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };246 var router = new Router();247248 router.use('/proxy', function (req, res, next) {249 assert.equal(req.hit++, 0);250 assert.equal(req.url, '/http://example.com/blog/post/1');251 next();252 });253254 router.handle(request, {}, function (err) {255 if (err) return done(err);256 assert.equal(request.hit, 1);257 done();258 });259 });260Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 261–346why, one step back261 it('should adjust FQDN req.url', function (done) {262 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };263 var router = new Router();264265 router.use('/blog', function (req, res, next) {266 assert.equal(req.hit++, 0);267 assert.equal(req.url, 'http://example.com/post/1');268 next();269 });270271 router.handle(request, {}, function (err) {272 if (err) return done(err);273 assert.equal(request.hit, 1);274 done();275 });276 });277278 it('should adjust FQDN req.url with multiple handlers', function (done) {279 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };280 var router = new Router();281282 router.use(function (req, res, next) {283 assert.equal(req.hit++, 0);284 assert.equal(req.url, 'http://example.com/blog/post/1');285 next();286 });287288 router.use('/blog', function (req, res, next) {289 assert.equal(req.hit++, 1);290 assert.equal(req.url, 'http://example.com/post/1');291 next();292 });293294 router.handle(request, {}, function (err) {295 if (err) return done(err);296 assert.equal(request.hit, 2);297 done();298 });299 });300301 it('should adjust FQDN req.url with multiple routed handlers', function (done) {302 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };303 var router = new Router();304305 router.use('/blog', function (req, res, next) {306 assert.equal(req.hit++, 0);307 assert.equal(req.url, 'http://example.com/post/1');308 next();309 });310311 router.use('/blog', function (req, res, next) {312 assert.equal(req.hit++, 1);313 assert.equal(req.url, 'http://example.com/post/1');314 next();315 });316317 router.use(function (req, res, next) {318 assert.equal(req.hit++, 2);319 assert.equal(req.url, 'http://example.com/blog/post/1');320 next();321 });322323 router.handle(request, {}, function (err) {324 if (err) return done(err);325 assert.equal(request.hit, 3);326 done();327 });328 });329 })330331 describe('.all', function() {332 it('should support using .all to capture all http verbs', function(done){333 var router = new Router();334335 var count = 0;336 router.all('/foo', function(){ count++; });337338 var url = '/foo?bar=baz';339340 methods.forEach(function testMethod(method) {341 router.handle({ url: url, method: method }, {}, function() {});342 });343344 assert.equal(count, methods.length);345 done();346 })Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 347–364why, one step back347348 it('should be called for any URL when "*"', function (done) {349 var cb = after(4, done)350 var router = new Router()351352 function no () {353 throw new Error('should not be called')354 }355356 router.all('*', function (req, res) {357 res.end()358 })359360 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)361 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)362 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)363 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)364 })Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 365–366why, one step back365 })366tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
line 367why, one step back367 describe('.use', function() {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 368–370why, one step back368 it('should require middleware', function () {369 var router = new Router()370 assert.throws(function () { router.use('/') }, /argument handler is required/)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 371–372why, one step back371 })372Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 373–390why, one step back373 it('should reject string as middleware', function () {374 var router = new Router()375 assert.throws(function () { router.use('/', 'foo') }, /argument handler must be a function/)376 })377378 it('should reject number as middleware', function () {379 var router = new Router()380 assert.throws(function () { router.use('/', 42) }, /argument handler must be a function/)381 })382383 it('should reject null as middleware', function () {384 var router = new Router()385 assert.throws(function () { router.use('/', null) }, /argument handler must be a function/)386 })387388 it('should reject Date as middleware', function () {389 var router = new Router()390 assert.throws(function () { router.use('/', new Date()) }, /argument handler must be a function/)Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 391–408why, one step back391 })392393 it('should be called for any URL', function (done) {394 var cb = after(4, done)395 var router = new Router()396397 function no () {398 throw new Error('should not be called')399 }400401 router.use(function (req, res) {402 res.end()403 })404405 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)406 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)407 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)408 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
line 409why, one step back409 })Fix router.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoption7289171
lines 410–431why, one step back410411 it('should accept array of middleware', function(done){412 var count = 0;413 var router = new Router();414415 function fn1(req, res, next){416 assert.equal(++count, 1);417 next();418 }419420 function fn2(req, res, next){421 assert.equal(++count, 2);422 next();423 }424425 router.use([fn1, fn2], function(req, res){426 assert.equal(++count, 3);427 done();428 });429430 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});431 })tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 432–433why, one step back432 })433Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
line 434why, one step back434 describe('.param', function() {Remove app.param(fn) signatureDouglas Christopher Wilsonbefore adoption1e2951a
lines 435–444why, one step back435 it('should require function', function () {436 var router = new Router();437 assert.throws(router.param.bind(router, 'id'), /argument fn is required/);438 });439440 it('should reject non-function', function () {441 var router = new Router();442 assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);443 });444Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 445–570why, one step back445 it('should call param function when routing VERBS', function(done) {446 var router = new Router();447448 router.param('id', function(req, res, next, id) {449 assert.equal(id, '123');450 next();451 });452453 router.get('/foo/:id/bar', function(req, res, next) {454 assert.equal(req.params.id, '123');455 next();456 });457458 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);459 });460461 it('should call param function when routing middleware', function(done) {462 var router = new Router();463464 router.param('id', function(req, res, next, id) {465 assert.equal(id, '123');466 next();467 });468469 router.use('/foo/:id/bar', function(req, res, next) {470 assert.equal(req.params.id, '123');471 assert.equal(req.url, '/baz');472 next();473 });474475 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);476 });477478 it('should only call once per request', function(done) {479 var count = 0;480 var req = { url: '/foo/bob/bar', method: 'get' };481 var router = new Router();482 var sub = new Router();483484 sub.get('/bar', function(req, res, next) {485 next();486 });487488 router.param('user', function(req, res, next, user) {489 count++;490 req.user = user;491 next();492 });493494 router.use('/foo/:user/', new Router());495 router.use('/foo/:user/', sub);496497 router.handle(req, {}, function(err) {498 if (err) return done(err);499 assert.equal(count, 1);500 assert.equal(req.user, 'bob');501 done();502 });503 });504505 it('should call when values differ', function(done) {506 var count = 0;507 var req = { url: '/foo/bob/bar', method: 'get' };508 var router = new Router();509 var sub = new Router();510511 sub.get('/bar', function(req, res, next) {512 next();513 });514515 router.param('user', function(req, res, next, user) {516 count++;517 req.user = user;518 next();519 });520521 router.use('/foo/:user/', new Router());522 router.use('/:user/bob/', sub);523524 router.handle(req, {}, function(err) {525 if (err) return done(err);526 assert.equal(count, 2);527 assert.equal(req.user, 'foo');528 done();529 });530 });531 });532533 describe('parallel requests', function() {534 it('should not mix requests', function(done) {535 var req1 = { url: '/foo/50/bar', method: 'get' };536 var req2 = { url: '/foo/10/bar', method: 'get' };537 var router = new Router();538 var sub = new Router();539540 done = after(2, done);541542 sub.get('/bar', function(req, res, next) {543 next();544 });545546 router.param('ms', function(req, res, next, ms) {547 ms = parseInt(ms, 10);548 req.ms = ms;549 setTimeout(next, ms);550 });551552 router.use('/foo/:ms/', new Router());553 router.use('/foo/:ms/', sub);554555 router.handle(req1, {}, function(err) {556 assert.ifError(err);557 assert.equal(req1.ms, 50);558 assert.equal(req1.originalUrl, '/foo/50/bar');559 done();560 });561562 router.handle(req2, {}, function(err) {563 assert.ifError(err);564 assert.equal(req2.ms, 10);565 assert.equal(req2.originalUrl, '/foo/10/bar');566 done();567 });568 });569 });570})