Files
flyer-crawler.projectium.com/src/utils/rateLimit.test.ts
Torben Sorensen 63a0dde0f8
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 16m21s
fix unit tests after frontend tests ran
2026-01-18 02:56:25 -08:00

78 lines
2.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createMockRequest } from '../tests/utils/createMockRequest';
describe('rateLimit utils', () => {
beforeEach(() => {
vi.resetModules();
vi.unstubAllEnvs();
});
afterEach(() => {
vi.unstubAllEnvs();
});
describe('shouldSkipRateLimit', () => {
it('should return false (do not skip) when NODE_ENV is "production"', async () => {
vi.stubEnv('NODE_ENV', 'production');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({ headers: {} });
expect(shouldSkipRateLimit(req)).toBe(false);
});
it('should return true (skip) when NODE_ENV is "development"', async () => {
vi.stubEnv('NODE_ENV', 'development');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({ headers: {} });
expect(shouldSkipRateLimit(req)).toBe(true);
});
it('should return true (skip) when NODE_ENV is "staging"', async () => {
vi.stubEnv('NODE_ENV', 'staging');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({ headers: {} });
expect(shouldSkipRateLimit(req)).toBe(true);
});
it('should return true (skip) when NODE_ENV is "test" and header is missing', async () => {
vi.stubEnv('NODE_ENV', 'test');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({ headers: {} });
expect(shouldSkipRateLimit(req)).toBe(true);
});
it('should return false (do not skip) when NODE_ENV is "test" and header is "true"', async () => {
vi.stubEnv('NODE_ENV', 'test');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({
headers: { 'x-test-rate-limit-enable': 'true' },
});
expect(shouldSkipRateLimit(req)).toBe(false);
});
it('should return true (skip) when NODE_ENV is "test" and header is "false"', async () => {
vi.stubEnv('NODE_ENV', 'test');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({
headers: { 'x-test-rate-limit-enable': 'false' },
});
expect(shouldSkipRateLimit(req)).toBe(true);
});
it('should return false (do not skip) when NODE_ENV is "development" and header is "true"', async () => {
vi.stubEnv('NODE_ENV', 'development');
const { shouldSkipRateLimit } = await import('./rateLimit');
const req = createMockRequest({
headers: { 'x-test-rate-limit-enable': 'true' },
});
expect(shouldSkipRateLimit(req)).toBe(false);
});
});
});