133 lines
3.6 KiB
JavaScript
133 lines
3.6 KiB
JavaScript
const axios = require('axios');
|
|
|
|
// 创建axios实例
|
|
const api = axios.create({
|
|
baseURL: 'http://localhost:5352/api/cattle-academy',
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer test-token'
|
|
}
|
|
});
|
|
|
|
// 测试获取养牛学院资讯列表
|
|
async function testGetCattleAcademyList() {
|
|
try {
|
|
console.log('测试获取养牛学院资讯列表...');
|
|
const response = await api.get('/');
|
|
console.log('获取列表成功:', response.data);
|
|
} catch (error) {
|
|
console.error('获取列表失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 测试获取单个养牛学院资讯详情
|
|
async function testGetCattleAcademyById() {
|
|
try {
|
|
console.log('测试获取养牛学院资讯详情...');
|
|
const response = await api.get('/1');
|
|
console.log('获取详情成功:', response.data);
|
|
} catch (error) {
|
|
console.error('获取详情失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 测试创建养牛学院资讯
|
|
async function testCreateCattleAcademy() {
|
|
try {
|
|
console.log('测试创建养牛学院资讯...');
|
|
const data = {
|
|
title: '测试养牛学院资讯',
|
|
coverImage: 'https://via.placeholder.com/100x50',
|
|
content: '测试内容...',
|
|
summary: '测试摘要',
|
|
category: '技术资讯',
|
|
sort: 50,
|
|
status: true,
|
|
author: '测试作者',
|
|
publishTime: new Date().toISOString(),
|
|
isTop: false,
|
|
isRecommend: false,
|
|
remarks: '测试备注'
|
|
};
|
|
const response = await api.post('/', data);
|
|
console.log('创建成功:', response.data);
|
|
} catch (error) {
|
|
console.error('创建失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 测试更新养牛学院资讯
|
|
async function testUpdateCattleAcademy() {
|
|
try {
|
|
console.log('测试更新养牛学院资讯...');
|
|
const data = {
|
|
title: '更新测试养牛学院资讯',
|
|
coverImage: 'https://via.placeholder.com/100x50',
|
|
content: '更新测试内容...',
|
|
summary: '更新测试摘要',
|
|
category: '养殖技术',
|
|
sort: 60,
|
|
status: true,
|
|
author: '更新测试作者',
|
|
publishTime: new Date().toISOString(),
|
|
isTop: true,
|
|
isRecommend: true,
|
|
remarks: '更新测试备注'
|
|
};
|
|
const response = await api.put('/1', data);
|
|
console.log('更新成功:', response.data);
|
|
} catch (error) {
|
|
console.error('更新失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 测试切换资讯状态
|
|
async function testToggleStatus() {
|
|
try {
|
|
console.log('测试切换资讯状态...');
|
|
const response = await api.patch('/1/status', { status: false });
|
|
console.log('状态切换成功:', response.data);
|
|
} catch (error) {
|
|
console.error('状态切换失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 测试删除养牛学院资讯
|
|
async function testDeleteCattleAcademy() {
|
|
try {
|
|
console.log('测试删除养牛学院资讯...');
|
|
const response = await api.delete('/1');
|
|
console.log('删除成功:', response.data);
|
|
} catch (error) {
|
|
console.error('删除失败:', error.response?.data || error.message);
|
|
}
|
|
}
|
|
|
|
// 运行所有测试
|
|
async function runAllTests() {
|
|
console.log('开始测试养牛学院API...\n');
|
|
|
|
await testGetCattleAcademyList();
|
|
console.log('\n');
|
|
|
|
await testGetCattleAcademyById();
|
|
console.log('\n');
|
|
|
|
await testCreateCattleAcademy();
|
|
console.log('\n');
|
|
|
|
await testUpdateCattleAcademy();
|
|
console.log('\n');
|
|
|
|
await testToggleStatus();
|
|
console.log('\n');
|
|
|
|
await testDeleteCattleAcademy();
|
|
console.log('\n');
|
|
|
|
console.log('所有测试完成');
|
|
}
|
|
|
|
runAllTests();
|