Merge pull request #484 from braydonf/ratelimiter-options

index: configurable ratelimiter options
This commit is contained in:
Matias Alejo Garcia 2016-06-11 11:25:36 -03:00 committed by GitHub
commit 7db7b1214b
3 changed files with 89 additions and 2 deletions

View File

@ -46,6 +46,9 @@ var InsightAPI = function(options) {
this.cacheShortSeconds = options.cacheShortSeconds;
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;
@ -116,13 +119,22 @@ InsightAPI.prototype.getRemoteAddress = function(req) {
return req.socket.remoteAddress;
};
InsightAPI.prototype._getRateLimiter = function() {
var rateLimiterOptions = _.isUndefined(this.rateLimiterOptions) ? {} : _.clone(this.rateLimiterOptions);
rateLimiterOptions.node = this.node;
var limiter = new RateLimiter(rateLimiterOptions);
return limiter;
};
InsightAPI.prototype.setupRoutes = function(app) {
var self = this;
//Enable rate limiter
var limiter = new RateLimiter({node: this.node});
app.use(limiter.middleware());
if (!this.disableRateLimiter) {
var limiter = this._getRateLimiter();
app.use(limiter.middleware());
}
//Setup logging
morgan.token('remote-forward-addr', function(req){

View File

@ -5,6 +5,39 @@ var sinon = require('sinon');
var InsightAPI = require('../lib/index');
describe('Index', function() {
describe('@constructor', function() {
it('will set rate limiter options', function() {
var options = {};
var node = {};
var index = new InsightAPI({
rateLimiterOptions: options,
node: node
});
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() {
var options = {
whitelist: ['127.0.0.1']
};
var node = {};
var index = new InsightAPI({
rateLimiterOptions: options,
node: node
});
var limiter = index._getRateLimiter();
limiter.whitelist.should.eql(['127.0.0.1']);
});
});
describe('#cache', function() {
it('will set cache control header', function(done) {
var node = {
@ -131,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);
});
});
});