Files
nxxmdata/government-backend/test-after-restart.js
2025-10-17 17:29:11 +08:00

117 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require('axios');
// 测试重启后的API接口
async function testAfterRestart() {
console.log('🔍 测试nginx重启后的API接口...\n');
const testCases = [
{
name: '登录接口测试',
url: 'https://ad.ningmuyun.com/api/government/auth/login',
method: 'POST',
data: { username: 'admin', password: 'admin123' },
expected: '应该返回401认证错误接口正常'
},
{
name: '部门列表接口测试',
url: 'https://ad.ningmuyun.com/api/government/departments',
method: 'GET',
data: null,
expected: '应该返回200或401'
},
{
name: '数据中心接口测试',
url: 'https://ad.ningmuyun.com/api/government/data-center',
method: 'GET',
data: null,
expected: '应该返回200或401'
},
{
name: '市场价格接口测试',
url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef',
method: 'GET',
data: null,
expected: '应该返回200或401'
},
{
name: '岗位列表接口测试',
url: 'https://ad.ningmuyun.com/api/government/positions',
method: 'GET',
data: null,
expected: '应该返回200或401'
}
];
for (const test of testCases) {
console.log(`📋 ${test.name}`);
console.log(` URL: ${test.url}`);
console.log(` 预期: ${test.expected}`);
try {
const config = {
method: test.method,
url: test.url,
headers: {
'Content-Type': 'application/json'
},
timeout: 10000,
validateStatus: function (status) {
return status < 500; // 接受所有小于500的状态码
}
};
if (test.data) {
config.data = test.data;
}
const response = await axios(config);
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(` 📊 数据类型: 对象`);
}
} 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('📝 当前nginx配置:');
console.log(' location ^~ /api/government/ {');
console.log(' proxy_pass http://localhost:5352/api/government/;');
console.log(' }');
console.log('');
console.log('🔄 路径映射:');
console.log(' /api/government/departments → http://localhost:5352/api/government/departments');
console.log(' /api/government/positions → http://localhost:5352/api/government/positions');
}
// 运行测试
testAfterRestart().catch(console.error);