Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(redis): throw error when passing points as non integer number #267

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/RateLimiterStoreAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,17 @@ module.exports = class RateLimiterStoreAbstract extends RateLimiterAbstract {
* @returns Promise<RateLimiterRes>
*/
consume(key, pointsToConsume = 1, options = {}) {
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
if(
typeof pointsToConsume == 'string'
){
if(!RegExp("^[1-9][0-9]*$").test(pointsToConsume)){
reject(new Error("Consuming string different than integer values is not supported by this package"));
}
} else if (!Number.isInteger(pointsToConsume)){
reject(new Error("Consuming decimal number of points is not supported by this package"));
}

const rlKey = this.getKey(key);

const inMemoryBlockMsBeforeExpire = this.getInMemoryBlockMsBeforeExpire(rlKey);
Expand Down
85 changes: 85 additions & 0 deletions test/RateLimiterRedis.ioredis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,91 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
});
});

describe('when points are passed as decimal numbers', () => {
it('thows error', (done) => {
const testKey = 'consume2';
const rateLimiter = new RateLimiterRedis({
storeClient: redisMockClient,
points: 1,
duration: 5
});

rateLimiter
.consume(testKey, 1.1)
.then(() => {
done(new Error('must not'));
})
.catch((err) => {
expect(err.message).to.equal('Consuming decimal number of points is not supported by this package')
done();
});
});
});

describe('when passing points as float without decimal values', () => {
it('does not throw an error', (done) => {
const testKey = 'consume1';
const rateLimiter = new RateLimiterRedis({
storeClient: redisMockClient,
points: 3,
duration: 5,
});
rateLimiter
.consume(testKey, 2.0)
.then(() => {
redisMockClient.get(rateLimiter.getKey(testKey)).then((consumedPoints)=>{
expect(consumedPoints).to.equal('2');
done();
});
})
.catch((err) => {
done(err);
});
});
});

describe('when passing points as string with decimal values', () => {
it('throws error', (done) => {
const testKey = 'consume1';
const rateLimiter = new RateLimiterRedis({
storeClient: redisMockClient,
points: 3,
duration: 5,
});
rateLimiter
.consume(testKey, "2.0")
.then(() => {
done(new Error('must not'));
})
.catch((err) => {
expect(err.message).to.equal('Consuming string different than integer values is not supported by this package')
done();
});
});
});

describe('when passing points as string without decimal values', () => {
it('does not throw an error', (done) => {
const testKey = 'consume1';
const rateLimiter = new RateLimiterRedis({
storeClient: redisMockClient,
points: 3,
duration: 5,
});
rateLimiter
.consume(testKey, "2")
.then(() => {
redisMockClient.get(rateLimiter.getKey(testKey)).then((consumedPoints)=>{
expect(consumedPoints).to.equal('2');
done();
});
})
.catch((err) => {
done(err);
});
});
});

it('execute evenly over duration', (done) => {
const testKey = 'consumeEvenly';
const rateLimiter = new RateLimiterRedis({
Expand Down
Loading