Skip to content
Myr

Router.js

This file

Path

test/Router.js

463 lines · 12.3 KiB · JavaScript

Last changed by

Fix router.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoption7289171

History of this file

Take 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  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      });131132      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() {});155    });156157    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    });175  })176177  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  })281282  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  })299300  describe('.use', function() {301    it('should require arguments', function(){302      var router = new Router();303      router.use.bind(router).should.throw(/requires middleware function/)304    })305306    it('should not accept non-functions', function(){307      var router = new Router();308      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/)312    })313314    it('should accept array of middleware', function(done){315      var count = 0;316      var router = new Router();317318      function fn1(req, res, next){319        assert.equal(++count, 1);320        next();321      }322323      function fn2(req, res, next){324        assert.equal(++count, 2);325        next();326      }327328      router.use([fn1, fn2], function(req, res){329        assert.equal(++count, 3);330        done();331      });332333      router.handle({ url: '/foo', method: 'GET' }, {}, function(){});334    })335  })336337  describe('.param', function() {338    it('should call param function when routing VERBS', function(done) {339      var router = new Router();340341      router.param('id', function(req, res, next, id) {342        assert.equal(id, '123');343        next();344      });345346      router.get('/foo/:id/bar', function(req, res, next) {347        assert.equal(req.params.id, '123');348        next();349      });350351      router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);352    });353354    it('should call param function when routing middleware', function(done) {355      var router = new Router();356357      router.param('id', function(req, res, next, id) {358        assert.equal(id, '123');359        next();360      });361362      router.use('/foo/:id/bar', function(req, res, next) {363        assert.equal(req.params.id, '123');364        assert.equal(req.url, '/baz');365        next();366      });367368      router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);369    });370371    it('should only call once per request', function(done) {372      var count = 0;373      var req = { url: '/foo/bob/bar', method: 'get' };374      var router = new Router();375      var sub = new Router();376377      sub.get('/bar', function(req, res, next) {378        next();379      });380381      router.param('user', function(req, res, next, user) {382        count++;383        req.user = user;384        next();385      });386387      router.use('/foo/:user/', new Router());388      router.use('/foo/:user/', sub);389390      router.handle(req, {}, function(err) {391        if (err) return done(err);392        assert.equal(count, 1);393        assert.equal(req.user, 'bob');394        done();395      });396    });397398    it('should call when values differ', function(done) {399      var count = 0;400      var req = { url: '/foo/bob/bar', method: 'get' };401      var router = new Router();402      var sub = new Router();403404      sub.get('/bar', function(req, res, next) {405        next();406      });407408      router.param('user', function(req, res, next, user) {409        count++;410        req.user = user;411        next();412      });413414      router.use('/foo/:user/', new Router());415      router.use('/:user/bob/', sub);416417      router.handle(req, {}, function(err) {418        if (err) return done(err);419        assert.equal(count, 2);420        assert.equal(req.user, 'foo');421        done();422      });423    });424  });425426  describe('parallel requests', function() {427    it('should not mix requests', function(done) {428      var req1 = { url: '/foo/50/bar', method: 'get' };429      var req2 = { url: '/foo/10/bar', method: 'get' };430      var router = new Router();431      var sub = new Router();432433      done = after(2, done);434435      sub.get('/bar', function(req, res, next) {436        next();437      });438439      router.param('ms', function(req, res, next, ms) {440        ms = parseInt(ms, 10);441        req.ms = ms;442        setTimeout(next, ms);443      });444445      router.use('/foo/:ms/', new Router());446      router.use('/foo/:ms/', sub);447448      router.handle(req1, {}, function(err) {449        assert.ifError(err);450        assert.equal(req1.ms, 50);451        assert.equal(req1.originalUrl, '/foo/50/bar');452        done();453      });454455      router.handle(req2, {}, function(err) {456        assert.ifError(err);457        assert.equal(req2.ms, 10);458        assert.equal(req2.originalUrl, '/foo/10/bar');459        done();460      });461    });462  });463})