|
@@ -1,17 +1,15 @@
|
|
|
import { faker } from '@faker-js/faker';
|
|
import { faker } from '@faker-js/faker';
|
|
|
|
|
|
|
|
-import { consumePoints } from './consume-points';
|
|
|
|
|
-
|
|
|
|
|
-const testRateLimitErrorWhenExceedingMaxRequests = async(method: string, key: string, maxRequests: number): Promise<void> => {
|
|
|
|
|
|
|
+const testRateLimitErrorWhenExceedingMaxRequests = async(method: string, endpoint: string, key: string, maxRequests: number): Promise<void> => {
|
|
|
// dynamic import is used because rateLimiterMongo needs to be initialized after connecting to DB
|
|
// dynamic import is used because rateLimiterMongo needs to be initialized after connecting to DB
|
|
|
// Issue: https://github.com/animir/node-rate-limiter-flexible/issues/216
|
|
// Issue: https://github.com/animir/node-rate-limiter-flexible/issues/216
|
|
|
- const { rateLimiter } = await import('./rate-limiter-mongo-client');
|
|
|
|
|
|
|
+ const { consumePoints } = await import('./consume-points');
|
|
|
let count = 0;
|
|
let count = 0;
|
|
|
try {
|
|
try {
|
|
|
for (let i = 1; i <= maxRequests + 1; i++) {
|
|
for (let i = 1; i <= maxRequests + 1; i++) {
|
|
|
count += 1;
|
|
count += 1;
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
- const res = await consumePoints(rateLimiter, method, key, { method, maxRequests });
|
|
|
|
|
|
|
+ const res = await consumePoints(method, endpoint, key, { method, maxRequests });
|
|
|
if (count === maxRequests) {
|
|
if (count === maxRequests) {
|
|
|
// Expect consumedPoints to be equal to maxRequest when maxRequest is reached
|
|
// Expect consumedPoints to be equal to maxRequest when maxRequest is reached
|
|
|
expect(res?.consumedPoints).toBe(maxRequests);
|
|
expect(res?.consumedPoints).toBe(maxRequests);
|
|
@@ -36,27 +34,30 @@ describe('consume-points.ts', async() => {
|
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: 1)', async() => {
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: 1)', async() => {
|
|
|
// setup
|
|
// setup
|
|
|
const method = 'GET';
|
|
const method = 'GET';
|
|
|
|
|
+ const endpoint = '/_api/v3/test-1';
|
|
|
const key = 'test-key-1';
|
|
const key = 'test-key-1';
|
|
|
const maxRequests = 1;
|
|
const maxRequests = 1;
|
|
|
|
|
|
|
|
- await testRateLimitErrorWhenExceedingMaxRequests(method, key, maxRequests);
|
|
|
|
|
|
|
+ await testRateLimitErrorWhenExceedingMaxRequests(method, endpoint, key, maxRequests);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: 500)', async() => {
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: 500)', async() => {
|
|
|
// setup
|
|
// setup
|
|
|
const method = 'GET';
|
|
const method = 'GET';
|
|
|
|
|
+ const endpoint = '/_api/v3/test-2';
|
|
|
const key = 'test-key-2';
|
|
const key = 'test-key-2';
|
|
|
const maxRequests = 500;
|
|
const maxRequests = 500;
|
|
|
|
|
|
|
|
- await testRateLimitErrorWhenExceedingMaxRequests(method, key, maxRequests);
|
|
|
|
|
|
|
+ await testRateLimitErrorWhenExceedingMaxRequests(method, endpoint, key, maxRequests);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: {random integer between 1 and 1000})', async() => {
|
|
it('Should trigger a rate limit error when maxRequest is exceeded (maxRequest: {random integer between 1 and 1000})', async() => {
|
|
|
// setup
|
|
// setup
|
|
|
const method = 'GET';
|
|
const method = 'GET';
|
|
|
|
|
+ const endpoint = '/_api/v3/test-3';
|
|
|
const key = 'test-key-3';
|
|
const key = 'test-key-3';
|
|
|
const maxRequests = faker.number.int({ min: 1, max: 1000 });
|
|
const maxRequests = faker.number.int({ min: 1, max: 1000 });
|
|
|
|
|
|
|
|
- await testRateLimitErrorWhenExceedingMaxRequests(method, key, maxRequests);
|
|
|
|
|
|
|
+ await testRateLimitErrorWhenExceedingMaxRequests(method, endpoint, key, maxRequests);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|