@@ -26,21 +26,22 @@ test.group('Http limiter', () => {
26
26
} )
27
27
28
28
const apiLimiter = limiterManager . define ( 'api' , ( _ , limiter ) => {
29
- return limiter . allowRequests ( 10 ) . every ( '1 minute' )
29
+ return limiter . allowRequests ( 10 ) . every ( '1 minute' ) . blockFor ( '20 mins' )
30
30
} )
31
31
32
32
const ctx = new HttpContextFactory ( ) . create ( )
33
33
ctx . request . ip = function ( ) {
34
34
return 'localhost'
35
35
}
36
36
37
- const limiter = apiLimiter ( ctx )
37
+ const limiter = await apiLimiter ( ctx )
38
38
39
39
assert . instanceOf ( limiter , HttpLimiter )
40
- assert . deepEqual ( limiter . toJSON ( ) , {
40
+ assert . deepEqual ( limiter ! . toJSON ( ) , {
41
41
duration : '1 minute' ,
42
42
key : `api_localhost` ,
43
43
requests : 10 ,
44
+ blockDuration : '20 mins' ,
44
45
store : undefined ,
45
46
} )
46
47
} )
@@ -59,10 +60,10 @@ test.group('Http limiter', () => {
59
60
} )
60
61
61
62
const ctx = new HttpContextFactory ( ) . create ( )
62
- const limiter = apiLimiter ( ctx )
63
+ const limiter = await apiLimiter ( ctx )
63
64
64
65
assert . instanceOf ( limiter , HttpLimiter )
65
- assert . deepEqual ( limiter . toJSON ( ) , {
66
+ assert . deepEqual ( limiter ! . toJSON ( ) , {
66
67
duration : '1 minute' ,
67
68
key : `api_1` ,
68
69
requests : 10 ,
@@ -84,10 +85,10 @@ test.group('Http limiter', () => {
84
85
} )
85
86
86
87
const ctx = new HttpContextFactory ( ) . create ( )
87
- const limiter = apiLimiter ( ctx )
88
+ const limiter = await apiLimiter ( ctx )
88
89
89
90
assert . instanceOf ( limiter , HttpLimiter )
90
- assert . deepEqual ( limiter . toJSON ( ) , {
91
+ assert . deepEqual ( limiter ! . toJSON ( ) , {
91
92
duration : '1 minute' ,
92
93
key : `api_1` ,
93
94
requests : 10 ,
@@ -109,10 +110,10 @@ test.group('Http limiter', () => {
109
110
} )
110
111
111
112
const ctx = new HttpContextFactory ( ) . create ( )
112
- const limiter = apiLimiter ( ctx )
113
+ const limiter = await apiLimiter ( ctx )
113
114
114
- await assert . doesNotReject ( ( ) => limiter . throttle ( ) )
115
- await assert . rejects ( ( ) => limiter . throttle ( ) )
115
+ await assert . doesNotReject ( ( ) => limiter ! . throttle ( ) )
116
+ await assert . rejects ( ( ) => limiter ! . throttle ( ) )
116
117
} )
117
118
118
119
test ( 'throttle requests using dynamic rules' , async ( { assert } ) => {
@@ -136,19 +137,19 @@ test.group('Http limiter', () => {
136
137
*/
137
138
const ctx = new HttpContextFactory ( ) . create ( )
138
139
ctx . request . updateBody ( { user_id : 1 } )
139
- const limiter = apiLimiter ( ctx )
140
- await assert . doesNotReject ( ( ) => limiter . throttle ( ) )
141
- await assert . rejects ( ( ) => limiter . throttle ( ) )
140
+ const limiter = await apiLimiter ( ctx )
141
+ await assert . doesNotReject ( ( ) => limiter ! . throttle ( ) )
142
+ await assert . rejects ( ( ) => limiter ! . throttle ( ) )
142
143
143
144
/**
144
145
* Allows two requests for user with id 2
145
146
*/
146
147
const freshCtx = new HttpContextFactory ( ) . create ( )
147
148
freshCtx . request . updateBody ( { user_id : 2 } )
148
- const freshLimiter = apiLimiter ( freshCtx )
149
- await assert . doesNotReject ( ( ) => freshLimiter . throttle ( ) )
150
- await assert . doesNotReject ( ( ) => freshLimiter . throttle ( ) )
151
- await assert . rejects ( ( ) => freshLimiter . throttle ( ) )
149
+ const freshLimiter = await apiLimiter ( freshCtx )
150
+ await assert . doesNotReject ( ( ) => freshLimiter ! . throttle ( ) )
151
+ await assert . doesNotReject ( ( ) => freshLimiter ! . throttle ( ) )
152
+ await assert . rejects ( ( ) => freshLimiter ! . throttle ( ) )
152
153
} )
153
154
154
155
test ( 'customize exception' , async ( { assert } ) => {
@@ -173,11 +174,11 @@ test.group('Http limiter', () => {
173
174
} )
174
175
175
176
const ctx = new HttpContextFactory ( ) . create ( )
176
- const limiter = apiLimiter ( ctx )
177
+ const limiter = await apiLimiter ( ctx )
177
178
178
- await limiter . throttle ( )
179
+ await limiter ! . throttle ( )
179
180
try {
180
- await limiter . throttle ( )
181
+ await limiter ! . throttle ( )
181
182
} catch ( error ) {
182
183
assert . equal ( error . message , 'Requests exhaused' )
183
184
assert . equal ( error . status , 400 )
@@ -198,9 +199,9 @@ test.group('Http limiter', () => {
198
199
} )
199
200
200
201
const ctx = new HttpContextFactory ( ) . create ( )
201
- const limiter = apiLimiter ( ctx )
202
+ const limiter = await apiLimiter ( ctx )
202
203
203
- const [ first , second ] = await Promise . allSettled ( [ limiter . throttle ( ) , limiter . throttle ( ) ] )
204
+ const [ first , second ] = await Promise . allSettled ( [ limiter ! . throttle ( ) , limiter ! . throttle ( ) ] )
204
205
assert . equal ( first . status , 'fulfilled' )
205
206
assert . equal ( second . status , 'rejected' )
206
207
} )
@@ -226,15 +227,15 @@ test.group('Http limiter', () => {
226
227
227
228
const ctx = new HttpContextFactory ( ) . create ( )
228
229
await assert . rejects (
229
- ( ) => noRequests ( ctx ) . throttle ( ) ,
230
+ async ( ) => ( await noRequests ( ctx ) ) ! . throttle ( ) ,
230
231
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
231
232
)
232
233
await assert . rejects (
233
- ( ) => noDuration ( ctx ) . throttle ( ) ,
234
+ async ( ) => ( await noDuration ( ctx ) ) ! . throttle ( ) ,
234
235
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
235
236
)
236
237
await assert . rejects (
237
- ( ) => noConfig ( ctx ) . throttle ( ) ,
238
+ async ( ) => ( await noConfig ( ctx ) ) ! . throttle ( ) ,
238
239
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
239
240
)
240
241
} )
0 commit comments