Files
nxxmdata/test-login.js

68 lines
1.9 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.

const axios = require('axios');
// 配置axios实例
const api = axios.create({
baseURL: 'http://localhost:5352/api',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});
// 登录测试函数
async function testLogin() {
console.log('开始测试登录功能...');
try {
// 1. 测试登录
console.log('1. 发送登录请求...');
const loginResponse = await api.post('/auth/login', {
username: 'admin',
password: '123456'
});
console.log('登录响应:', loginResponse.data);
if (loginResponse.data.code !== 200) {
console.error('登录失败:', loginResponse.data.message);
return;
}
const token = loginResponse.data.data.token;
console.log('获取到token:', token);
// 设置axios默认headers添加token
api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
// 2. 测试获取用户信息
console.log('\n2. 测试获取用户信息...');
const userInfoResponse = await api.get('/auth/userinfo');
console.log('用户信息响应:', userInfoResponse.data);
if (userInfoResponse.data.code !== 200) {
console.error('获取用户信息失败:', userInfoResponse.data.message);
} else {
console.log('用户信息获取成功!');
}
// 3. 测试退出登录
console.log('\n3. 测试退出登录...');
const logoutResponse = await api.post('/auth/logout');
console.log('退出登录响应:', logoutResponse.data);
if (logoutResponse.data.code === 200) {
console.log('退出登录成功!');
}
} catch (error) {
console.error('测试过程中发生错误:', error.response ? error.response.data : error.message);
}
}
// 运行测试
console.log('准备运行登录功能测试...');
testLogin().then(() => {
console.log('\n登录功能测试完成');
});