Catch invalid rate shorthand notations

This commit is contained in:
Romain Prieto 2014-08-22 15:50:49 +10:00
parent 2dbff9abae
commit 8d92f4df7e
2 changed files with 25 additions and 3 deletions

View file

@ -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

View file

@ -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');
});
});