const http = require('http'); // 测试函数 function testRoute(path, description) { return new Promise((resolve, reject) => { const options = { hostname: 'localhost', port: 5353, path: path, method: 'GET', headers: { 'Authorization': 'Bearer mock-jwt-token-test', 'Content-Type': 'application/json' } }; const req = http.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { resolve({ path: path, description: description, statusCode: res.statusCode, headers: res.headers, body: data }); }); }); req.on('error', (error) => { reject({ path: path, description: description, error: error.message }); }); req.end(); }); } // 运行所有测试 async function runTests() { console.log('开始测试路由...\n'); try { // 测试健康检查路由 const healthResult = await testRoute('/health', '健康检查'); console.log(`${healthResult.description} - 状态码: ${healthResult.statusCode}`); console.log(`响应: ${healthResult.body}\n`); // 测试测试路由 const testResult = await testRoute('/api/test/test', '测试路由'); console.log(`${testResult.description} - 状态码: ${testResult.statusCode}`); console.log(`响应: ${testResult.body}\n`); // 测试slaughter路由 const slaughterResult = await testRoute('/api/slaughter/slaughterhouses', 'Slaughter路由'); console.log(`${slaughterResult.description} - 状态码: ${slaughterResult.statusCode}`); console.log(`响应: ${slaughterResult.body}\n`); // 测试不存在的路由 const notFoundResult = await testRoute('/api/not-exist', '不存在的路由'); console.log(`${notFoundResult.description} - 状态码: ${notFoundResult.statusCode}`); console.log(`响应: ${notFoundResult.body}\n`); } catch (error) { console.error('测试失败:', error); } } // 等待一会儿再运行测试,给服务器启动时间 sleep(2000).then(() => { runTests(); }); // 简单的sleep函数 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }