Files
nl-blogs/test-user-api.js
2026-01-15 13:51:44 +08:00

46 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const fetch = require('node-fetch');
const API_BASE = 'http://localhost:8081/api';
async function testUserApi() {
try {
console.log('Testing user API...');
// 测试获取用户列表(不需要身份验证?)
console.log('1. Testing GET /admin/users');
const usersResponse = await fetch(`${API_BASE}/admin/users`);
console.log('Status:', usersResponse.status);
if (usersResponse.ok) {
const users = await usersResponse.json();
console.log('Users:', users);
// 如果有用户,测试获取单个用户
if (users && users.length > 0) {
const userId = users[0].id;
console.log(`\n2. Testing GET /admin/users/${userId}`);
const userResponse = await fetch(`${API_BASE}/admin/users/${userId}`);
console.log('Status:', userResponse.status);
if (userResponse.ok) {
const user = await userResponse.json();
console.log('User:', user);
console.log('\n✅ API test passed!');
} else {
const error = await userResponse.json();
console.log('Error:', error);
console.log('\n⚠ API test failed!');
}
}
} else {
const error = await usersResponse.json().catch(() => usersResponse.text());
console.log('Error:', error);
console.log('\n⚠ API test failed!');
}
} catch (error) {
console.error('Test error:', error);
console.log('\n❌ API test failed!');
}
}
testUserApi();