diff --git a/lib/index.js b/lib/index.js index 5317ba3..422615f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,6 +47,7 @@ var InsightAPI = function(options) { this.cacheLongSeconds = options.cacheLongSeconds; this.rateLimiterOptions = options.rateLimiterOptions; + this.disableRateLimiter = options.disableRateLimiter; this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE; this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE; @@ -130,8 +131,10 @@ InsightAPI.prototype.setupRoutes = function(app) { var self = this; //Enable rate limiter - var limiter = this._getRateLimiter(); - app.use(limiter.middleware()); + if (!this.disableRateLimiter) { + var limiter = this._getRateLimiter(); + app.use(limiter.middleware()); + } //Setup logging morgan.token('remote-forward-addr', function(req){ diff --git a/test/index.js b/test/index.js index 26e1a76..964fe6e 100644 --- a/test/index.js +++ b/test/index.js @@ -15,6 +15,14 @@ describe('Index', function() { }); index.rateLimiterOptions.should.equal(options); }); + it('will set disable rate limiter option', function() { + var node = {}; + var index = new InsightAPI({ + disableRateLimiter: true, + node: node + }); + index.disableRateLimiter.should.equal(true); + }); }); describe('#_getRateLimiter', function() { it('will pass options to rate limiter', function() { @@ -156,4 +164,46 @@ describe('Index', function() { }); }); }); + describe('#setupRoutes', function() { + it('will use rate limiter by default', function() { + var node = {}; + var index = new InsightAPI({ + node: node + }); + var middlewareFunc = sinon.stub(); + var middleware = sinon.stub().returns(middlewareFunc); + var limiter = { + middleware: middleware + }; + index._getRateLimiter = sinon.stub().returns(limiter); + var use = sinon.stub(); + var app = { + use: use, + get: sinon.stub(), + param: sinon.stub(), + post: sinon.stub() + }; + index.setupRoutes(app); + use.callCount.should.be.above(0); + use.args[0][0].should.equal(middlewareFunc); + middleware.callCount.should.equal(1); + }); + it('will NOT use rate limiter if disabled', function() { + var node = {}; + var index = new InsightAPI({ + node: node, + disableRateLimiter: true + }); + index._getRateLimiter = sinon.stub(); + var use = sinon.stub(); + var app = { + use: use, + get: sinon.stub(), + param: sinon.stub(), + post: sinon.stub() + }; + index.setupRoutes(app); + index._getRateLimiter.callCount.should.equal(0); + }); + }); });