106 lines
3.5 KiB
JavaScript

// test-deployed.js
// 用于测试部署到阿里云函数计算的服务
const https = require('https');
const http = require('http');
// 替换为你的函数计算触发器 URL
const FC_URL = 'https://get-sts-token-qltjykaafr.cn-shanghai.fcapp.run';
const testData = {
steamId: '76561198000000000'
};
function request(url, data) {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const isHttps = urlObj.protocol === 'https:';
const client = isHttps ? https : http;
const body = JSON.stringify(data);
const options = {
hostname: urlObj.hostname,
port: urlObj.port || (isHttps ? 443 : 80),
path: urlObj.pathname + urlObj.search,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
'Date': new Date().toUTCString() // 添加必需的 Date 头
}
};
const req = client.request(options, (res) => {
let responseBody = '';
res.on('data', chunk => responseBody += chunk);
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
body: responseBody
});
});
});
req.on('error', reject);
req.write(body);
req.end();
});
}
async function runTests() {
console.log('=== 开始测试阿里云函数计算服务 ===\n');
console.log(`目标 URL: ${FC_URL}`);
console.log(`测试数据: ${JSON.stringify(testData)}\n`);
try {
// 测试 1: 正常请求
console.log('--- 测试 1: 正常 POST 请求 ---');
const res1 = await request(FC_URL, testData);
console.log('状态码:', res1.statusCode);
console.log('响应体:', res1.body);
if (res1.statusCode === 200) {
const json = JSON.parse(res1.body);
const required = ['accessKeyId', 'accessKeySecret', 'securityToken', 'objectKey', 'expiresIn', 'bucket', 'endpoint'];
const missing = required.filter(k => !json[k]);
if (missing.length) {
console.log('❌ 缺少字段:', missing.join(', '));
} else {
console.log('✅ 测试 1 通过\n');
}
} else {
console.log('❌ 测试 1 失败: 状态码不是 200\n');
}
// 测试 2: 无效 SteamID
console.log('--- 测试 2: 无效 SteamID ---');
const res2 = await request(FC_URL, { steamId: 'invalid' });
console.log('状态码:', res2.statusCode);
console.log('响应体:', res2.body);
if (res2.statusCode === 400) {
console.log('✅ 测试 2 通过\n');
} else {
console.log('❌ 测试 2 失败: 应返回 400\n');
}
// 测试 3: 空请求体
console.log('--- 测试 3: 空请求体 ---');
const res3 = await request(FC_URL, {});
console.log('状态码:', res3.statusCode);
console.log('响应体:', res3.body);
if (res3.statusCode === 400) {
console.log('✅ 测试 3 通过\n');
} else {
console.log('❌ 测试 3 失败: 应返回 400\n');
}
console.log('=== 测试完成 ===');
} catch (err) {
console.error('请求失败:', err.message);
process.exitCode = 1;
}
}
runTests();