@@ -34,21 +34,27 @@ test.group('Limiter', () => {
3434 */
3535 const consumeCall = sinon . spy ( store , 'consume' )
3636 await limiter . consume ( 'ip_localhost' )
37- assert . isTrue ( consumeCall . calledOnceWithExactly ( 'ip_localhost' ) , 'consume called' )
37+ assert . isTrue ( consumeCall . calledOnceWithExactly ( 'ip_localhost' , undefined ) , 'consume called' )
3838
3939 /**
4040 * increment call
4141 */
4242 const incrementCall = sinon . spy ( store , 'increment' )
4343 await limiter . increment ( 'ip_localhost' )
44- assert . isTrue ( incrementCall . calledOnceWithExactly ( 'ip_localhost' ) , 'increment called' )
44+ assert . isTrue (
45+ incrementCall . calledOnceWithExactly ( 'ip_localhost' , undefined ) ,
46+ 'increment called'
47+ )
4548
4649 /**
4750 * decrement call
4851 */
4952 const decrementCall = sinon . spy ( store , 'decrement' )
5053 await limiter . decrement ( 'ip_localhost' )
51- assert . isTrue ( decrementCall . calledOnceWithExactly ( 'ip_localhost' ) , 'decrement called' )
54+ assert . isTrue (
55+ decrementCall . calledOnceWithExactly ( 'ip_localhost' , undefined ) ,
56+ 'decrement called'
57+ )
5258
5359 /**
5460 * get call
@@ -104,6 +110,142 @@ test.group('Limiter', () => {
104110 await assert . doesNotReject ( ( ) => limiter . increment ( 'ip_localhost' ) )
105111 } )
106112
113+ test ( 'increment requests count with negative amount should default to 1' , async ( { assert } ) => {
114+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
115+ const store = new LimiterRedisStore ( redis , {
116+ duration : '1 minute' ,
117+ requests : 5 ,
118+ } )
119+
120+ const limiter = new Limiter ( store )
121+
122+ await limiter . increment ( 'ip_localhost' , - 5 )
123+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 4 )
124+ } )
125+
126+ test ( 'decrement requests count with negative amount should default to 1' , async ( { assert } ) => {
127+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
128+ const store = new LimiterRedisStore ( redis , {
129+ duration : '1 minute' ,
130+ requests : 5 ,
131+ } )
132+
133+ const limiter = new Limiter ( store )
134+
135+ await limiter . increment ( 'ip_localhost' , 5 )
136+ await limiter . decrement ( 'ip_localhost' , - 3 )
137+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 1 )
138+ } )
139+
140+ test ( 'increment requests count with zero amount should default to 1' , async ( { assert } ) => {
141+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
142+ const store = new LimiterRedisStore ( redis , {
143+ duration : '1 minute' ,
144+ requests : 5 ,
145+ } )
146+
147+ const limiter = new Limiter ( store )
148+
149+ await limiter . increment ( 'ip_localhost' , 0 )
150+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 4 )
151+ } )
152+
153+ test ( 'decrement requests count with zero amount should default to 1' , async ( { assert } ) => {
154+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
155+ const store = new LimiterRedisStore ( redis , {
156+ duration : '1 minute' ,
157+ requests : 5 ,
158+ } )
159+
160+ const limiter = new Limiter ( store )
161+
162+ await limiter . increment ( 'ip_localhost' , 5 )
163+ await limiter . decrement ( 'ip_localhost' , 0 )
164+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 1 )
165+ } )
166+
167+ test ( 'increment remaining requests by amount' , async ( { assert } ) => {
168+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
169+ const store = new LimiterRedisStore ( redis , {
170+ duration : '1 minute' ,
171+ requests : 5 ,
172+ } )
173+
174+ const limiter = new Limiter ( store )
175+
176+ await limiter . increment ( 'ip_localhost' , 3 )
177+ const response = await limiter . get ( 'ip_localhost' )
178+ assert . containsSubset ( response , {
179+ consumed : 3 ,
180+ remaining : 2 ,
181+ limit : 5 ,
182+ } )
183+ } )
184+
185+ test ( 'decrement consumed requests by amount' , async ( { assert } ) => {
186+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
187+ const store = new LimiterRedisStore ( redis , {
188+ duration : '1 minute' ,
189+ requests : 5 ,
190+ } )
191+
192+ const limiter = new Limiter ( store )
193+
194+ await limiter . increment ( 'ip_localhost' , 4 )
195+ await limiter . decrement ( 'ip_localhost' , 2 )
196+ const response = await limiter . get ( 'ip_localhost' )
197+ assert . containsSubset ( response , {
198+ consumed : 2 ,
199+ remaining : 3 ,
200+ limit : 5 ,
201+ } )
202+ } )
203+
204+ test ( 'consume remaining requests by amount' , async ( { assert } ) => {
205+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
206+ const store = new LimiterRedisStore ( redis , {
207+ duration : '1 minute' ,
208+ requests : 5 ,
209+ } )
210+
211+ const limiter = new Limiter ( store )
212+
213+ await limiter . consume ( 'ip_localhost' , 3 )
214+ const response = await limiter . get ( 'ip_localhost' )
215+ assert . containsSubset ( response , {
216+ consumed : 3 ,
217+ remaining : 2 ,
218+ limit : 5 ,
219+ } )
220+ } )
221+
222+ test ( 'increment requests count with a custom amount' , async ( { assert } ) => {
223+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
224+ const store = new LimiterRedisStore ( redis , {
225+ duration : '1 minute' ,
226+ requests : 10 ,
227+ } )
228+
229+ const limiter = new Limiter ( store )
230+
231+ await limiter . increment ( 'ip_localhost' , 3 )
232+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 7 )
233+ } )
234+
235+ test ( 'decrement requests count with a custom amount' , async ( { assert } ) => {
236+ const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
237+ const store = new LimiterRedisStore ( redis , {
238+ duration : '1 minute' ,
239+ requests : 10 ,
240+ } )
241+
242+ const limiter = new Limiter ( store )
243+
244+ await limiter . increment ( 'ip_localhost' , 10 )
245+ await limiter . decrement ( 'ip_localhost' , 4 )
246+ assert . equal ( await limiter . remaining ( 'ip_localhost' ) , 4 )
247+ } )
248+
107249 test ( 'do not run action when all requests have been exhausted' , async ( { assert } ) => {
108250 const executionStack : string [ ] = [ ]
109251 const redis = createRedis ( [ 'rlflx:ip_localhost' ] ) . connection ( )
0 commit comments