Skip to content

Commit 0a9aae6

Browse files
authored
Merge pull request #267 from roggervalf/throw-erron-decimal-points
fix(redis): throw error when passing points as non integer number
2 parents fa9ce19 + b4ad778 commit 0a9aae6

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

lib/RateLimiterRedis.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
7979
}
8080

8181
async _upsert(rlKey, points, msDuration, forceExpire = false) {
82+
if(
83+
typeof points == 'string'
84+
){
85+
if(!RegExp("^[1-9][0-9]*$").test(points)){
86+
throw new Error("Consuming string different than integer values is not supported by this package");
87+
}
88+
} else if (!Number.isInteger(points)){
89+
throw new Error("Consuming decimal number of points is not supported by this package");
90+
}
91+
8292
if (!this._isRedisReady()) {
8393
throw new Error('Redis connection is not ready');
8494
}

test/RateLimiterRedis.ioredis.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,91 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
153153
});
154154
});
155155

156+
describe('when points are passed as decimal numbers', () => {
157+
it('thows error', (done) => {
158+
const testKey = 'consume2';
159+
const rateLimiter = new RateLimiterRedis({
160+
storeClient: redisMockClient,
161+
points: 1,
162+
duration: 5
163+
});
164+
165+
rateLimiter
166+
.consume(testKey, 1.1)
167+
.then(() => {
168+
done(new Error('must not'));
169+
})
170+
.catch((err) => {
171+
expect(err.message).to.equal('Consuming decimal number of points is not supported by this package')
172+
done();
173+
});
174+
});
175+
});
176+
177+
describe('when passing points as float without decimal values', () => {
178+
it('does not throw an error', (done) => {
179+
const testKey = 'consume1';
180+
const rateLimiter = new RateLimiterRedis({
181+
storeClient: redisMockClient,
182+
points: 3,
183+
duration: 5,
184+
});
185+
rateLimiter
186+
.consume(testKey, 2.0)
187+
.then(() => {
188+
redisMockClient.get(rateLimiter.getKey(testKey)).then((consumedPoints)=>{
189+
expect(consumedPoints).to.equal('2');
190+
done();
191+
});
192+
})
193+
.catch((err) => {
194+
done(err);
195+
});
196+
});
197+
});
198+
199+
describe('when passing points as string with decimal values', () => {
200+
it('throws error', (done) => {
201+
const testKey = 'consume1';
202+
const rateLimiter = new RateLimiterRedis({
203+
storeClient: redisMockClient,
204+
points: 3,
205+
duration: 5,
206+
});
207+
rateLimiter
208+
.consume(testKey, "2.0")
209+
.then(() => {
210+
done(new Error('must not'));
211+
})
212+
.catch((err) => {
213+
expect(err.message).to.equal('Consuming string different than integer values is not supported by this package')
214+
done();
215+
});
216+
});
217+
});
218+
219+
describe('when passing points as string without decimal values', () => {
220+
it('does not throw an error', (done) => {
221+
const testKey = 'consume1';
222+
const rateLimiter = new RateLimiterRedis({
223+
storeClient: redisMockClient,
224+
points: 3,
225+
duration: 5,
226+
});
227+
rateLimiter
228+
.consume(testKey, "2")
229+
.then(() => {
230+
redisMockClient.get(rateLimiter.getKey(testKey)).then((consumedPoints)=>{
231+
expect(consumedPoints).to.equal('2');
232+
done();
233+
});
234+
})
235+
.catch((err) => {
236+
done(err);
237+
});
238+
});
239+
});
240+
156241
it('execute evenly over duration', (done) => {
157242
const testKey = 'consumeEvenly';
158243
const rateLimiter = new RateLimiterRedis({

0 commit comments

Comments
 (0)