57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
// Import the built-in HTTP module from Node.js
|
|
// This lets us make HTTP requests without curl or extra packages
|
|
import http from "http";
|
|
|
|
// How many times we want to hit the endpoint
|
|
const TOTAL_REQUESTS = 60;
|
|
|
|
// Counter to keep track of how many requests we've made
|
|
let count = 0;
|
|
|
|
// The URL we are testing
|
|
const url = "http://localhost:8082/matches";
|
|
|
|
// test websocket rate limit
|
|
const testWS = () => {
|
|
for (let i = 0; i < 10; i++) {
|
|
const ws = new WebSocket("ws://localhost:8082/ws");
|
|
ws.onopen = () => console.info(`Socket ${i} opened`);
|
|
ws.onclose = (e) =>
|
|
console.info(`Socket ${i} closed: ${e.code} ${e.reason}`);
|
|
}
|
|
};
|
|
|
|
// This function makes ONE HTTP request
|
|
function makeRequest() {
|
|
count++;
|
|
|
|
// Create an HTTP GET request
|
|
const req = http.get(url, (res) => {
|
|
// res.statusCode is the HTTP response code (200, 404, 500, etc)
|
|
console.info(res.statusCode);
|
|
|
|
// We don't care about the response body, so just discard it
|
|
res.resume();
|
|
|
|
// If we haven't hit our limit yet, make another request
|
|
if (count < TOTAL_REQUESTS) {
|
|
makeRequest();
|
|
}
|
|
});
|
|
|
|
// If something goes wrong (server down, connection refused, etc)
|
|
req.on("error", (err) => {
|
|
console.error("Request failed:", err.message);
|
|
|
|
// Still continue so we always try 60 times
|
|
if (count < TOTAL_REQUESTS) {
|
|
makeRequest();
|
|
}
|
|
});
|
|
|
|
testWS();
|
|
}
|
|
|
|
// Start the first request
|
|
makeRequest();
|