|
1 | | -import { test, expect } from 'bun:test' |
2 | | -import { PRISME_PAGEVIEWS_URL } from '../const' |
3 | | -import { faker } from '@faker-js/faker' |
| 1 | +import { expect } from "@std/expect"; |
| 2 | +import { faker } from "@faker-js/faker"; |
4 | 3 |
|
5 | | -const seed = new Date().getTime() |
6 | | -console.log('faker seed', seed) |
7 | | -faker.seed(seed) |
| 4 | +import { PRISME_PAGEVIEWS_URL } from "../const.ts"; |
| 5 | +import { sleep } from "../utils.ts"; |
8 | 6 |
|
9 | | -test('more than 60 requests per minute are rejected', async () => { |
10 | | - const clientIp = faker.internet.ip() |
| 7 | +const seed = new Date().getTime(); |
| 8 | +console.log("faker seed", seed); |
| 9 | +faker.seed(seed); |
| 10 | + |
| 11 | +Deno.test("more than 60 requests per minute are rejected", async () => { |
| 12 | + const clientIp = faker.internet.ip(); |
11 | 13 |
|
12 | 14 | for (let i = 0; i < 100; i++) { |
13 | 15 | const response = await fetch(PRISME_PAGEVIEWS_URL, { |
14 | | - method: 'POST', |
| 16 | + method: "POST", |
15 | 17 | headers: { |
16 | | - Origin: 'http://mywebsite.localhost', |
17 | | - 'X-Custom-Forwarded-For': clientIp, |
18 | | - 'X-Prisme-Referrer': 'http://mywebsite.localhost' |
19 | | - } |
20 | | - }) |
| 18 | + Origin: "http://mywebsite.localhost", |
| 19 | + "X-Custom-Forwarded-For": clientIp, |
| 20 | + "X-Prisme-Referrer": "http://mywebsite.localhost", |
| 21 | + }, |
| 22 | + }); |
21 | 23 | if (i < 60) { |
22 | | - expect(response.status).toBe(200) |
| 24 | + expect(response.status).toBe(200); |
23 | 25 | } else { |
24 | | - expect(response.status).toBe(429) |
| 26 | + expect(response.status).toBe(429); |
25 | 27 | } |
26 | 28 | } |
27 | 29 |
|
28 | 30 | // Wait a minute. |
29 | | - Bun.sleepSync(60 * 1000) |
| 31 | + await sleep(60 * 1000); |
30 | 32 |
|
31 | 33 | const response = await fetch(PRISME_PAGEVIEWS_URL, { |
32 | | - method: 'POST', |
| 34 | + method: "POST", |
33 | 35 | headers: { |
34 | | - Origin: 'http://mywebsite.localhost', |
35 | | - 'X-Custom-Forwarded-For': clientIp, |
36 | | - 'X-Prisme-Referrer': 'http://mywebsite.localhost' |
37 | | - } |
38 | | - }) |
39 | | - expect(response.status).toBe(200) |
40 | | -}, { timeout: 120 * 1000 }) |
| 36 | + Origin: "http://mywebsite.localhost", |
| 37 | + "X-Custom-Forwarded-For": clientIp, |
| 38 | + "X-Prisme-Referrer": "http://mywebsite.localhost", |
| 39 | + }, |
| 40 | + }); |
| 41 | + expect(response.status).toBe(200); |
| 42 | +}); |
41 | 43 |
|
42 | | -test('requests are rate limited based on X-Custom-Forwarded-For header', async () => { |
| 44 | +Deno.test("requests are rate limited based on X-Custom-Forwarded-For header", async () => { |
43 | 45 | for (let i = 0; i < 100; i++) { |
44 | 46 | const response = await fetch(PRISME_PAGEVIEWS_URL, { |
45 | | - method: 'POST', |
| 47 | + method: "POST", |
46 | 48 | headers: { |
47 | | - Origin: 'http://mywebsite.localhost', |
48 | | - 'X-Custom-Forwarded-For': faker.internet.ip(), |
49 | | - 'X-Prisme-Referrer': 'http://mywebsite.localhost' |
50 | | - } |
51 | | - }) |
52 | | - expect(response.status).toBe(200) |
| 49 | + Origin: "http://mywebsite.localhost", |
| 50 | + "X-Custom-Forwarded-For": faker.internet.ip(), |
| 51 | + "X-Prisme-Referrer": "http://mywebsite.localhost", |
| 52 | + }, |
| 53 | + }); |
| 54 | + expect(response.status).toBe(200); |
53 | 55 | } |
54 | | -}) |
| 56 | +}); |
0 commit comments