Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Set 'Retry-After' header when throwing error #63

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ module.exports = function ratelimit (opts = {}) {
ctx.body = opts.errorMessage || `Rate limit exceeded, retry in ${ms(delta, { long: true })}.`

if (opts.throw) {
headers['Retry-After'] = after
ctx.throw(ctx.status, ctx.body, { headers })
}
}
Expand Down
11 changes: 8 additions & 3 deletions test/memory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ describe('ratelimit middleware with memory driver', () => {
describe('limit with throw', () => {
let guard
let app
let errorWasThrown

const hitOnce = () => guard.should.equal(1)

beforeEach(async () => {
app = new Koa()
errorWasThrown = false

app.use(async (ctx, next) => {
try {
await next()
} catch (e) {
ctx.body = e.message
ctx.set(Object.assign({ 'X-Custom': 'foobar' }, e.headers))
errorWasThrown = true
throw e
}
})

Expand Down Expand Up @@ -103,10 +105,13 @@ describe('ratelimit middleware with memory driver', () => {
it('responds with 429 when rate limit is exceeded', async () => {
await request(app.listen())
.get('/')
.expect('X-Custom', 'foobar')
.expect('X-RateLimit-Remaining', '0')
.expect('Retry-After', '0')
.expect((res) => res.error.text.should.match(/^Rate limit exceeded, retry in.*/))
.expect(429)
.then(() => {
errorWasThrown.should.equal(true)
})
})
})

Expand Down
11 changes: 8 additions & 3 deletions test/redis.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,20 @@ describe('ratelimit middleware with redis driver', () => {
describe('limit with throw', () => {
let guard
let app
let errorWasThrown

const hitOnce = () => guard.should.equal(1)

beforeEach(async () => {
app = new Koa()
errorWasThrown = false

app.use(async (ctx, next) => {
try {
await next()
} catch (e) {
ctx.body = e.message
ctx.set(Object.assign({ 'X-Custom': 'foobar' }, e.headers))
errorWasThrown = true
throw e
}
})

Expand Down Expand Up @@ -102,10 +104,13 @@ describe('ratelimit middleware with redis driver', () => {
it('responds with 429 when rate limit is exceeded', async () => {
await request(app.listen())
.get('/')
.expect('X-Custom', 'foobar')
.expect('X-RateLimit-Remaining', '0')
.expect('Retry-After', '0')
.expect((res) => res.error.text.should.match(/^Rate limit exceeded, retry in.*/))
.expect(429)
.then(() => {
errorWasThrown.should.equal(true)
})
})
})

Expand Down