Router.js
This file
Path
test/Router.js
515 lines · 13.7 KiB · JavaScript
Last changed by
Skip routing when req.url is not setDouglas Christopher Wilsonbefore adoption668f545
History of this fileTake it
12var 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 it('should handle blank URL', function(done){47 var router = new Router();4849 router.use(function (req, res) {50 false.should.be.true;51 });5253 router.handle({ url: '', method: 'GET' }, {}, done);54 });5556 it('should handle missing URL', function (done) {57 var router = new Router()5859 router.use(function (req, res) {60 throw new Error('should not be called')61 })6263 router.handle({ method: 'GET' }, {}, done)64 })6566 it('should not stack overflow with many registered routes', function(done){67 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 });8081 describe('.handle', function(){82 it('should dispatch', function(done){83 var router = new Router();8485 router.route('/foo').get(function(req, res){86 res.send('foo');87 });8889 var res = {90 send: function(val) {91 val.should.equal('foo');92 done();93 }94 }95 router.handle({ url: '/foo', method: 'GET' }, res);96 })97 })9899 describe('.multiple callbacks', function(){100 it('should throw if a callback is null', function(){101 assert.throws(function () {102 var router = new Router();103 router.route('/foo').all(null);104 })105 })106107 it('should throw if a callback is undefined', function(){108 assert.throws(function () {109 var router = new Router();110 router.route('/foo').all(undefined);111 })112 })113114 it('should throw if a callback is not a function', function(){115 assert.throws(function () {116 var router = new Router();117 router.route('/foo').all('not a function');118 })119 })120121 it('should not throw if all callbacks are functions', function(){122 var router = new Router();123 router.route('/foo').all(function(){}).all(function(){});124 })125 })126127 describe('error', function(){128 it('should skip non error middleware', function(done){129 var router = new Router();130131 router.get('/foo', function(req, res, next){132 next(new Error('foo'));133 });134135 router.get('/bar', function(req, res, next){136 next(new Error('bar'));137 });138139 router.use(function(req, res, next){140 assert(false);141 });142143 router.use(function(err, req, res, next){144 assert.equal(err.message, 'foo');145 done();146 });147148 router.handle({ url: '/foo', method: 'GET' }, {}, done);149 });150151 it('should handle throwing inside routes with params', function(done) {152 var router = new Router();153154 router.get('/foo/:id', function(req, res, next){155 throw new Error('foo');156 });157158 router.use(function(req, res, next){159 assert(false);160 });161162 router.use(function(err, req, res, next){163 assert.equal(err.message, 'foo');164 done();165 });166167 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});168 });169170 it('should handle throwing in handler after async param', function(done) {171 var router = new Router();172173 router.param('user', function(req, res, next, val){174 process.nextTick(function(){175 req.user = val;176 next();177 });178 });179180 router.use('/:user', function(req, res, next){181 throw new Error('oh no!');182 });183184 router.use(function(err, req, res, next){185 assert.equal(err.message, 'oh no!');186 done();187 });188189 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});190 });191192 it('should handle throwing inside error handlers', function(done) {193 var router = new Router();194195 router.use(function(req, res, next){196 throw new Error('boom!');197 });198199 router.use(function(err, req, res, next){200 throw new Error('oops');201 });202203 router.use(function(err, req, res, next){204 assert.equal(err.message, 'oops');205 done();206 });207208 router.handle({ url: '/', method: 'GET' }, {}, done);209 });210 })211212 describe('FQDN', function () {213 it('should not obscure FQDNs', function (done) {214 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };215 var router = new Router();216217 router.use(function (req, res, next) {218 assert.equal(req.hit++, 0);219 assert.equal(req.url, 'http://example.com/foo');220 next();221 });222223 router.handle(request, {}, function (err) {224 if (err) return done(err);225 assert.equal(request.hit, 1);226 done();227 });228 });229230 it('should ignore FQDN in search', function (done) {231 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };232 var router = new Router();233234 router.use('/proxy', function (req, res, next) {235 assert.equal(req.hit++, 0);236 assert.equal(req.url, '/?url=http://example.com/blog/post/1');237 next();238 });239240 router.handle(request, {}, function (err) {241 if (err) return done(err);242 assert.equal(request.hit, 1);243 done();244 });245 });246247 it('should ignore FQDN in path', function (done) {248 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };249 var router = new Router();250251 router.use('/proxy', function (req, res, next) {252 assert.equal(req.hit++, 0);253 assert.equal(req.url, '/http://example.com/blog/post/1');254 next();255 });256257 router.handle(request, {}, function (err) {258 if (err) return done(err);259 assert.equal(request.hit, 1);260 done();261 });262 });263264 it('should adjust FQDN req.url', function (done) {265 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };266 var router = new Router();267268 router.use('/blog', function (req, res, next) {269 assert.equal(req.hit++, 0);270 assert.equal(req.url, 'http://example.com/post/1');271 next();272 });273274 router.handle(request, {}, function (err) {275 if (err) return done(err);276 assert.equal(request.hit, 1);277 done();278 });279 });280281 it('should adjust FQDN req.url with multiple handlers', function (done) {282 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };283 var router = new Router();284285 router.use(function (req, res, next) {286 assert.equal(req.hit++, 0);287 assert.equal(req.url, 'http://example.com/blog/post/1');288 next();289 });290291 router.use('/blog', function (req, res, next) {292 assert.equal(req.hit++, 1);293 assert.equal(req.url, 'http://example.com/post/1');294 next();295 });296297 router.handle(request, {}, function (err) {298 if (err) return done(err);299 assert.equal(request.hit, 2);300 done();301 });302 });303304 it('should adjust FQDN req.url with multiple routed handlers', function (done) {305 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };306 var router = new Router();307308 router.use('/blog', function (req, res, next) {309 assert.equal(req.hit++, 0);310 assert.equal(req.url, 'http://example.com/post/1');311 next();312 });313314 router.use('/blog', function (req, res, next) {315 assert.equal(req.hit++, 1);316 assert.equal(req.url, 'http://example.com/post/1');317 next();318 });319320 router.use(function (req, res, next) {321 assert.equal(req.hit++, 2);322 assert.equal(req.url, 'http://example.com/blog/post/1');323 next();324 });325326 router.handle(request, {}, function (err) {327 if (err) return done(err);328 assert.equal(request.hit, 3);329 done();330 });331 });332 })333334 describe('.all', function() {335 it('should support using .all to capture all http verbs', function(done){336 var router = new Router();337338 var count = 0;339 router.all('/foo', function(){ count++; });340341 var url = '/foo?bar=baz';342343 methods.forEach(function testMethod(method) {344 router.handle({ url: url, method: method }, {}, function() {});345 });346347 assert.equal(count, methods.length);348 done();349 })350 })351352 describe('.use', function() {353 it('should require arguments', function(){354 var router = new Router();355 router.use.bind(router).should.throw(/requires middleware function/)356 })357358 it('should not accept non-functions', function(){359 var router = new Router();360 router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)361 router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)362 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)363 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)364 })365366 it('should accept array of middleware', function(done){367 var count = 0;368 var router = new Router();369370 function fn1(req, res, next){371 assert.equal(++count, 1);372 next();373 }374375 function fn2(req, res, next){376 assert.equal(++count, 2);377 next();378 }379380 router.use([fn1, fn2], function(req, res){381 assert.equal(++count, 3);382 done();383 });384385 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});386 })387 })388389 describe('.param', function() {390 it('should call param function when routing VERBS', function(done) {391 var router = new Router();392393 router.param('id', function(req, res, next, id) {394 assert.equal(id, '123');395 next();396 });397398 router.get('/foo/:id/bar', function(req, res, next) {399 assert.equal(req.params.id, '123');400 next();401 });402403 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);404 });405406 it('should call param function when routing middleware', function(done) {407 var router = new Router();408409 router.param('id', function(req, res, next, id) {410 assert.equal(id, '123');411 next();412 });413414 router.use('/foo/:id/bar', function(req, res, next) {415 assert.equal(req.params.id, '123');416 assert.equal(req.url, '/baz');417 next();418 });419420 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);421 });422423 it('should only call once per request', function(done) {424 var count = 0;425 var req = { url: '/foo/bob/bar', method: 'get' };426 var router = new Router();427 var sub = new Router();428429 sub.get('/bar', function(req, res, next) {430 next();431 });432433 router.param('user', function(req, res, next, user) {434 count++;435 req.user = user;436 next();437 });438439 router.use('/foo/:user/', new Router());440 router.use('/foo/:user/', sub);441442 router.handle(req, {}, function(err) {443 if (err) return done(err);444 assert.equal(count, 1);445 assert.equal(req.user, 'bob');446 done();447 });448 });449450 it('should call when values differ', function(done) {451 var count = 0;452 var req = { url: '/foo/bob/bar', method: 'get' };453 var router = new Router();454 var sub = new Router();455456 sub.get('/bar', function(req, res, next) {457 next();458 });459460 router.param('user', function(req, res, next, user) {461 count++;462 req.user = user;463 next();464 });465466 router.use('/foo/:user/', new Router());467 router.use('/:user/bob/', sub);468469 router.handle(req, {}, function(err) {470 if (err) return done(err);471 assert.equal(count, 2);472 assert.equal(req.user, 'foo');473 done();474 });475 });476 });477478 describe('parallel requests', function() {479 it('should not mix requests', function(done) {480 var req1 = { url: '/foo/50/bar', method: 'get' };481 var req2 = { url: '/foo/10/bar', method: 'get' };482 var router = new Router();483 var sub = new Router();484485 done = after(2, done);486487 sub.get('/bar', function(req, res, next) {488 next();489 });490491 router.param('ms', function(req, res, next, ms) {492 ms = parseInt(ms, 10);493 req.ms = ms;494 setTimeout(next, ms);495 });496497 router.use('/foo/:ms/', new Router());498 router.use('/foo/:ms/', sub);499500 router.handle(req1, {}, function(err) {501 assert.ifError(err);502 assert.equal(req1.ms, 50);503 assert.equal(req1.originalUrl, '/foo/50/bar');504 done();505 });506507 router.handle(req2, {}, function(err) {508 assert.ifError(err);509 assert.equal(req2.ms, 10);510 assert.equal(req2.originalUrl, '/foo/10/bar');511 done();512 });513 });514 });515})