Skip to content
Myr

Router.js

This file

Path

test/Router.js

561 lines · 15.1 KiB · JavaScript

Last changed by

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

History of this file

Take it

1'use strict'23var 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){36      assert.strictEqual(req.params.bar, 'route')37      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) {48      throw new Error('should not be called')49    });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){65    var handler = function(req, res){ res.end(new Error('wrong handler')) };66    var router = new Router();6768    for (var i = 0; i < 6000; i++) {69      router.get('/thing' + i, handler)70    }7172    router.get('/', function (req, res) {73      res.end();74    });7576    router.handle({ url: '/', method: 'GET' }, { end: done });77  });7879  describe('.handle', function(){80    it('should dispatch', function(done){81      var router = new Router();8283      router.route('/foo').get(function(req, res){84        res.send('foo');85      });8687      var res = {88        send: function(val) {89          assert.strictEqual(val, 'foo')90          done();91        }92      }93      router.handle({ url: '/foo', method: 'GET' }, res);94    })95  })9697  describe('.multiple callbacks', function(){98    it('should throw if a callback is null', function(){99      assert.throws(function () {100        var router = new Router();101        router.route('/foo').all(null);102      })103    })104105    it('should throw if a callback is undefined', function(){106      assert.throws(function () {107        var router = new Router();108        router.route('/foo').all(undefined);109      })110    })111112    it('should throw if a callback is not a function', function(){113      assert.throws(function () {114        var router = new Router();115        router.route('/foo').all('not a function');116      })117    })118119    it('should not throw if all callbacks are functions', function(){120      var router = new Router();121      router.route('/foo').all(function(){}).all(function(){});122    })123  })124125  describe('error', function(){126    it('should skip non error middleware', function(done){127      var router = new Router();128129      router.get('/foo', function(req, res, next){130        next(new Error('foo'));131      });132133      router.get('/bar', function(req, res, next){134        next(new Error('bar'));135      });136137      router.use(function(req, res, next){138        assert(false);139      });140141      router.use(function(err, req, res, next){142        assert.equal(err.message, 'foo');143        done();144      });145146      router.handle({ url: '/foo', method: 'GET' }, {}, done);147    });148149    it('should handle throwing inside routes with params', function(done) {150      var router = new Router();151152      router.get('/foo/:id', function(req, res, next){153        throw new Error('foo');154      });155156      router.use(function(req, res, next){157        assert(false);158      });159160      router.use(function(err, req, res, next){161        assert.equal(err.message, 'foo');162        done();163      });164165      router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});166    });167168    it('should handle throwing in handler after async param', function(done) {169      var router = new Router();170171      router.param('user', function(req, res, next, val){172        process.nextTick(function(){173          req.user = val;174          next();175        });176      });177178      router.use('/:user', function(req, res, next){179        throw new Error('oh no!');180      });181182      router.use(function(err, req, res, next){183        assert.equal(err.message, 'oh no!');184        done();185      });186187      router.handle({ url: '/bob', method: 'GET' }, {}, function() {});188    });189190    it('should handle throwing inside error handlers', function(done) {191      var router = new Router();192193      router.use(function(req, res, next){194        throw new Error('boom!');195      });196197      router.use(function(err, req, res, next){198        throw new Error('oops');199      });200201      router.use(function(err, req, res, next){202        assert.equal(err.message, 'oops');203        done();204      });205206      router.handle({ url: '/', method: 'GET' }, {}, done);207    });208  })209210  describe('FQDN', function () {211    it('should not obscure FQDNs', function (done) {212      var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };213      var router = new Router();214215      router.use(function (req, res, next) {216        assert.equal(req.hit++, 0);217        assert.equal(req.url, 'http://example.com/foo');218        next();219      });220221      router.handle(request, {}, function (err) {222        if (err) return done(err);223        assert.equal(request.hit, 1);224        done();225      });226    });227228    it('should ignore FQDN in search', function (done) {229      var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };230      var router = new Router();231232      router.use('/proxy', function (req, res, next) {233        assert.equal(req.hit++, 0);234        assert.equal(req.url, '/?url=http://example.com/blog/post/1');235        next();236      });237238      router.handle(request, {}, function (err) {239        if (err) return done(err);240        assert.equal(request.hit, 1);241        done();242      });243    });244245    it('should ignore FQDN in path', function (done) {246      var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };247      var router = new Router();248249      router.use('/proxy', function (req, res, next) {250        assert.equal(req.hit++, 0);251        assert.equal(req.url, '/http://example.com/blog/post/1');252        next();253      });254255      router.handle(request, {}, function (err) {256        if (err) return done(err);257        assert.equal(request.hit, 1);258        done();259      });260    });261262    it('should adjust FQDN req.url', function (done) {263      var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };264      var router = new Router();265266      router.use('/blog', function (req, res, next) {267        assert.equal(req.hit++, 0);268        assert.equal(req.url, 'http://example.com/post/1');269        next();270      });271272      router.handle(request, {}, function (err) {273        if (err) return done(err);274        assert.equal(request.hit, 1);275        done();276      });277    });278279    it('should adjust FQDN req.url with multiple handlers', function (done) {280      var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };281      var router = new Router();282283      router.use(function (req, res, next) {284        assert.equal(req.hit++, 0);285        assert.equal(req.url, 'http://example.com/blog/post/1');286        next();287      });288289      router.use('/blog', function (req, res, next) {290        assert.equal(req.hit++, 1);291        assert.equal(req.url, 'http://example.com/post/1');292        next();293      });294295      router.handle(request, {}, function (err) {296        if (err) return done(err);297        assert.equal(request.hit, 2);298        done();299      });300    });301302    it('should adjust FQDN req.url with multiple routed handlers', function (done) {303      var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };304      var router = new Router();305306      router.use('/blog', function (req, res, next) {307        assert.equal(req.hit++, 0);308        assert.equal(req.url, 'http://example.com/post/1');309        next();310      });311312      router.use('/blog', function (req, res, next) {313        assert.equal(req.hit++, 1);314        assert.equal(req.url, 'http://example.com/post/1');315        next();316      });317318      router.use(function (req, res, next) {319        assert.equal(req.hit++, 2);320        assert.equal(req.url, 'http://example.com/blog/post/1');321        next();322      });323324      router.handle(request, {}, function (err) {325        if (err) return done(err);326        assert.equal(request.hit, 3);327        done();328      });329    });330  })331332  describe('.all', function() {333    it('should support using .all to capture all http verbs', function(done){334      var router = new Router();335336      var count = 0;337      router.all('/foo', function(){ count++; });338339      var url = '/foo?bar=baz';340341      methods.forEach(function testMethod(method) {342        router.handle({ url: url, method: method }, {}, function() {});343      });344345      assert.equal(count, methods.length);346      done();347    })348349    it('should be called for any URL when "*"', function (done) {350      var cb = after(4, done)351      var router = new Router()352353      function no () {354        throw new Error('should not be called')355      }356357      router.all('*', function (req, res) {358        res.end()359      })360361      router.handle({ url: '/', method: 'GET' }, { end: cb }, no)362      router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)363      router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)364      router.handle({ url: '*', method: 'GET' }, { end: cb }, no)365    })366  })367368  describe('.use', function() {369    it('should require middleware', function () {370      var router = new Router()371      assert.throws(function () { router.use('/') }, /requires a middleware function/)372    })373374    it('should reject string as middleware', function () {375      var router = new Router()376      assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)377    })378379    it('should reject number as middleware', function () {380      var router = new Router()381      assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)382    })383384    it('should reject null as middleware', function () {385      var router = new Router()386      assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)387    })388389    it('should reject Date as middleware', function () {390      var router = new Router()391      assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)392    })393394    it('should be called for any URL', function (done) {395      var cb = after(4, done)396      var router = new Router()397398      function no () {399        throw new Error('should not be called')400      }401402      router.use(function (req, res) {403        res.end()404      })405406      router.handle({ url: '/', method: 'GET' }, { end: cb }, no)407      router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)408      router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)409      router.handle({ url: '*', method: 'GET' }, { end: cb }, no)410    })411412    it('should accept array of middleware', function(done){413      var count = 0;414      var router = new Router();415416      function fn1(req, res, next){417        assert.equal(++count, 1);418        next();419      }420421      function fn2(req, res, next){422        assert.equal(++count, 2);423        next();424      }425426      router.use([fn1, fn2], function(req, res){427        assert.equal(++count, 3);428        done();429      });430431      router.handle({ url: '/foo', method: 'GET' }, {}, function(){});432    })433  })434435  describe('.param', function() {436    it('should call param function when routing VERBS', function(done) {437      var router = new Router();438439      router.param('id', function(req, res, next, id) {440        assert.equal(id, '123');441        next();442      });443444      router.get('/foo/:id/bar', function(req, res, next) {445        assert.equal(req.params.id, '123');446        next();447      });448449      router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);450    });451452    it('should call param function when routing middleware', function(done) {453      var router = new Router();454455      router.param('id', function(req, res, next, id) {456        assert.equal(id, '123');457        next();458      });459460      router.use('/foo/:id/bar', function(req, res, next) {461        assert.equal(req.params.id, '123');462        assert.equal(req.url, '/baz');463        next();464      });465466      router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);467    });468469    it('should only call once per request', function(done) {470      var count = 0;471      var req = { url: '/foo/bob/bar', method: 'get' };472      var router = new Router();473      var sub = new Router();474475      sub.get('/bar', function(req, res, next) {476        next();477      });478479      router.param('user', function(req, res, next, user) {480        count++;481        req.user = user;482        next();483      });484485      router.use('/foo/:user/', new Router());486      router.use('/foo/:user/', sub);487488      router.handle(req, {}, function(err) {489        if (err) return done(err);490        assert.equal(count, 1);491        assert.equal(req.user, 'bob');492        done();493      });494    });495496    it('should call when values differ', function(done) {497      var count = 0;498      var req = { url: '/foo/bob/bar', method: 'get' };499      var router = new Router();500      var sub = new Router();501502      sub.get('/bar', function(req, res, next) {503        next();504      });505506      router.param('user', function(req, res, next, user) {507        count++;508        req.user = user;509        next();510      });511512      router.use('/foo/:user/', new Router());513      router.use('/:user/bob/', sub);514515      router.handle(req, {}, function(err) {516        if (err) return done(err);517        assert.equal(count, 2);518        assert.equal(req.user, 'foo');519        done();520      });521    });522  });523524  describe('parallel requests', function() {525    it('should not mix requests', function(done) {526      var req1 = { url: '/foo/50/bar', method: 'get' };527      var req2 = { url: '/foo/10/bar', method: 'get' };528      var router = new Router();529      var sub = new Router();530531      done = after(2, done);532533      sub.get('/bar', function(req, res, next) {534        next();535      });536537      router.param('ms', function(req, res, next, ms) {538        ms = parseInt(ms, 10);539        req.ms = ms;540        setTimeout(next, ms);541      });542543      router.use('/foo/:ms/', new Router());544      router.use('/foo/:ms/', sub);545546      router.handle(req1, {}, function(err) {547        assert.ifError(err);548        assert.equal(req1.ms, 50);549        assert.equal(req1.originalUrl, '/foo/50/bar');550        done();551      });552553      router.handle(req2, {}, function(err) {554        assert.ifError(err);555        assert.equal(req2.ms, 10);556        assert.equal(req2.originalUrl, '/foo/10/bar');557        done();558      });559    });560  });561})