-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathratelimit.test.js
69 lines (66 loc) · 2.22 KB
/
ratelimit.test.js
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
const fetch = require("node-fetch");
describe("Rate limit tests", () => {
jest.setTimeout(20 * 1000);
const itr = 10;
test("Rate limit for web enpoints", async () => {
const url = "http://127.0.0.1:8080/";
const limit = 50;
let success = 0;
for (let i = 0; i < itr; i++) {
let promises = [];
for (let j = 0; j < 100; j++) {
promises.push(
new Promise((resolve, reject) => {
fetch(url)
.then((res) => {
resolve(res.status !== 429);
})
.catch((err) => {
console.error(err);
reject();
});
})
);
}
const resp = await Promise.all(promises);
resp.forEach((res) => {
if (res) {
success++;
}
});
}
expect(success).toBeLessThanOrEqual(limit);
});
it("wait for new window", async () => {
await new Promise((resolve) => setTimeout(resolve, 10 * 1000));
});
test("Rate limit for api enpoints", async () => {
const url = "http://127.0.0.1:8080/api/v1/ping";
const limit = 20;
let success = 0;
for (let i = 0; i < itr; i++) {
let promises = [];
for (let j = 0; j < 100; j++) {
promises.push(
new Promise((resolve, reject) => {
fetch(url)
.then((res) => {
resolve(res.status !== 429);
})
.catch((err) => {
console.error(err);
reject();
});
})
);
}
const resp = await Promise.all(promises);
resp.forEach((res) => {
if (res) {
success++;
}
});
}
expect(success).toBeLessThanOrEqual(limit);
});
});