mirror of
https://github.com/scratchfoundation/node-redis-rate-limiter.git
synced 2024-11-28 10:16:26 -05:00
Catch invalid rate shorthand notations
This commit is contained in:
parent
2dbff9abae
commit
8d92f4df7e
2 changed files with 25 additions and 3 deletions
|
@ -21,6 +21,8 @@ exports.canonical = function(opts) {
|
|||
assert.ok(match, 'Invalid rate: ' + opts.rate);
|
||||
canon.limit = parseInt(match[1], 10);
|
||||
canon.window = moment.duration(1, match[2]) / 1000;
|
||||
assert.notEqual(canon.limit, 0, 'Invalid rate limit: ' + opts.rate);
|
||||
assert.notEqual(canon.window, 0, 'Invalid rate window: ' + opts.rate);
|
||||
}
|
||||
|
||||
// Limit + window
|
||||
|
|
|
@ -95,14 +95,34 @@ describe('Options', function() {
|
|||
assertRate('5000/d', 5000, 86400);
|
||||
});
|
||||
|
||||
it('has to be a valid rate', function() {
|
||||
it('has to be a valid format', function() {
|
||||
(function() {
|
||||
var opts = options.canonical({
|
||||
redis: {},
|
||||
key: 'ip',
|
||||
rate: '50 things'
|
||||
rate: 'foobar'
|
||||
});
|
||||
}).should.throw('Invalid rate: 50 things');
|
||||
}).should.throw('Invalid rate: foobar');
|
||||
});
|
||||
|
||||
it('has to be a valid limit', function() {
|
||||
(function() {
|
||||
var opts = options.canonical({
|
||||
redis: {},
|
||||
key: 'ip',
|
||||
rate: '0/hour'
|
||||
});
|
||||
}).should.throw('Invalid rate limit: 0/hour');
|
||||
});
|
||||
|
||||
it('has to be a valid unit', function() {
|
||||
(function() {
|
||||
var opts = options.canonical({
|
||||
redis: {},
|
||||
key: 'ip',
|
||||
rate: '50/century'
|
||||
});
|
||||
}).should.throw('Invalid rate window: 50/century');
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue