备份:修复前完整项目快照 2026-04-19
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
// 快速测试脚本 - 验证API端点
|
||||
const http = require('http');
|
||||
|
||||
const BASE_URL = 'http://localhost:3000';
|
||||
const TEST_CUSTOMER = {
|
||||
name: '快速测试客户',
|
||||
email: 'quick-test@example.com',
|
||||
phone: '12345678901',
|
||||
company: '测试公司'
|
||||
};
|
||||
|
||||
async function testEndpoint(method, path, data = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path,
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
let responseData = '';
|
||||
res.on('data', (chunk) => {
|
||||
responseData += chunk;
|
||||
});
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const parsed = JSON.parse(responseData);
|
||||
resolve({
|
||||
statusCode: res.statusCode,
|
||||
data: parsed
|
||||
});
|
||||
} catch (e) {
|
||||
resolve({
|
||||
statusCode: res.statusCode,
|
||||
data: responseData
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
if (data) {
|
||||
req.write(JSON.stringify(data));
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function runTests() {
|
||||
console.log('=== 客户管理API快速测试 ===\n');
|
||||
|
||||
try {
|
||||
// 1. 测试健康检查
|
||||
console.log('1. 测试健康检查...');
|
||||
const health = await testEndpoint('GET', '/health');
|
||||
console.log(` 状态码: ${health.statusCode}, 响应: ${JSON.stringify(health.data)}\n`);
|
||||
|
||||
// 2. 测试获取客户列表
|
||||
console.log('2. 测试获取客户列表...');
|
||||
const list = await testEndpoint('GET', '/api/customers?limit=2');
|
||||
console.log(` 状态码: ${list.statusCode}, 获取到 ${list.data.data?.length || 0} 个客户\n`);
|
||||
|
||||
// 3. 测试创建客户
|
||||
console.log('3. 测试创建客户...');
|
||||
const create = await testEndpoint('POST', '/api/customers', TEST_CUSTOMER);
|
||||
console.log(` 状态码: ${create.statusCode}, 客户ID: ${create.data.data?.id || 'N/A'}`);
|
||||
|
||||
let customerId = create.data.data?.id;
|
||||
|
||||
if (customerId) {
|
||||
// 4. 测试获取单个客户
|
||||
console.log(`\n4. 测试获取单个客户 (ID=${customerId})...`);
|
||||
const getOne = await testEndpoint('GET', `/api/customers/${customerId}`);
|
||||
console.log(` 状态码: ${getOne.statusCode}, 客户名称: ${getOne.data.data?.name || 'N/A'}\n`);
|
||||
|
||||
// 5. 测试更新客户
|
||||
console.log('5. 测试更新客户...');
|
||||
const update = await testEndpoint('PUT', `/api/customers/${customerId}`, {
|
||||
phone: '13888888888',
|
||||
company: '更新后的公司'
|
||||
});
|
||||
console.log(` 状态码: ${update.statusCode}, 更新成功: ${update.data.success || false}\n`);
|
||||
|
||||
// 6. 测试获取客户联系人
|
||||
console.log('6. 测试获取客户联系人...');
|
||||
const contacts = await testEndpoint('GET', `/api/customers/${customerId}/contacts`);
|
||||
console.log(` 状态码: ${contacts.statusCode}, 联系人数量: ${contacts.data.data?.length || 0}\n`);
|
||||
|
||||
// 7. 测试删除客户
|
||||
console.log('7. 测试删除客户...');
|
||||
const del = await testEndpoint('DELETE', `/api/customers/${customerId}`);
|
||||
console.log(` 状态码: ${del.statusCode}, 删除成功: ${del.data.success || false}\n`);
|
||||
}
|
||||
|
||||
// 8. 测试搜索功能
|
||||
console.log('8. 测试搜索功能 (搜索"张")...');
|
||||
const search = await testEndpoint('GET', '/api/customers?search=张');
|
||||
console.log(` 状态码: ${search.statusCode}, 搜索结果数量: ${search.data.data?.length || 0}\n`);
|
||||
|
||||
// 9. 测试验证错误
|
||||
console.log('9. 测试验证错误 (无效邮箱)...');
|
||||
const invalid = await testEndpoint('POST', '/api/customers', {
|
||||
name: '无效客户',
|
||||
email: 'invalid-email'
|
||||
});
|
||||
console.log(` 状态码: ${invalid.statusCode}, 验证错误: ${invalid.statusCode === 400}\n`);
|
||||
|
||||
console.log('=== 测试完成 ===');
|
||||
console.log('所有端点基本功能验证完成。');
|
||||
console.log('如需完整测试,请运行: ./test-api.sh');
|
||||
|
||||
} catch (error) {
|
||||
console.error('测试过程中发生错误:', error.message);
|
||||
console.log('请确保服务器正在运行: npm run dev');
|
||||
}
|
||||
}
|
||||
|
||||
// 检查服务器是否运行
|
||||
testEndpoint('GET', '/health')
|
||||
.then(() => {
|
||||
runTests();
|
||||
})
|
||||
.catch(() => {
|
||||
console.log('服务器未运行或无法连接。请先启动服务器:');
|
||||
console.log('1. cd /opt/company-finance-system/backend');
|
||||
console.log('2. npm run dev');
|
||||
console.log('\n然后在另一个终端运行此测试:');
|
||||
console.log('node quick-test.js');
|
||||
});
|
||||
Reference in New Issue
Block a user