Skip to content
Myr

Router.js

This file

Path

test/Router.js

551 lines · 14.7 KiB · JavaScript

Last changed by

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

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  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    })350351    it('should be called for any URL when "*"', function (done) {352      var cb = after(4, done)353      var router = new Router()354355      function no () {356        throw new Error('should not be called')357      }358359      router.all('*', function (req, res) {360        res.end()361      })362363      router.handle({ url: '/', method: 'GET' }, { end: cb }, no)364      router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)365      router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)366      router.handle({ url: '*', method: 'GET' }, { end: cb }, no)367    })368  })369370  describe('.use', function() {371    it('should require arguments', function(){372      var router = new Router();373      router.use.bind(router).should.throw(/requires middleware function/)374    })375376    it('should not accept non-functions', function(){377      var router = new Router();378      router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)379      router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)380      router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)381      router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)382    })383384    it('should be called for any URL', function (done) {385      var cb = after(4, done)386      var router = new Router()387388      function no () {389        throw new Error('should not be called')390      }391392      router.use(function (req, res) {393        res.end()394      })395396      router.handle({ url: '/', method: 'GET' }, { end: cb }, no)397      router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)398      router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)399      router.handle({ url: '*', method: 'GET' }, { end: cb }, no)400    })401402    it('should accept array of middleware', function(done){403      var count = 0;404      var router = new Router();405406      function fn1(req, res, next){407        assert.equal(++count, 1);408        next();409      }410411      function fn2(req, res, next){412        assert.equal(++count, 2);413        next();414      }415416      router.use([fn1, fn2], function(req, res){417        assert.equal(++count, 3);418        done();419      });420421      router.handle({ url: '/foo', method: 'GET' }, {}, function(){});422    })423  })424425  describe('.param', function() {426    it('should call param function when routing VERBS', function(done) {427      var router = new Router();428429      router.param('id', function(req, res, next, id) {430        assert.equal(id, '123');431        next();432      });433434      router.get('/foo/:id/bar', function(req, res, next) {435        assert.equal(req.params.id, '123');436        next();437      });438439      router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);440    });441442    it('should call param function when routing middleware', function(done) {443      var router = new Router();444445      router.param('id', function(req, res, next, id) {446        assert.equal(id, '123');447        next();448      });449450      router.use('/foo/:id/bar', function(req, res, next) {451        assert.equal(req.params.id, '123');452        assert.equal(req.url, '/baz');453        next();454      });455456      router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);457    });458459    it('should only call once per request', function(done) {460      var count = 0;461      var req = { url: '/foo/bob/bar', method: 'get' };462      var router = new Router();463      var sub = new Router();464465      sub.get('/bar', function(req, res, next) {466        next();467      });468469      router.param('user', function(req, res, next, user) {470        count++;471        req.user = user;472        next();473      });474475      router.use('/foo/:user/', new Router());476      router.use('/foo/:user/', sub);477478      router.handle(req, {}, function(err) {479        if (err) return done(err);480        assert.equal(count, 1);481        assert.equal(req.user, 'bob');482        done();483      });484    });485486    it('should call when values differ', function(done) {487      var count = 0;488      var req = { url: '/foo/bob/bar', method: 'get' };489      var router = new Router();490      var sub = new Router();491492      sub.get('/bar', function(req, res, next) {493        next();494      });495496      router.param('user', function(req, res, next, user) {497        count++;498        req.user = user;499        next();500      });501502      router.use('/foo/:user/', new Router());503      router.use('/:user/bob/', sub);504505      router.handle(req, {}, function(err) {506        if (err) return done(err);507        assert.equal(count, 2);508        assert.equal(req.user, 'foo');509        done();510      });511    });512  });513514  describe('parallel requests', function() {515    it('should not mix requests', function(done) {516      var req1 = { url: '/foo/50/bar', method: 'get' };517      var req2 = { url: '/foo/10/bar', method: 'get' };518      var router = new Router();519      var sub = new Router();520521      done = after(2, done);522523      sub.get('/bar', function(req, res, next) {524        next();525      });526527      router.param('ms', function(req, res, next, ms) {528        ms = parseInt(ms, 10);529        req.ms = ms;530        setTimeout(next, ms);531      });532533      router.use('/foo/:ms/', new Router());534      router.use('/foo/:ms/', sub);535536      router.handle(req1, {}, function(err) {537        assert.ifError(err);538        assert.equal(req1.ms, 50);539        assert.equal(req1.originalUrl, '/foo/50/bar');540        done();541      });542543      router.handle(req2, {}, function(err) {544        assert.ifError(err);545        assert.equal(req2.ms, 10);546        assert.equal(req2.originalUrl, '/foo/10/bar');547        done();548      });549    });550  });551})