spt-launcher/test-auth-deflate.js

89 lines
2.3 KiB
JavaScript

const https = require('https');
const zlib = require('zlib');
const agent = new https.Agent({ rejectUnauthorized: false });
const makeRequest = (path, data, cookie) => {
return new Promise((resolve, reject) => {
const payload = JSON.stringify(data);
const deflated = zlib.deflateSync(payload);
const headers = {
'Content-Type': 'application/json',
'Accept-Encoding': 'deflate',
'Expect': '100-continue'
};
if (cookie) {
headers['Cookie'] = cookie;
}
const req = https.request({
hostname: '127.0.0.1',
port: 6969,
path: path,
method: 'POST',
headers: headers,
agent
}, (res) => {
const chunks = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => {
const buffer = Buffer.concat(chunks);
let body = '';
if (buffer.length > 0) {
try {
body = zlib.inflateSync(buffer).toString();
} catch (e) {
body = buffer.toString();
}
}
resolve({ status: res.statusCode, body });
});
});
req.on('error', reject);
req.write(deflated);
req.end();
});
};
async function testFlow() {
const user = 'testuser' + Math.floor(Math.random() * 1000);
const pass = 'password123';
console.log(`Registering new user: ${user}`);
const regRes = await makeRequest('/launcher/profile/register', {
username: user,
password: pass,
edition: 'Standard'
});
console.log("Registration Response:", regRes.status, regRes.body);
if (regRes.status !== 200 || regRes.body === 'FAILED') {
console.log("Registration failed, stopping.");
return;
}
console.log("\nLogging in with new user...");
const loginRes = await makeRequest('/launcher/profile/login', {
username: user,
password: pass
});
console.log("Login Response:", loginRes.status, loginRes.body);
if (loginRes.status !== 200 || loginRes.body === 'FAILED') {
console.log("Login failed, stopping.");
return;
}
const sessionId = loginRes.body;
console.log("\nFetching profile info...");
const infoRes = await makeRequest('/launcher/profile/info', {
username: user
}, `PHPSESSID=${sessionId}`);
console.log("Info Response:", infoRes.status, infoRes.body);
}
testFlow().catch(console.error);