Created
June 19, 2024 01:54
-
-
Save alsgu3rra/31ce2f98cae800d7f18c8ecee673ddcd to your computer and use it in GitHub Desktop.
testar rate limiter no jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { INestApplication } from '@nestjs/common'; | |
import { Test, TestingModule } from '@nestjs/testing'; | |
import * as request from 'supertest'; | |
import { AppModule } from './app.module'; | |
describe('Rate Limit', () => { | |
let app: INestApplication; | |
beforeAll(async () => { | |
const moduleFixture: TestingModule = await Test.createTestingModule({ | |
imports: [AppModule], | |
}).compile(); | |
app = moduleFixture.createNestApplication(); | |
await app.init(); | |
}); | |
it('deve retornar status 429, solicitações demais após exceder o limite de taxa', async () => { | |
const server = app.getHttpServer(); | |
const endpoint = '/'; | |
for (let i = 0; i < 10; i++) { | |
await request(server).get(endpoint).expect(200); | |
} | |
const response = await request(server).get('/'); | |
expect(response.status).toBe(429); | |
expect(response.body.statusCode).toBe(429); | |
expect(response.body.message).toBe('ThrottlerException: Too Many Requests'); | |
}); | |
afterAll(async () => { | |
await app.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment