# GoFaster API 测试脚本 # 用于测试用户管理API接口 param( [string]$BaseUrl = "http://localhost:8080", [string]$Username = "admin", [string]$Password = "password" ) Write-Host "========================================" -ForegroundColor Cyan Write-Host " GoFaster API 测试脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # 1. 测试健康检查端点 Write-Host "1. 测试健康检查端点..." -ForegroundColor Yellow try { $healthResponse = Invoke-RestMethod -Uri "$BaseUrl/health" -Method GET -TimeoutSec 10 Write-Host "✅ 健康检查通过: $($healthResponse.status)" -ForegroundColor Green } catch { Write-Host "❌ 健康检查失败: $($_.Exception.Message)" -ForegroundColor Red exit 1 } Write-Host "" # 2. 测试登录接口 Write-Host "2. 测试登录接口..." -ForegroundColor Yellow try { $loginData = @{ username = $Username password = $Password } | ConvertTo-Json $loginResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/login" -Method POST -Body $loginData -ContentType "application/json" -TimeoutSec 10 if ($loginResponse.token) { Write-Host "✅ 登录成功,获取到token" -ForegroundColor Green $token = $loginResponse.token } else { Write-Host "❌ 登录失败,未获取到token" -ForegroundColor Red Write-Host "响应内容: $($loginResponse | ConvertTo-Json)" -ForegroundColor Red exit 1 } } catch { Write-Host "❌ 登录失败: $($_.Exception.Message)" -ForegroundColor Red exit 1 } Write-Host "" # 3. 测试用户列表接口(需要管理员权限) Write-Host "3. 测试用户列表接口..." -ForegroundColor Yellow try { $headers = @{ "Authorization" = "Bearer $token" "Content-Type" = "application/json" } $usersResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/admin/users?page=1&pageSize=10" -Method GET -Headers $headers -TimeoutSec 10 if ($usersResponse.data) { Write-Host "✅ 用户列表获取成功" -ForegroundColor Green Write-Host " 用户数量: $($usersResponse.data.Count)" -ForegroundColor White Write-Host " 总数量: $($usersResponse.total)" -ForegroundColor White Write-Host " 当前页: $($usersResponse.page)" -ForegroundColor White Write-Host " 每页大小: $($usersResponse.size)" -ForegroundColor White } else { Write-Host "❌ 用户列表获取失败,响应格式异常" -ForegroundColor Red Write-Host "响应内容: $($usersResponse | ConvertTo-Json)" -ForegroundColor Red } } catch { Write-Host "❌ 用户列表获取失败: $($_.Exception.Message)" -ForegroundColor Red if ($_.Exception.Response) { $statusCode = $_.Exception.Response.StatusCode Write-Host "HTTP状态码: $statusCode" -ForegroundColor Red } } Write-Host "" # 4. 测试角色列表接口 Write-Host "4. 测试角色列表接口..." -ForegroundColor Yellow try { $rolesResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/admin/roles" -Method GET -Headers $headers -TimeoutSec 10 if ($rolesResponse.data) { Write-Host "✅ 角色列表获取成功" -ForegroundColor Green Write-Host " 角色数量: $($rolesResponse.data.Count)" -ForegroundColor White foreach ($role in $rolesResponse.data) { Write-Host " - $($role.name) ($($role.code))" -ForegroundColor White } } else { Write-Host "❌ 角色列表获取失败,响应格式异常" -ForegroundColor Red Write-Host "响应内容: $($rolesResponse | ConvertTo-Json)" -ForegroundColor Red } } catch { Write-Host "❌ 角色列表获取失败: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host " API 测试完成" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # 显示使用说明 Write-Host "使用说明:" -ForegroundColor Yellow Write-Host " - 默认用户名: admin" -ForegroundColor White Write-Host " - 默认密码: password" -ForegroundColor White Write-Host " - 可以通过参数修改: .\test-api.ps1 -Username 'your_username' -Password 'your_password'" -ForegroundColor White Write-Host "" Write-Host "按任意键退出..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")