Skip to content

Commit

Permalink
Disable plugin if a key is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Melnikov committed Jul 15, 2016
1 parent 28ddcb0 commit ab24bc1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
if (err) {
return reply(err);
}
if (!key) {
// do not apply limit if there is no key
return reply.continue();
}

debug('Request key: %s', key);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-throttling",
"version": "1.0.1",
"version": "1.0.2",
"description": "HAPI rate limit plugin with Redis storage",
"main": "index.js",
"scripts": {
Expand Down
66 changes: 66 additions & 0 deletions test/disable.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var Hapi = require('hapi');
var async = require('async');
var chai = require('chai');
chai.should();

describe('HAPI with globally registered plugin', function () {
var server;
var max = 5;
var duration = 10000;

beforeEach(function() {
server = new Hapi.Server();
server.connection({port: 8000});

var options = {
redis: {
host: process.env.REDIS_HOST,
port: 6379
},
getKey: function (request, reply, done) {
done();
},
getLimit: function (request, reply, done) {
done(null, {
max: max,
duration: duration
});
}
};

server.register({
register: require('../'),
options: options
}, function (err) {
if (err) {
throw err;
}
});

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('OK');
}
});
});

it("should disable limits if the key is not determined", function (done) {
var options = {
method: "GET",
url: "/"
};

server.inject(options, function (response) {
response.statusCode.should.equal(200);

response.headers.should.not.have.property('x-ratelimit-limit');
response.headers.should.not.have.property('x-ratelimit-remaining');
response.headers.should.not.have.property('x-ratelimit-reset');

done();
});
});

});

0 comments on commit ab24bc1

Please sign in to comment.