Files
nxxmdata/government-backend/test-path-duplication-fix.js
2025-10-17 17:29:11 +08:00

116 lines
4.2 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');
// 测试路径重复修复效果
async function testPathDuplicationFix() {
console.log('🔍 测试路径重复修复效果...\n');
const testCases = [
{
name: '正确路径 - 部门列表',
url: 'https://ad.ningmuyun.com/api/government/departments',
expected: '应该返回200状态码'
},
{
name: '重复路径 - 部门列表',
url: 'https://ad.ningmuyun.com/api/government/government/departments',
expected: '应该返回404错误路径不存在'
},
{
name: '正确路径 - 数据中心',
url: 'https://ad.ningmuyun.com/api/government/data-center',
expected: '应该返回200状态码'
},
{
name: '重复路径 - 数据中心',
url: 'https://ad.ningmuyun.com/api/government/government/data-center',
expected: '应该返回404错误路径不存在'
},
{
name: '正确路径 - 市场价格',
url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef',
expected: '应该返回200状态码'
},
{
name: '重复路径 - 市场价格',
url: 'https://ad.ningmuyun.com/api/government/government/market-price?type=beef',
expected: '应该返回404错误路径不存在'
},
{
name: '正确路径 - 养殖类型',
url: 'https://ad.ningmuyun.com/api/government/farm-types',
expected: '应该返回200状态码'
},
{
name: '重复路径 - 养殖类型',
url: 'https://ad.ningmuyun.com/api/government/government/farm-types',
expected: '应该返回404错误路径不存在'
}
];
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(` ✅ 正常: 路径不存在(符合预期)`);
}
} 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(' 问题: proxy_pass配置导致路径重复');
console.log(' 修复前: proxy_pass http://localhost:5352/api/government/;');
console.log(' 修复后: proxy_pass http://localhost:5352/;');
console.log('');
console.log('🔄 路径映射:');
console.log(' /api/government/departments → http://localhost:5352/api/government/departments');
console.log(' /api/government/data-center → http://localhost:5352/api/government/data-center');
console.log(' /api/government/market-price → http://localhost:5352/api/government/market-price');
console.log('');
console.log('🔄 现在需要重启nginx使配置生效:');
console.log(' sudo nginx -t # 检查配置语法');
console.log(' sudo systemctl reload nginx # 重新加载配置');
}
// 运行测试
testPathDuplicationFix().catch(console.error);