Skip to content

Instantly share code, notes, and snippets.

@alsgu3rra
Created June 19, 2024 01:54
Show Gist options
  • Save alsgu3rra/31ce2f98cae800d7f18c8ecee673ddcd to your computer and use it in GitHub Desktop.
Save alsgu3rra/31ce2f98cae800d7f18c8ecee673ddcd to your computer and use it in GitHub Desktop.
testar rate limiter no jest
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