Skip to content

Commit d7650b0

Browse files
committed
test: fix linter issues
1 parent e9fa96d commit d7650b0

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

src/http_limiter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ export class HttpLimiter<KnownStores extends Record<string, LimiterManagerStoreF
128128
return this
129129
}
130130

131+
/**
132+
* Define the block duration. The key will be blocked for the
133+
* specified duration after all the requests have been
134+
* exhausted
135+
*/
136+
blockFor(duration: number | string): this {
137+
this.#options = this.#options || {}
138+
this.#options.blockDuration = duration
139+
return this
140+
}
141+
131142
/**
132143
* Returns null to disable rate limiting for the given request
133144
*/

tests/http_limiter.spec.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ test.group('Http limiter', () => {
2626
})
2727

2828
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')
3030
})
3131

3232
const ctx = new HttpContextFactory().create()
3333
ctx.request.ip = function () {
3434
return 'localhost'
3535
}
3636

37-
const limiter = apiLimiter(ctx)
37+
const limiter = await apiLimiter(ctx)
3838

3939
assert.instanceOf(limiter, HttpLimiter)
40-
assert.deepEqual(limiter.toJSON(), {
40+
assert.deepEqual(limiter!.toJSON(), {
4141
duration: '1 minute',
4242
key: `api_localhost`,
4343
requests: 10,
44+
blockDuration: '20 mins',
4445
store: undefined,
4546
})
4647
})
@@ -59,10 +60,10 @@ test.group('Http limiter', () => {
5960
})
6061

6162
const ctx = new HttpContextFactory().create()
62-
const limiter = apiLimiter(ctx)
63+
const limiter = await apiLimiter(ctx)
6364

6465
assert.instanceOf(limiter, HttpLimiter)
65-
assert.deepEqual(limiter.toJSON(), {
66+
assert.deepEqual(limiter!.toJSON(), {
6667
duration: '1 minute',
6768
key: `api_1`,
6869
requests: 10,
@@ -84,10 +85,10 @@ test.group('Http limiter', () => {
8485
})
8586

8687
const ctx = new HttpContextFactory().create()
87-
const limiter = apiLimiter(ctx)
88+
const limiter = await apiLimiter(ctx)
8889

8990
assert.instanceOf(limiter, HttpLimiter)
90-
assert.deepEqual(limiter.toJSON(), {
91+
assert.deepEqual(limiter!.toJSON(), {
9192
duration: '1 minute',
9293
key: `api_1`,
9394
requests: 10,
@@ -109,10 +110,10 @@ test.group('Http limiter', () => {
109110
})
110111

111112
const ctx = new HttpContextFactory().create()
112-
const limiter = apiLimiter(ctx)
113+
const limiter = await apiLimiter(ctx)
113114

114-
await assert.doesNotReject(() => limiter.throttle())
115-
await assert.rejects(() => limiter.throttle())
115+
await assert.doesNotReject(() => limiter!.throttle())
116+
await assert.rejects(() => limiter!.throttle())
116117
})
117118

118119
test('throttle requests using dynamic rules', async ({ assert }) => {
@@ -136,19 +137,19 @@ test.group('Http limiter', () => {
136137
*/
137138
const ctx = new HttpContextFactory().create()
138139
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())
142143

143144
/**
144145
* Allows two requests for user with id 2
145146
*/
146147
const freshCtx = new HttpContextFactory().create()
147148
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())
152153
})
153154

154155
test('customize exception', async ({ assert }) => {
@@ -173,11 +174,11 @@ test.group('Http limiter', () => {
173174
})
174175

175176
const ctx = new HttpContextFactory().create()
176-
const limiter = apiLimiter(ctx)
177+
const limiter = await apiLimiter(ctx)
177178

178-
await limiter.throttle()
179+
await limiter!.throttle()
179180
try {
180-
await limiter.throttle()
181+
await limiter!.throttle()
181182
} catch (error) {
182183
assert.equal(error.message, 'Requests exhaused')
183184
assert.equal(error.status, 400)
@@ -198,9 +199,9 @@ test.group('Http limiter', () => {
198199
})
199200

200201
const ctx = new HttpContextFactory().create()
201-
const limiter = apiLimiter(ctx)
202+
const limiter = await apiLimiter(ctx)
202203

203-
const [first, second] = await Promise.allSettled([limiter.throttle(), limiter.throttle()])
204+
const [first, second] = await Promise.allSettled([limiter!.throttle(), limiter!.throttle()])
204205
assert.equal(first.status, 'fulfilled')
205206
assert.equal(second.status, 'rejected')
206207
})
@@ -226,15 +227,15 @@ test.group('Http limiter', () => {
226227

227228
const ctx = new HttpContextFactory().create()
228229
await assert.rejects(
229-
() => noRequests(ctx).throttle(),
230+
async () => (await noRequests(ctx))!.throttle(),
230231
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
231232
)
232233
await assert.rejects(
233-
() => noDuration(ctx).throttle(),
234+
async () => (await noDuration(ctx))!.throttle(),
234235
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
235236
)
236237
await assert.rejects(
237-
() => noConfig(ctx).throttle(),
238+
async () => (await noConfig(ctx))!.throttle(),
238239
'Cannot throttle requests for "api" limiter. Make sure to define the allowed requests and duration'
239240
)
240241
})

0 commit comments

Comments
 (0)