Files
jiebanke/scripts/test-api.js
2025-09-03 13:25:08 +08:00

129 lines
4.0 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.

// 结伴客系统API测试脚本
const axios = require('axios');
const BASE_URL = 'https://webapi.jiebanke.com/api/v1';
// 创建axios实例
const api = axios.create({
baseURL: BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});
// 测试用例
async function testAPI() {
console.log('🚀 开始测试结伴客系统API...\n');
try {
// 1. 测试健康检查
console.log('1. 测试健康检查接口...');
const healthResponse = await api.get('/health');
console.log('✅ 健康检查成功:', healthResponse.data);
// 2. 测试用户注册
console.log('\n2. 测试用户注册接口...');
const registerData = {
username: 'testuser',
password: 'test123',
nickname: '测试用户',
email: 'test@jiebanke.com',
phone: '13800138000',
gender: 'male',
birthday: '1990-01-01'
};
try {
const registerResponse = await api.post('/auth/register', registerData);
console.log('✅ 用户注册成功:', registerResponse.data);
} catch (error) {
if (error.response?.status === 409) {
console.log('⚠️ 用户已存在,跳过注册测试');
} else {
throw error;
}
}
// 3. 测试用户登录
console.log('\n3. 测试用户登录接口...');
const loginData = {
username: 'testuser',
password: 'test123'
};
const loginResponse = await api.post('/auth/login', loginData);
console.log('✅ 用户登录成功');
const token = loginResponse.data.data.token;
// 设置认证头
api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
// 4. 测试获取用户信息
console.log('\n4. 测试获取用户信息接口...');
const userInfoResponse = await api.get('/auth/me');
console.log('✅ 获取用户信息成功:', userInfoResponse.data.data.user.nickname);
// 5. 测试创建旅行计划
console.log('\n5. 测试创建旅行计划接口...');
const travelPlanData = {
destination: '西藏',
start_date: '2025-07-01',
end_date: '2025-07-15',
budget: 5000,
interests: '自驾,摄影,探险',
visibility: 'public'
};
const travelResponse = await api.post('/travel/plans', travelPlanData);
console.log('✅ 创建旅行计划成功:', travelResponse.data.data.plan.destination);
// 6. 测试获取旅行计划列表
console.log('\n6. 测试获取旅行计划列表接口...');
const travelListResponse = await api.get('/travel/plans');
console.log('✅ 获取旅行计划列表成功:', travelListResponse.data.data.plans.length + '条记录');
// 7. 测试商家注册
console.log('\n7. 测试商家注册接口...');
const merchantData = {
merchant_type: 'farm_owner',
business_name: '测试农场',
contact_person: '测试联系人',
contact_phone: '13800138001',
address: '测试地址',
description: '测试农场描述'
};
try {
const merchantResponse = await api.post('/merchants/register', merchantData);
console.log('✅ 商家注册成功:', merchantResponse.data.data.merchant.business_name);
} catch (error) {
console.log('⚠️ 商家注册测试跳过:', error.response?.data?.message || error.message);
}
console.log('\n🎉 所有API测试完成');
console.log('\n📋 测试总结:');
console.log('✅ 健康检查接口 - 正常');
console.log('✅ 用户认证接口 - 正常');
console.log('✅ 旅行计划接口 - 正常');
console.log('✅ 商家服务接口 - 部分测试');
} catch (error) {
console.error('❌ API测试失败:');
if (error.response) {
console.error('状态码:', error.response.status);
console.error('错误信息:', error.response.data);
} else {
console.error('错误信息:', error.message);
}
process.exit(1);
}
}
// 运行测试
if (require.main === module) {
testAPI().catch(console.error);
}
module.exports = { testAPI, api };