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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add describe statements
  • Loading branch information
roggervalf committed Sep 28, 2024
commit bcae315c95518968b8c38185f6521f4b3c56f2d3
105 changes: 55 additions & 50 deletions test/RateLimiterRedis.ioredis.test.js
Original file line number Diff line number Diff line change
@@ -171,63 +171,68 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
});
});

it('consume 2 point passed as float', (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 float without decimal values', () => {
it('does not throw an error', (done) => {
const testKey = 'consume1';
const rateLimiter = new RateLimiterRedis({
storeClient: redisMockClient,
points: 3,
duration: 5,
});
});

it('consume 2.0 point passed as string', (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);
});
});
rateLimiter
.consume(testKey, "2.0")
.then(() => {
done(new Error('must not'));
})
.catch((err) => {
console.log(err.message)
expect(err.message).to.equal('Consuming string different than integer values is not supported by this package')
done();
});
});

it('consume 2 point passed as string', (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');
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();
});
})
.catch((err) => {
done(err);
});
});

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) => {
Loading
Oops, something went wrong.