98 lines
3.2 KiB
JavaScript
98 lines
3.2 KiB
JavaScript
|
|
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);
|