test_apis.ps1 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Comprehensive API test script for saasadminui
  2. # 1. Login to get token
  3. # 2. Extract all API paths from saasadminui frontend source
  4. # 3. Test each API against fs-company (8006)
  5. # 4. Categorize results: 200, 404, 500, 403
  6. $companyUrl = "http://localhost:8006"
  7. # Step 1: Login
  8. Write-Host "=== Step 1: Login ==="
  9. $loginBody = '{"username":"admin","password":"admin123","tenantCode":"T202605253515"}'
  10. $loginResp = Invoke-RestMethod -Uri "$companyUrl/login" -Method POST -ContentType "application/json" -Body $loginBody
  11. $token = $loginResp.token
  12. Write-Host "Login OK, token length: $($token.Length)"
  13. $headers = @{ Authorization = "Bearer $token" }
  14. # Step 2: Extract API paths from saasadminui frontend source
  15. Write-Host "`n=== Step 2: Extract API paths ==="
  16. $apiDir = "d:\ylrz\saasadminui\src\api"
  17. $apiPaths = @{}
  18. Get-ChildItem -Path $apiDir -Filter "*.js" -Recurse | ForEach-Object {
  19. $content = Get-Content $_.FullName -Raw -Encoding UTF8
  20. # Match patterns like: url: '/xxx/yyy' or url: "/xxx/yyy" or request({ url: '/xxx/yyy'
  21. $matches = [regex]::Matches($content, "[Uu][Rr][Ll]\s*[:=]\s*['""]([^'""]+)['""]")
  22. foreach ($m in $matches) {
  23. $path = $m.Groups[1].Value
  24. if ($path -match "^/" -and $path -notmatch "\$\{" -and $path -notmatch "^/proxy") {
  25. # Determine HTTP method from context
  26. $method = "GET"
  27. $lineBefore = $content.Substring(0, $m.Index)
  28. if ($lineBefore -match "(?m)(delete|remove|del)\s*\(" -or $path -match "/del|/remove") {
  29. $method = "DELETE"
  30. } elseif ($lineBefore -match "(?m)(add|create|save|insert|post|upload)\s*\(" -or $path -match "/add|/create|/save|/upload") {
  31. $method = "POST"
  32. } elseif ($lineBefore -match "(?m)(update|edit|modify|put)\s*\(" -or $path -match "/edit|/update") {
  33. $method = "PUT"
  34. }
  35. if (-not $apiPaths.ContainsKey($path)) {
  36. $apiPaths[$path] = $method
  37. }
  38. }
  39. }
  40. }
  41. Write-Host "Found $($apiPaths.Count) unique API paths"
  42. # Step 3: Test each API
  43. Write-Host "`n=== Step 3: Testing APIs ==="
  44. $results = @{ ok = 0; notFound = 0; serverError = 0; forbidden = 0; other = 0 }
  45. $notFoundList = @()
  46. $serverErrorList = @()
  47. $forbiddenList = @()
  48. $errorDetails = @{}
  49. $total = $apiPaths.Count
  50. $idx = 0
  51. foreach ($path in ($apiPaths.Keys | Sort-Object)) {
  52. $idx++
  53. $method = $apiPaths[$path]
  54. $url = "$companyUrl$path"
  55. try {
  56. if ($method -eq "GET") {
  57. $resp = Invoke-WebRequest -Uri $url -Headers $headers -Method GET -UseBasicParsing -TimeoutSec 10
  58. } elseif ($method -eq "POST") {
  59. $resp = Invoke-WebRequest -Uri $url -Headers $headers -Method POST -ContentType "application/json" -Body "{}" -UseBasicParsing -TimeoutSec 10
  60. } elseif ($method -eq "PUT") {
  61. $resp = Invoke-WebRequest -Uri $url -Headers $headers -Method PUT -ContentType "application/json" -Body "{}" -UseBasicParsing -TimeoutSec 10
  62. } elseif ($method -eq "DELETE") {
  63. $resp = Invoke-WebRequest -Uri $url -Headers $headers -Method DELETE -UseBasicParsing -TimeoutSec 10
  64. }
  65. $code = $resp.StatusCode
  66. $results.ok++
  67. } catch {
  68. $statusCode = $_.Exception.Response.StatusCode.value__
  69. if ($statusCode -eq 404) {
  70. $results.notFound++
  71. $notFoundList += $path
  72. } elseif ($statusCode -eq 500) {
  73. $results.serverError++
  74. $serverErrorList += $path
  75. # Get error message
  76. try {
  77. $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
  78. $errBody = $reader.ReadToEnd()
  79. $reader.Close()
  80. if ($errBody -match '"message":"([^"]+)"') {
  81. $errorDetails[$path] = $matches[1]
  82. } elseif ($errBody -match '"msg":"([^"]+)"') {
  83. $errorDetails[$path] = $matches[1]
  84. }
  85. } catch {}
  86. } elseif ($statusCode -eq 403) {
  87. $results.forbidden++
  88. $forbiddenList += $path
  89. } else {
  90. $results.other++
  91. }
  92. }
  93. if ($idx % 100 -eq 0) {
  94. Write-Host " Progress: $idx / $total (200=$($results.ok), 404=$($results.notFound), 500=$($results.serverError))"
  95. }
  96. }
  97. # Step 4: Output results
  98. Write-Host "`n=== Results ==="
  99. Write-Host "200 OK: $($results.ok)"
  100. Write-Host "404 Not Found: $($results.notFound)"
  101. Write-Host "500 Server Error: $($results.serverError)"
  102. Write-Host "403 Forbidden: $($results.forbidden)"
  103. Write-Host "Other: $($results.other)"
  104. # Save detailed results
  105. $notFoundList | Out-File -FilePath "d:\ylrz\saasadminui\api_404.txt" -Encoding UTF8
  106. $serverErrorList | Out-File -FilePath "d:\ylrz\saasadminui\api_500.txt" -Encoding UTF8
  107. $forbiddenList | Out-File -FilePath "d:\ylrz\saasadminui\api_403.txt" -Encoding UTF8
  108. # Analyze 404 by prefix
  109. Write-Host "`n=== 404 by prefix ==="
  110. $notFoundByPrefix = @{}
  111. foreach ($p in $notFoundList) {
  112. $parts = $p.Split("/")
  113. if ($parts.Length -ge 3) {
  114. $prefix = "/$($parts[1])/$($parts[2])"
  115. } elseif ($parts.Length -ge 2) {
  116. $prefix = "/$($parts[1])"
  117. } else {
  118. $prefix = $p
  119. }
  120. if (-not $notFoundByPrefix.ContainsKey($prefix)) { $notFoundByPrefix[$prefix] = 0 }
  121. $notFoundByPrefix[$prefix]++
  122. }
  123. $notFoundByPrefix.GetEnumerator() | Sort-Object Value -Descending | Select-Object -First 30 | ForEach-Object {
  124. Write-Host " $($_.Key): $($_.Value)"
  125. }
  126. Write-Host "`n=== 500 by prefix ==="
  127. $serverErrorByPrefix = @{}
  128. foreach ($p in $serverErrorList) {
  129. $parts = $p.Split("/")
  130. if ($parts.Length -ge 3) {
  131. $prefix = "/$($parts[1])/$($parts[2])"
  132. } elseif ($parts.Length -ge 2) {
  133. $prefix = "/$($parts[1])"
  134. } else {
  135. $prefix = $p
  136. }
  137. if (-not $serverErrorByPrefix.ContainsKey($prefix)) { $serverErrorByPrefix[$prefix] = 0 }
  138. $serverErrorByPrefix[$prefix]++
  139. }
  140. $serverErrorByPrefix.GetEnumerator() | Sort-Object Value -Descending | Select-Object -First 30 | ForEach-Object {
  141. Write-Host " $($_.Key): $($_.Value)"
  142. }
  143. Write-Host "`n=== 500 error details ==="
  144. $errorDetails.GetEnumerator() | Select-Object -First 30 | ForEach-Object {
  145. Write-Host " $($_.Key): $($_.Value)"
  146. }
  147. Write-Host "`nDone!"