@@ -145,7 +145,7 @@ describe('rate limiter', () => {
145
145
target : 'jsdelivr.com' ,
146
146
} ) . expect ( 202 ) as Response ;
147
147
148
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 99999 ) ;
148
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( ' 99999' ) ;
149
149
} ) ;
150
150
151
151
it ( 'should fail (limit reached)' , async ( ) => {
@@ -156,7 +156,7 @@ describe('rate limiter', () => {
156
156
target : 'jsdelivr.com' ,
157
157
} ) . expect ( 429 ) as Response ;
158
158
159
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 0 ) ;
159
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
160
160
} ) ;
161
161
162
162
it ( 'should consume all points successfully or none at all (cost > remaining > 0)' , async ( ) => {
@@ -168,7 +168,7 @@ describe('rate limiter', () => {
168
168
limit : 2 ,
169
169
} ) . expect ( 429 ) as Response ;
170
170
171
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 1 ) ;
171
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '1' ) ;
172
172
} ) ;
173
173
} ) ;
174
174
@@ -183,7 +183,7 @@ describe('rate limiter', () => {
183
183
target : 'jsdelivr.com' ,
184
184
} ) . expect ( 202 ) as Response ;
185
185
186
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 249 ) ;
186
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( ' 249' ) ;
187
187
} ) ;
188
188
189
189
it ( 'should fail (limit reached)' , async ( ) => {
@@ -196,7 +196,7 @@ describe('rate limiter', () => {
196
196
target : 'jsdelivr.com' ,
197
197
} ) . expect ( 429 ) as Response ;
198
198
199
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 0 ) ;
199
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
200
200
} ) ;
201
201
202
202
it ( 'should consume all points successfully or none at all (cost > remaining > 0)' , async ( ) => {
@@ -210,7 +210,7 @@ describe('rate limiter', () => {
210
210
limit : 2 ,
211
211
} ) . expect ( 429 ) as Response ;
212
212
213
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 1 ) ;
213
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '1' ) ;
214
214
} ) ;
215
215
} ) ;
216
216
@@ -235,7 +235,9 @@ describe('rate limiter', () => {
235
235
limit : 2 ,
236
236
} ) . expect ( 202 ) as Response ;
237
237
238
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 248 ) ;
238
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '248' ) ;
239
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . not . exist ;
240
+ expect ( response . headers [ 'x-credits-remaining' ] ) . to . not . exist ;
239
241
const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
240
242
expect ( amount ) . to . equal ( 10 ) ;
241
243
} ) ;
@@ -251,7 +253,9 @@ describe('rate limiter', () => {
251
253
limit : 2 ,
252
254
} ) . expect ( 202 ) as Response ;
253
255
254
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 0 ) ;
256
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
257
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . equal ( '2' ) ;
258
+ expect ( ( response . headers [ 'x-credits-remaining' ] ) ) . to . equal ( '8' ) ;
255
259
const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
256
260
expect ( amount ) . to . equal ( 8 ) ;
257
261
} ) ;
@@ -267,7 +271,9 @@ describe('rate limiter', () => {
267
271
limit : 2 ,
268
272
} ) . expect ( 202 ) as Response ;
269
273
270
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 0 ) ;
274
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
275
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . equal ( '1' ) ;
276
+ expect ( ( response . headers [ 'x-credits-remaining' ] ) ) . to . equal ( '9' ) ;
271
277
const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
272
278
expect ( amount ) . to . equal ( 9 ) ;
273
279
} ) ;
@@ -289,7 +295,9 @@ describe('rate limiter', () => {
289
295
limit : 2 ,
290
296
} ) . expect ( 429 ) as Response ;
291
297
292
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 0 ) ;
298
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
299
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . not . exist ;
300
+ expect ( response . headers [ 'x-credits-remaining' ] ) . to . not . exist ;
293
301
const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
294
302
expect ( amount ) . to . equal ( 1 ) ;
295
303
} ) ;
@@ -311,9 +319,33 @@ describe('rate limiter', () => {
311
319
limit : 2 ,
312
320
} ) . expect ( 429 ) as Response ;
313
321
314
- expect ( Number ( response . headers [ 'x-ratelimit-remaining' ] ) ) . to . equal ( 1 ) ;
322
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '1' ) ;
323
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . not . exist ;
324
+ expect ( response . headers [ 'x-credits-remaining' ] ) . to . not . exist ;
315
325
const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
316
326
expect ( amount ) . to . equal ( 0 ) ;
317
327
} ) ;
328
+
329
+ it ( 'should work fine if there is no credits row for that user' , async ( ) => {
330
+ await authenticatedRateLimiter . set ( '89da69bd-a236-4ab7-9c5d-b5f52ce09959' , 250 , 0 ) ;
331
+
332
+ await client ( CREDITS_TABLE ) . where ( {
333
+ user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' ,
334
+ } ) . delete ( ) ;
335
+
336
+ const response = await requestAgent . post ( '/v1/measurements' )
337
+ . set ( 'Authorization' , 'Bearer v2lUHEVLtVSskaRKDBabpyp4AkzdMnob' )
338
+ . send ( {
339
+ type : 'ping' ,
340
+ target : 'jsdelivr.com' ,
341
+ limit : 2 ,
342
+ } ) . expect ( 429 ) as Response ;
343
+
344
+ expect ( response . headers [ 'x-ratelimit-remaining' ] ) . to . equal ( '0' ) ;
345
+ expect ( response . headers [ 'x-credits-cost' ] ) . to . not . exist ;
346
+ expect ( response . headers [ 'x-credits-remaining' ] ) . to . not . exist ;
347
+ const [ { amount } ] = await client ( CREDITS_TABLE ) . select ( 'amount' ) . where ( { user_id : '89da69bd-a236-4ab7-9c5d-b5f52ce09959' } ) ;
348
+ expect ( amount ) . to . equal ( null ) ;
349
+ } ) ;
318
350
} ) ;
319
351
} ) ;
0 commit comments