forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiRateLimit.test.js
More file actions
97 lines (81 loc) · 3.07 KB
/
Copy pathapiRateLimit.test.js
File metadata and controls
97 lines (81 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const express = require('express');
const request = require('supertest');
const { createCacheMock } = require('./helpers/cacheMock');
const mockHelper = createCacheMock();
const { reset } = mockHelper;
jest.mock('../src/services/cache', () => mockHelper.cacheMock);
jest.mock('../src/logger', () => ({
info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn(),
}));
const mockGetPrice = jest.fn();
jest.mock('../src/services/priceOracle', () => ({
getPrice: mockGetPrice,
fetchFreshPrice: jest.fn(),
}));
const buildRateLimit = require('../src/middleware/rateLimit');
const pricesRouter = require('../src/routes/prices');
const { errorHandler } = require('../src/middleware/errorHandler');
function priceResponse() {
return {
asset_code: 'XLM',
issuer: null,
price_usd: 0.12,
source: 'coingecko',
fetched_at: '2026-06-25T00:00:00.000Z',
is_stale: false,
stale_warning: null,
sources_attempted: ['coingecko'],
redis_unavailable: false,
};
}
function buildApiApp({ globalMax = 100, globalWindowSeconds = 60 } = {}) {
const app = express();
app.use(express.json());
app.use('/api/v1', buildRateLimit({
windowSeconds: globalWindowSeconds,
max: globalMax,
keyPrefix: 'api',
}));
app.use('/api/v1', pricesRouter);
app.use(errorHandler);
return app;
}
beforeEach(() => {
reset();
mockGetPrice.mockReset();
mockGetPrice.mockResolvedValue(priceResponse());
});
describe('API rate limiting integration', () => {
test('global limit returns 429 after max requests per IP', async () => {
const app = buildApiApp({ globalMax: 2, globalWindowSeconds: 60 });
await request(app).get('/api/v1/prices/XLM');
await request(app).get('/api/v1/prices/XLM');
const blocked = await request(app).get('/api/v1/prices/XLM');
expect(blocked.status).toBe(429);
expect(blocked.body.error.code).toBe('RATE_LIMITED');
expect(blocked.body.error.details.retry_after_seconds).toBeGreaterThan(0);
expect(blocked.headers['x-ratelimit-limit']).toBe('2');
expect(blocked.headers['retry-after']).toBeDefined();
});
test('prices routes enforce stricter 30 req/min limit', async () => {
const app = buildApiApp({ globalMax: 100, globalWindowSeconds: 60 });
for (let i = 0; i < 30; i += 1) {
const res = await request(app).get('/api/v1/prices/XLM');
expect(res.status).toBe(200);
expect(res.headers['x-ratelimit-limit']).toBe('30');
}
const blocked = await request(app).get('/api/v1/prices/XLM');
expect(blocked.status).toBe(429);
expect(blocked.body.error.code).toBe('RATE_LIMITED');
expect(blocked.headers['x-ratelimit-limit']).toBe('30');
});
test('successful responses include rate-limit headers', async () => {
const app = buildApiApp({ globalMax: 100, globalWindowSeconds: 60 });
const res = await request(app).get('/api/v1/prices/XLM');
expect(res.status).toBe(200);
expect(res.headers['x-ratelimit-limit']).toBe('30');
expect(res.headers['x-ratelimit-remaining']).toBeDefined();
expect(res.headers['x-ratelimit-reset']).toBeDefined();
});
});