Skip to content
Myr

Router.js

This file

Path

test/Router.js

552 lines · 14.9 KiB · JavaScript

Last changed by

deps: router@2.0.0-beta.1Douglas Christopher Wilsonbefore adoption1574925

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 = new Router();11    assert(typeof router === 'function')1213    assert(typeof router.get === 'function')14    assert(typeof router.handle === 'function')15    assert(typeof router.use === 'function')16  });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);2627    router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }, function(){});28  });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      assert.strictEqual(req.params.bar, 'route')36      res.end();37    });38    router.use('/:foo', another);3940    router.handle({ url: '/test/route', method: 'GET' }, { end: done }, function(){});41  });4243  it('should handle blank URL', function(done){44    var router = new Router();4546    router.use(function (req, res) {47      throw new Error('should not be called')48    });4950    router.handle({ url: '', method: 'GET' }, {}, done);51  });5253  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  })6263  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    });7475    router.handle({ url: '/', method: 'GET' }, { end: done }, function(){});76  });7778  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          assert.strictEqual(val, 'foo')89          done();90        }91      }92      router.handle({ url: '/foo', method: 'GET' }, res, function(){});93    })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    });243244    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    });260261    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    })347  })348349  describe('.use', function() {350    it('should require middleware', function () {351      var router = new Router()352      assert.throws(function () { router.use('/') }, /argument handler is required/)353    })354355    it('should reject string as middleware', function () {356      var router = new Router()357      assert.throws(function () { router.use('/', 'foo') }, /argument handler must be a function/)358    })359360    it('should reject number as middleware', function () {361      var router = new Router()362      assert.throws(function () { router.use('/', 42) }, /argument handler must be a function/)363    })364365    it('should reject null as middleware', function () {366      var router = new Router()367      assert.throws(function () { router.use('/', null) }, /argument handler must be a function/)368    })369370    it('should reject Date as middleware', function () {371      var router = new Router()372      assert.throws(function () { router.use('/', new Date()) }, /argument handler must be a function/)373    })374375    it('should be called for any URL', function (done) {376      var cb = after(4, done)377      var router = new Router()378379      function no () {380        throw new Error('should not be called')381      }382383      router.use(function (req, res) {384        res.end()385      })386387      router.handle({ url: '/', method: 'GET' }, { end: cb }, no)388      router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)389      router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)390      router.handle({ url: '*', method: 'GET' }, { end: cb }, no)391    })392393    it('should accept array of middleware', function(done){394      var count = 0;395      var router = new Router();396397      function fn1(req, res, next){398        assert.equal(++count, 1);399        next();400      }401402      function fn2(req, res, next){403        assert.equal(++count, 2);404        next();405      }406407      router.use([fn1, fn2], function(req, res){408        assert.equal(++count, 3);409        done();410      });411412      router.handle({ url: '/foo', method: 'GET' }, {}, function(){});413    })414  })415416  describe('.param', function() {417    it('should require function', function () {418      var router = new Router();419      assert.throws(router.param.bind(router, 'id'), /argument fn is required/);420    });421422    it('should reject non-function', function () {423      var router = new Router();424      assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);425    });426427    it('should call param function when routing VERBS', function(done) {428      var router = new Router();429430      router.param('id', function(req, res, next, id) {431        assert.equal(id, '123');432        next();433      });434435      router.get('/foo/:id/bar', function(req, res, next) {436        assert.equal(req.params.id, '123');437        next();438      });439440      router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);441    });442443    it('should call param function when routing middleware', function(done) {444      var router = new Router();445446      router.param('id', function(req, res, next, id) {447        assert.equal(id, '123');448        next();449      });450451      router.use('/foo/:id/bar', function(req, res, next) {452        assert.equal(req.params.id, '123');453        assert.equal(req.url, '/baz');454        next();455      });456457      router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);458    });459460    it('should only call once per request', function(done) {461      var count = 0;462      var req = { url: '/foo/bob/bar', method: 'get' };463      var router = new Router();464      var sub = new Router();465466      sub.get('/bar', function(req, res, next) {467        next();468      });469470      router.param('user', function(req, res, next, user) {471        count++;472        req.user = user;473        next();474      });475476      router.use('/foo/:user/', new Router());477      router.use('/foo/:user/', sub);478479      router.handle(req, {}, function(err) {480        if (err) return done(err);481        assert.equal(count, 1);482        assert.equal(req.user, 'bob');483        done();484      });485    });486487    it('should call when values differ', function(done) {488      var count = 0;489      var req = { url: '/foo/bob/bar', method: 'get' };490      var router = new Router();491      var sub = new Router();492493      sub.get('/bar', function(req, res, next) {494        next();495      });496497      router.param('user', function(req, res, next, user) {498        count++;499        req.user = user;500        next();501      });502503      router.use('/foo/:user/', new Router());504      router.use('/:user/bob/', sub);505506      router.handle(req, {}, function(err) {507        if (err) return done(err);508        assert.equal(count, 2);509        assert.equal(req.user, 'foo');510        done();511      });512    });513  });514515  describe('parallel requests', function() {516    it('should not mix requests', function(done) {517      var req1 = { url: '/foo/50/bar', method: 'get' };518      var req2 = { url: '/foo/10/bar', method: 'get' };519      var router = new Router();520      var sub = new Router();521522      done = after(2, done);523524      sub.get('/bar', function(req, res, next) {525        next();526      });527528      router.param('ms', function(req, res, next, ms) {529        ms = parseInt(ms, 10);530        req.ms = ms;531        setTimeout(next, ms);532      });533534      router.use('/foo/:ms/', new Router());535      router.use('/foo/:ms/', sub);536537      router.handle(req1, {}, function(err) {538        assert.ifError(err);539        assert.equal(req1.ms, 50);540        assert.equal(req1.originalUrl, '/foo/50/bar');541        done();542      });543544      router.handle(req2, {}, function(err) {545        assert.ifError(err);546        assert.equal(req2.ms, 10);547        assert.equal(req2.originalUrl, '/foo/10/bar');548        done();549      });550    });551  });552})