74 lines
2.7 KiB
JavaScript
74 lines
2.7 KiB
JavaScript
|
|
/**
|
|||
|
|
* API访问测试脚本
|
|||
|
|
* @file test-api-access.js
|
|||
|
|
* @description 测试API接口是否正常访问
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const axios = require('axios');
|
|||
|
|
|
|||
|
|
async function testApiAccess() {
|
|||
|
|
console.log('🔍 测试API访问...\n');
|
|||
|
|
|
|||
|
|
const baseUrl = 'http://localhost:5350';
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 1. 测试服务器根路径
|
|||
|
|
console.log('1. 测试服务器根路径...');
|
|||
|
|
const rootResponse = await axios.get(`${baseUrl}/`);
|
|||
|
|
console.log('✅ 服务器根路径正常:', rootResponse.data);
|
|||
|
|
|
|||
|
|
// 2. 测试API文档访问
|
|||
|
|
console.log('\n2. 测试API文档访问...');
|
|||
|
|
const docsResponse = await axios.get(`${baseUrl}/api-docs/`);
|
|||
|
|
console.log('✅ API文档页面正常访问');
|
|||
|
|
|
|||
|
|
// 3. 测试Swagger JSON
|
|||
|
|
console.log('\n3. 测试Swagger JSON...');
|
|||
|
|
const swaggerResponse = await axios.get(`${baseUrl}/api-docs/swagger.json`);
|
|||
|
|
console.log('✅ Swagger JSON正常:', swaggerResponse.data.info.title);
|
|||
|
|
|
|||
|
|
// 4. 测试智能耳标预警API
|
|||
|
|
console.log('\n4. 测试智能耳标预警API...');
|
|||
|
|
const eartagStatsResponse = await axios.get(`${baseUrl}/api/smart-alerts/public/eartag/stats`);
|
|||
|
|
console.log('✅ 智能耳标预警统计API正常:', eartagStatsResponse.data.success);
|
|||
|
|
|
|||
|
|
// 5. 测试智能项圈预警API
|
|||
|
|
console.log('\n5. 测试智能项圈预警API...');
|
|||
|
|
const collarStatsResponse = await axios.get(`${baseUrl}/api/smart-alerts/public/collar/stats`);
|
|||
|
|
console.log('✅ 智能项圈预警统计API正常:', collarStatsResponse.data.success);
|
|||
|
|
|
|||
|
|
// 6. 检查Swagger JSON中的路径
|
|||
|
|
console.log('\n6. 检查Swagger JSON中的路径...');
|
|||
|
|
const paths = Object.keys(swaggerResponse.data.paths || {});
|
|||
|
|
const smartAlertPaths = paths.filter(path => path.includes('/smart-alerts/public'));
|
|||
|
|
console.log('📋 找到的智能预警API路径:');
|
|||
|
|
smartAlertPaths.forEach(path => {
|
|||
|
|
console.log(` - ${path}`);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (smartAlertPaths.length === 0) {
|
|||
|
|
console.log('❌ 未找到智能预警API路径,可能是Swagger配置问题');
|
|||
|
|
} else {
|
|||
|
|
console.log(`✅ 找到 ${smartAlertPaths.length} 个智能预警API路径`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 测试失败:', error.message);
|
|||
|
|
|
|||
|
|
if (error.code === 'ECONNREFUSED') {
|
|||
|
|
console.log('💡 建议: 请确保服务器已启动 (npm start)');
|
|||
|
|
} else if (error.response) {
|
|||
|
|
console.log('💡 建议: 检查API路径和端口配置');
|
|||
|
|
console.log(' 状态码:', error.response.status);
|
|||
|
|
console.log(' 响应:', error.response.data);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果直接运行此脚本
|
|||
|
|
if (require.main === module) {
|
|||
|
|
testApiAccess().catch(console.error);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = { testApiAccess };
|