@@ -153,6 +153,91 @@ describe('RateLimiterRedis with fixed window', function RateLimiterRedisTest() {
153
153
} ) ;
154
154
} ) ;
155
155
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
+
156
241
it ( 'execute evenly over duration' , ( done ) => {
157
242
const testKey = 'consumeEvenly' ;
158
243
const rateLimiter = new RateLimiterRedis ( {
0 commit comments