Enhance WebSocket testing script to include session ID handling

This commit updates the `testWebSocket.js` script to extract and utilize the session ID from the HTTP polling response. The WebSocket upgrade request is modified to include the session ID when available, improving the accuracy of the upgrade tests. Additionally, detailed logging for response bodies is added to aid in diagnosing potential issues during the WebSocket upgrade process.
This commit is contained in:
Torsten Schulz (local)
2025-11-16 00:00:42 +01:00
parent 3e5ddd8a05
commit bbdc923950

View File

@@ -33,12 +33,14 @@ const pollingReq = http.get(pollingUrl, (res) => {
console.log(` Response: ${data.substring(0, 200)}...`);
// Versuche Session-ID zu extrahieren
let sessionId = null;
try {
const jsonMatch = data.match(/\{.*\}/);
if (jsonMatch) {
const json = JSON.parse(jsonMatch[0]);
if (json.sid) {
console.log(` Session ID: ${json.sid}`);
sessionId = json.sid;
console.log(` Session ID: ${sessionId}`);
}
}
} catch (e) {
@@ -47,7 +49,7 @@ const pollingReq = http.get(pollingUrl, (res) => {
// Test 2: WebSocket-Upgrade
console.log('\n2⃣ Teste WebSocket-Upgrade...');
testWebSocketUpgrade(host, port);
testWebSocketUpgrade(host, port, sessionId);
} else {
console.error(` ❌ HTTP-Polling fehlgeschlagen: Status ${res.statusCode}`);
process.exit(1);
@@ -71,14 +73,17 @@ pollingReq.setTimeout(5000, () => {
process.exit(1);
});
function testWebSocketUpgrade(host, port) {
function testWebSocketUpgrade(host, port, sessionId) {
// WebSocket-Upgrade-Request
const wsKey = Buffer.from(Math.random().toString()).toString('base64').substring(0, 16);
const path = sessionId
? `/socket.io/?EIO=4&transport=websocket&sid=${sessionId}`
: '/socket.io/?EIO=4&transport=websocket';
const options = {
hostname: host,
port: port,
path: '/socket.io/?EIO=4&transport=websocket',
path: path,
method: 'GET',
headers: {
'Upgrade': 'websocket',
@@ -100,8 +105,22 @@ function testWebSocketUpgrade(host, port) {
} else {
console.log(` ⚠️ WebSocket-Upgrade: Status ${res.statusCode} (erwartet: 101)`);
console.log(` → Server antwortet, aber Upgrade nicht erfolgreich`);
console.log(` → Möglicherweise wird nur HTTP-Polling unterstützt`);
process.exit(0);
// Lese Response-Body für weitere Informationen
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (body) {
console.log(` Response Body: ${body.substring(0, 200)}`);
}
if (res.statusCode === 400) {
console.log(` → Socket.IO lehnt den Request ab (Bad Request)`);
console.log(` → Möglicherweise fehlt die Session-ID oder sie ist ungültig`);
}
process.exit(0);
});
}
});