comprehensive_test.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # comprehensive_test.ps1 - 通过代理全面测试所有API
  2. # 1. 先登录获取token
  3. # 2. 从all_api_urls.json读取所有URL
  4. # 3. 通过代理(80端口)用POST方法测试
  5. # 4. 分类统计结果
  6. $ErrorActionPreference = "SilentlyContinue"
  7. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  8. # 登录获取token
  9. Write-Output "=== 登录获取token ==="
  10. $loginBody = '{"username":"admin","password":"admin123","tenantCode":"T202605253515"}'
  11. $loginResp = Invoke-WebRequest -Uri 'http://localhost/prod-api/login' -Method POST -ContentType 'application/json' -Body $loginBody -UseBasicParsing -TimeoutSec 10
  12. $loginJson = $loginResp.Content | ConvertFrom-Json
  13. $token = $loginJson.token
  14. if (-not $token) {
  15. Write-Output "登录失败!退出"
  16. exit 1
  17. }
  18. Write-Output "Token: $($token.Substring(0,20))..."
  19. # 读取所有API URL
  20. $allUrls = Get-Content -Path 'd:\ylrz\saasadminui\all_api_urls.json' -Raw | ConvertFrom-Json
  21. Write-Output "总URL数: $($allUrls.Count)"
  22. # 去重
  23. $uniqueUrls = $allUrls | Select-Object -Unique
  24. Write-Output "唯一URL数: $($uniqueUrls.Count)"
  25. # 测试结果分类
  26. $results = @{
  27. ok = [System.Collections.ArrayList]::new()
  28. notFound = [System.Collections.ArrayList]::new()
  29. serverError = [System.Collections.ArrayList]::new()
  30. forbidden = [System.Collections.ArrayList]::new()
  31. timeout = [System.Collections.ArrayList]::new()
  32. other = [System.Collections.ArrayList]::new()
  33. }
  34. $headers = @{
  35. 'Authorization' = "Bearer $token"
  36. 'Content-Type' = 'application/json'
  37. }
  38. $total = $uniqueUrls.Count
  39. $i = 0
  40. foreach ($url in $uniqueUrls) {
  41. $i++
  42. $fullUrl = "http://localhost/prod-api$url"
  43. # 进度每100个输出一次
  44. if ($i % 100 -eq 0) {
  45. Write-Output "进度: $i / $total (OK=$($results.ok.Count) 404=$($results.notFound.Count) 500=$($results.serverError.Count) TIMEOUT=$($results.timeout.Count) OTHER=$($results.other.Count))"
  46. }
  47. try {
  48. $resp = Invoke-WebRequest -Uri $fullUrl -Method POST -Headers $headers -Body '{}' -UseBasicParsing -TimeoutSec 8
  49. $code = $resp.StatusCode
  50. # 有些API即使200也可能返回错误
  51. $body = $resp.Content
  52. if ($code -eq 200) {
  53. [void]$results.ok.Add("$url")
  54. } else {
  55. [void]$results.other.Add("$code|$url")
  56. }
  57. }
  58. catch {
  59. $errMsg = $_.Exception.Message
  60. if ($errMsg -match '404') {
  61. [void]$results.notFound.Add("404|$url")
  62. }
  63. elseif ($errMsg -match '500') {
  64. [void]$results.serverError.Add("500|$url")
  65. }
  66. elseif ($errMsg -match '403') {
  67. [void]$results.forbidden.Add("403|$url")
  68. }
  69. elseif ($errMsg -match '401') {
  70. [void]$results.forbidden.Add("401|$url")
  71. }
  72. elseif ($errMsg -match 'timeout|timed out|连接|无法连接') {
  73. [void]$results.timeout.Add("0|$url")
  74. }
  75. else {
  76. # 提取HTTP状态码
  77. if ($errMsg -match '(\d{3})') {
  78. $code = $Matches[1]
  79. [void]$results.other.Add("$code|$url|$errMsg")
  80. } else {
  81. [void]$results.other.Add("0|$url|$errMsg")
  82. }
  83. }
  84. }
  85. }
  86. # 输出统计
  87. Write-Output ""
  88. Write-Output "=== TEST SUMMARY ==="
  89. Write-Output "OK: $($results.ok.Count)"
  90. Write-Output "404 NOT FOUND: $($results.notFound.Count)"
  91. Write-Output "500 SERVER ERROR: $($results.serverError.Count)"
  92. Write-Output "403/401 FORBIDDEN: $($results.forbidden.Count)"
  93. Write-Output "TIMEOUT: $($results.timeout.Count)"
  94. Write-Output "OTHER: $($results.other.Count)"
  95. # 保存详细结果
  96. $output = @()
  97. $output += "=== TEST SUMMARY ==="
  98. $output += "OK: $($results.ok.Count)"
  99. $output += "404 NOT FOUND: $($results.notFound.Count)"
  100. $output += "500 SERVER ERROR: $($results.serverError.Count)"
  101. $output += "403/401 FORBIDDEN: $($results.forbidden.Count)"
  102. $output += "TIMEOUT: $($results.timeout.Count)"
  103. $output += "OTHER: $($results.other.Count)"
  104. $output += ""
  105. $output += "=== ALL 404 ERRORS ==="
  106. $output += $results.notFound
  107. $output += ""
  108. $output += "=== ALL 500 ERRORS ==="
  109. $output += $results.serverError
  110. $output += ""
  111. $output += "=== ALL 403/401 ERRORS ==="
  112. $output += $results.forbidden
  113. $output += ""
  114. $output += "=== ALL TIMEOUT ERRORS ==="
  115. $output += $results.timeout
  116. $output += ""
  117. $output += "=== ALL OTHER ERRORS ==="
  118. $output += $results.other
  119. $output | Out-File -FilePath 'd:\ylrz\saasadminui\test_results_v3.txt' -Encoding utf8
  120. Write-Output "结果已保存到 test_results_v3.txt"