The API rate limiter in src/config/rate-limit.ts uses express-rate-limit and is skipped when process.env.NODE_ENV === "test". To thoroughly exercise the 429 response path, RateLimit-* headers, and the custom message body, we need a dedicated integration/unit test file (e.g., src/__tests__/rate-limit.test.ts) that instantiates a minimal Express application with the rate limiter (overriding the test skip condition locally) and sends multiple requests to trigger the 429 rate limit.
Add a test file src/__tests__/rate-limit.test.ts implementing the test suite.
import express from 'express';
import request from 'supertest';
import rateLimit from 'express-rate-limit';
describe('API Rate Limiter 429 Response', () => {
let app: express.Application;
beforeEach(() => {
app = express();
// Create a rate limiter instance with strict limits for testing the 429 path
const testLimiter = rateLimit({
windowMs: 1000, // 1 second
max: 2, // limit each IP to 2 requests per windowMs
standardHeaders: true,
legacyHeaders: false,
message: {
success: false,
message: 'Too many requests, please try again later.',
},
// Explicitly NOT skipping in tests for this specific test app
skip: () => false,
});
app.use(testLimiter);
app.get('/test', (req, res) => {
res.status(200).json({ success: true, message: 'OK' });
});
});
it('should return 429 status, RateLimit headers, and custom message after exceeding limit', async () => {
// Request 1: Should succeed
const res1 = await request(app).get('/test');
expect(res1.status).toBe(200);
expect(res1.headers['ratelimit-limit']).toBe('2');
expect(res1.headers['ratelimit-remaining']).toBe('1');
// Request 2: Should succeed
const res2 = await request(app).get('/test');
expect(res2.status).toBe(200);
expect(res2.headers['ratelimit-remaining']).toBe('0');
// Request 3: Should hit rate limit (429)
const res3 = await request(app).get('/test');
expect(res3.status).toBe(429);
expect(res3.headers['ratelimit-remaining']).toBe('0');
expect(res3.body).toEqual({
success: false,
message: 'Too many requests, please try again later.',
});
});
});Run the test suite using Jest / Supertest: ```bash npm test src/tests/rate-limit.test.ts ```
Submitted by Aditya Waghamare
💰 Payout Address (Base L2 / EVM): 0xb61dBcdBc3407F71EaCb64D4CBFAcf9FFfe2415C