You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.1 KiB

# Swagger和身份验证测试脚本
param(
[switch]$StartBackend
)
Write-Host "🔍 Swagger和身份验证测试工具" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""
# 检查后端服务状态
Write-Host "📋 检查后端服务状态..." -ForegroundColor Yellow
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop
Write-Host "✅ 后端服务正在运行 (状态码: $($response.StatusCode))" -ForegroundColor Green
} catch {
Write-Host "❌ 后端服务未运行或无法访问" -ForegroundColor Red
Write-Host " 错误信息: $($_.Exception.Message)" -ForegroundColor Red
if ($StartBackend) {
Write-Host ""
Write-Host "🚀 正在启动后端服务..." -ForegroundColor Yellow
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd backend; go run main.go"
Write-Host "⏳ 等待后端服务启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 10
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop
Write-Host "✅ 后端服务启动成功" -ForegroundColor Green
} catch {
Write-Host "❌ 后端服务启动失败" -ForegroundColor Red
return
}
} else {
return
}
}
Write-Host ""
# 测试Swagger路由
Write-Host "🔍 测试Swagger路由..." -ForegroundColor Yellow
$swaggerRoutes = @(
@{Path="/swagger/index.html"; Name="Swagger UI"},
@{Path="/docs"; Name="文档重定向"},
@{Path="/api-docs"; Name="API文档重定向"}
)
foreach ($route in $swaggerRoutes) {
try {
$uri = "http://localhost:8080$($route.Path)"
$response = Invoke-WebRequest -Uri $uri -TimeoutSec 3 -ErrorAction Stop
$statusColor = if ($response.StatusCode -eq 200) { "Green" } else { "Yellow" }
Write-Host "$($route.Name): $($response.StatusCode)" -ForegroundColor $statusColor
} catch {
$errorMsg = if ($_.Exception.Response) {
"HTTP $($_.Exception.Response.StatusCode)"
} else {
$_.Exception.Message
}
Write-Host "$($route.Name): $errorMsg" -ForegroundColor Red
}
}
Write-Host ""
# 测试API路由(无需认证)
Write-Host "🔍 测试无需认证的API路由..." -ForegroundColor Yellow
$publicRoutes = @(
@{Path="/api/auth/roles/test"; Method="GET"; Name="角色列表(测试)"},
@{Path="/api/auth/captcha"; Method="GET"; Name="验证码接口"},
@{Path="/api/auth/login"; Method="POST"; Name="登录接口"}
)
foreach ($route in $publicRoutes) {
try {
$uri = "http://localhost:8080$($route.Path)"
if ($route.Method -eq "GET") {
$response = Invoke-WebRequest -Uri $uri -TimeoutSec 3 -ErrorAction Stop
} else {
$response = Invoke-WebRequest -Uri $uri -Method $route.Method -TimeoutSec 3 -ErrorAction Stop
}
$statusColor = if ($response.StatusCode -eq 200) { "Green" } else { "Yellow" }
Write-Host "$($route.Name): $($response.StatusCode)" -ForegroundColor $statusColor
} catch {
$errorMsg = if ($_.Exception.Response) {
"HTTP $($_.Exception.Response.StatusCode)"
} else {
$_.Exception.Message
}
Write-Host "$($route.Name): $errorMsg" -ForegroundColor Red
}
}
Write-Host ""
# 测试需要认证的API路由
Write-Host "🔍 测试需要认证的API路由..." -ForegroundColor Yellow
$protectedRoutes = @(
@{Path="/api/auth/roles"; Method="GET"; Name="角色列表(正式)"},
@{Path="/api/auth/roles"; Method="POST"; Name="创建角色"},
@{Path="/api/auth/roles/1"; Method="GET"; Name="获取角色详情"}
)
foreach ($route in $protectedRoutes) {
try {
$uri = "http://localhost:8080$($route.Path)"
$headers = @{}
if ($route.Method -eq "GET") {
$response = Invoke-WebRequest -Uri $uri -Headers $headers -TimeoutSec 3 -ErrorAction Stop
} else {
$response = Invoke-WebRequest -Uri $uri -Method $route.Method -Headers $headers -TimeoutSec 3 -ErrorAction Stop
}
Write-Host " $($route.Name): $($response.StatusCode) (应该返回401)" -ForegroundColor Yellow
} catch {
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) {
Write-Host "$($route.Name): 401 (正确返回未认证)" -ForegroundColor Green
} else {
$errorMsg = if ($_.Exception.Response) {
"HTTP $($_.Exception.Response.StatusCode)"
} else {
$_.Exception.Message
}
Write-Host "$($route.Name): $errorMsg" -ForegroundColor Red
}
}
}
Write-Host ""
# 显示访问信息
Write-Host "💡 访问信息:" -ForegroundColor Cyan
Write-Host " Swagger UI: http://localhost:8080/swagger/index.html" -ForegroundColor White
Write-Host " 文档重定向: http://localhost:8080/docs" -ForegroundColor White
Write-Host " API文档: http://localhost:8080/api-docs" -ForegroundColor White
Write-Host ""
Write-Host "🔧 身份验证说明:" -ForegroundColor Cyan
Write-Host " 1. 在Swagger UI中点击右上角的 'Authorize' 按钮" -ForegroundColor White
Write-Host " 2. 输入格式: Bearer <your-jwt-token>" -ForegroundColor White
Write-Host " 3. 先调用登录接口获取token" -ForegroundColor White
Write-Host " 4. 然后就可以测试需要认证的API了" -ForegroundColor White
Write-Host ""
Write-Host "🎯 测试步骤:" -ForegroundColor Cyan
Write-Host " 1. 访问 http://localhost:8080/swagger/index.html" -ForegroundColor White
Write-Host " 2. 测试 /api/auth/captcha 获取验证码" -ForegroundColor White
Write-Host " 3. 测试 /api/auth/login 进行登录" -ForegroundColor White
Write-Host " 4. 复制返回的access_token" -ForegroundColor White
Write-Host " 5. 点击 'Authorize' 输入: Bearer <token>" -ForegroundColor White
Write-Host " 6. 测试角色管理相关API" -ForegroundColor White