Files
nxxmdata/government-backend/test-government-apis.js

98 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-10-17 17:29:11 +08:00
const axios = require('axios');
// 测试政府端各种API接口
async function testGovernmentAPIs() {
console.log('🔍 测试政府端API接口...\n');
const testCases = [
{
name: '登录接口测试',
url: 'https://ad.ningmuyun.com/api/government/auth/login',
method: 'POST',
data: { username: 'admin', password: 'admin123' },
expected: '应该返回登录成功或认证错误'
},
{
name: '部门列表接口测试',
url: 'https://ad.ningmuyun.com/api/government/departments',
method: 'GET',
data: null,
expected: '应该返回部门列表或认证错误'
},
{
name: '市场价格接口测试',
url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef',
method: 'GET',
data: null,
expected: '应该返回市场价格数据或认证错误'
},
{
name: '数据中心接口测试',
url: 'https://ad.ningmuyun.com/api/government/data-center',
method: 'GET',
data: null,
expected: '应该返回数据中心数据或认证错误'
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
console.log(` URL: ${testCase.url}`);
console.log(` 方法: ${testCase.method}`);
console.log(` 预期: ${testCase.expected}`);
try {
const config = {
method: testCase.method,
url: testCase.url,
headers: {
'Content-Type': 'application/json'
},
timeout: 10000
};
if (testCase.data) {
config.data = testCase.data;
}
const response = await axios(config);
console.log(` ✅ 状态码: ${response.status}`);
console.log(` ✅ 响应数据: ${JSON.stringify(response.data, null, 2).substring(0, 200)}...`);
} catch (error) {
if (error.response) {
console.log(` ❌ 状态码: ${error.response.status}`);
console.log(` ❌ 错误信息: ${JSON.stringify(error.response.data, null, 2)}`);
// 分析错误类型
if (error.response.status === 404) {
console.log(` 🔍 分析: 404错误可能是路径映射问题`);
} else if (error.response.status === 401) {
console.log(` 🔍 分析: 401错误需要认证这是正常的`);
} else if (error.response.status === 500) {
console.log(` 🔍 分析: 500错误服务器内部错误`);
}
} else if (error.request) {
console.log(` ❌ 网络错误: ${error.message}`);
} else {
console.log(` ❌ 请求配置错误: ${error.message}`);
}
}
console.log(' ' + '─'.repeat(60));
console.log('');
}
console.log('🎯 测试完成!');
console.log('');
console.log('📝 分析说明:');
console.log(' - 如果所有接口都返回404说明nginx配置有问题');
console.log(' - 如果只有登录接口正常其他返回404说明路径映射有问题');
console.log(' - 如果返回401认证错误说明接口正常但需要登录');
console.log(' - 如果返回500错误说明后端服务有问题');
}
// 运行测试
testGovernmentAPIs().catch(console.error);