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

95 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-10-17 17:29:11 +08:00
const axios = require('axios');
// 测试修复后的API接口
async function testFixedAPIs() {
console.log('🔍 测试修复后的政府端API接口...\n');
const testCases = [
{
name: '部门列表接口',
url: 'https://ad.ningmuyun.com/api/government/departments',
expected: '应该返回200状态码和部门数据'
},
{
name: '数据中心接口',
url: 'https://ad.ningmuyun.com/api/government/data-center',
expected: '应该返回200状态码和统计数据'
},
{
name: '市场价格接口',
url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef',
expected: '应该返回200状态码和价格数据'
},
{
name: '岗位列表接口',
url: 'https://ad.ningmuyun.com/api/government/positions',
expected: '应该返回200状态码和岗位数据'
},
{
name: '养殖户列表接口',
url: 'https://ad.ningmuyun.com/api/government/farmers',
expected: '应该返回200状态码和养殖户数据'
}
];
for (const test of testCases) {
console.log(`📋 ${test.name}`);
console.log(` URL: ${test.url}`);
console.log(` 预期: ${test.expected}`);
try {
const response = await axios.get(test.url, {
timeout: 10000,
validateStatus: function (status) {
return status < 500; // 接受所有小于500的状态码
}
});
console.log(` ✅ 状态码: ${response.status}`);
if (response.status === 200) {
console.log(` ✅ 成功: 接口正常工作`);
if (Array.isArray(response.data)) {
console.log(` 📊 数据量: ${response.data.length} 条记录`);
} else if (response.data && typeof response.data === 'object') {
console.log(` 📊 数据类型: 对象,包含 ${Object.keys(response.data).length} 个字段`);
}
} else if (response.status === 401) {
console.log(` ✅ 正常: 需要认证(接口路径正确)`);
} else if (response.status === 404) {
console.log(` ❌ 错误: 接口不存在`);
}
} catch (error) {
if (error.response) {
console.log(` ❌ 状态码: ${error.response.status}`);
console.log(` ❌ 错误: ${error.response.statusText}`);
if (error.response.status === 404) {
console.log(` 🔍 分析: nginx配置可能还有问题`);
}
} 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(' 问题: location规则缺少注释符号导致被注释掉');
console.log(' 修复: 添加了正确的注释和location规则');
console.log('');
console.log('🔄 现在需要重启nginx使配置生效:');
console.log(' sudo nginx -t # 检查配置语法');
console.log(' sudo systemctl reload nginx # 重新加载配置');
}
// 运行测试
testFixedAPIs().catch(console.error);