Generating commit message...
This commit is contained in:
103
backend/test-api.js
Normal file
103
backend/test-api.js
Normal file
@@ -0,0 +1,103 @@
|
||||
const http = require('http');
|
||||
|
||||
// 测试健康检查接口
|
||||
function testHealthCheck() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/health',
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
let data = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
console.log('✅ 健康检查接口测试成功');
|
||||
console.log('状态码:', res.statusCode);
|
||||
console.log('响应:', JSON.parse(data));
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
console.error('❌ 健康检查接口测试失败:', error.message);
|
||||
reject(error);
|
||||
});
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// 测试认证接口
|
||||
function testAuthAPI() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const postData = JSON.stringify({
|
||||
username: 'testuser',
|
||||
password: 'testpass123'
|
||||
});
|
||||
|
||||
const options = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/api/v1/auth/login',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(postData)
|
||||
}
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
let data = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
console.log('\n✅ 认证接口测试成功');
|
||||
console.log('状态码:', res.statusCode);
|
||||
try {
|
||||
const response = JSON.parse(data);
|
||||
console.log('响应:', response);
|
||||
} catch (e) {
|
||||
console.log('原始响应:', data);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
console.error('❌ 认证接口测试失败:', error.message);
|
||||
reject(error);
|
||||
});
|
||||
|
||||
req.write(postData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function runTests() {
|
||||
console.log('🚀 开始测试API接口...\n');
|
||||
|
||||
try {
|
||||
await testHealthCheck();
|
||||
await testAuthAPI();
|
||||
console.log('\n🎉 所有测试完成!');
|
||||
} catch (error) {
|
||||
console.error('\n❌ 测试失败:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果直接运行此文件,则执行测试
|
||||
if (require.main === module) {
|
||||
runTests();
|
||||
}
|
||||
|
||||
module.exports = { runTests };
|
||||
Reference in New Issue
Block a user