| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- # comprehensive_test.ps1 - 通过代理全面测试所有API
- # 1. 先登录获取token
- # 2. 从all_api_urls.json读取所有URL
- # 3. 通过代理(80端口)用POST方法测试
- # 4. 分类统计结果
- $ErrorActionPreference = "SilentlyContinue"
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- # 登录获取token
- Write-Output "=== 登录获取token ==="
- $loginBody = '{"username":"admin","password":"admin123","tenantCode":"T202605253515"}'
- $loginResp = Invoke-WebRequest -Uri 'http://localhost/prod-api/login' -Method POST -ContentType 'application/json' -Body $loginBody -UseBasicParsing -TimeoutSec 10
- $loginJson = $loginResp.Content | ConvertFrom-Json
- $token = $loginJson.token
- if (-not $token) {
- Write-Output "登录失败!退出"
- exit 1
- }
- Write-Output "Token: $($token.Substring(0,20))..."
- # 读取所有API URL
- $allUrls = Get-Content -Path 'd:\ylrz\saasadminui\all_api_urls.json' -Raw | ConvertFrom-Json
- Write-Output "总URL数: $($allUrls.Count)"
- # 去重
- $uniqueUrls = $allUrls | Select-Object -Unique
- Write-Output "唯一URL数: $($uniqueUrls.Count)"
- # 测试结果分类
- $results = @{
- ok = [System.Collections.ArrayList]::new()
- notFound = [System.Collections.ArrayList]::new()
- serverError = [System.Collections.ArrayList]::new()
- forbidden = [System.Collections.ArrayList]::new()
- timeout = [System.Collections.ArrayList]::new()
- other = [System.Collections.ArrayList]::new()
- }
- $headers = @{
- 'Authorization' = "Bearer $token"
- 'Content-Type' = 'application/json'
- }
- $total = $uniqueUrls.Count
- $i = 0
- foreach ($url in $uniqueUrls) {
- $i++
- $fullUrl = "http://localhost/prod-api$url"
-
- # 进度每100个输出一次
- if ($i % 100 -eq 0) {
- Write-Output "进度: $i / $total (OK=$($results.ok.Count) 404=$($results.notFound.Count) 500=$($results.serverError.Count) TIMEOUT=$($results.timeout.Count) OTHER=$($results.other.Count))"
- }
-
- try {
- $resp = Invoke-WebRequest -Uri $fullUrl -Method POST -Headers $headers -Body '{}' -UseBasicParsing -TimeoutSec 8
- $code = $resp.StatusCode
- # 有些API即使200也可能返回错误
- $body = $resp.Content
- if ($code -eq 200) {
- [void]$results.ok.Add("$url")
- } else {
- [void]$results.other.Add("$code|$url")
- }
- }
- catch {
- $errMsg = $_.Exception.Message
- if ($errMsg -match '404') {
- [void]$results.notFound.Add("404|$url")
- }
- elseif ($errMsg -match '500') {
- [void]$results.serverError.Add("500|$url")
- }
- elseif ($errMsg -match '403') {
- [void]$results.forbidden.Add("403|$url")
- }
- elseif ($errMsg -match '401') {
- [void]$results.forbidden.Add("401|$url")
- }
- elseif ($errMsg -match 'timeout|timed out|连接|无法连接') {
- [void]$results.timeout.Add("0|$url")
- }
- else {
- # 提取HTTP状态码
- if ($errMsg -match '(\d{3})') {
- $code = $Matches[1]
- [void]$results.other.Add("$code|$url|$errMsg")
- } else {
- [void]$results.other.Add("0|$url|$errMsg")
- }
- }
- }
- }
- # 输出统计
- Write-Output ""
- Write-Output "=== TEST SUMMARY ==="
- Write-Output "OK: $($results.ok.Count)"
- Write-Output "404 NOT FOUND: $($results.notFound.Count)"
- Write-Output "500 SERVER ERROR: $($results.serverError.Count)"
- Write-Output "403/401 FORBIDDEN: $($results.forbidden.Count)"
- Write-Output "TIMEOUT: $($results.timeout.Count)"
- Write-Output "OTHER: $($results.other.Count)"
- # 保存详细结果
- $output = @()
- $output += "=== TEST SUMMARY ==="
- $output += "OK: $($results.ok.Count)"
- $output += "404 NOT FOUND: $($results.notFound.Count)"
- $output += "500 SERVER ERROR: $($results.serverError.Count)"
- $output += "403/401 FORBIDDEN: $($results.forbidden.Count)"
- $output += "TIMEOUT: $($results.timeout.Count)"
- $output += "OTHER: $($results.other.Count)"
- $output += ""
- $output += "=== ALL 404 ERRORS ==="
- $output += $results.notFound
- $output += ""
- $output += "=== ALL 500 ERRORS ==="
- $output += $results.serverError
- $output += ""
- $output += "=== ALL 403/401 ERRORS ==="
- $output += $results.forbidden
- $output += ""
- $output += "=== ALL TIMEOUT ERRORS ==="
- $output += $results.timeout
- $output += ""
- $output += "=== ALL OTHER ERRORS ==="
- $output += $results.other
- $output | Out-File -FilePath 'd:\ylrz\saasadminui\test_results_v3.txt' -Encoding utf8
- Write-Output "结果已保存到 test_results_v3.txt"
|