您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

121 行
3.6KB

  1. [CmdletBinding()]
  2. param(
  3. [Parameter(Mandatory = $true)]
  4. [string]$BaseUrl
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. Set-StrictMode -Version 2.0
  8. $script:Failures = 0
  9. function Report-Result {
  10. param(
  11. [Parameter(Mandatory = $true)][bool]$Passed,
  12. [Parameter(Mandatory = $true)][string]$Name,
  13. [string]$Detail
  14. )
  15. if ($Passed) {
  16. if ($Detail) { Write-Output "PASS: $Name ($Detail)" } else { Write-Output "PASS: $Name" }
  17. } else {
  18. if ($Detail) { Write-Output "FAIL: $Name ($Detail)" } else { Write-Output "FAIL: $Name" }
  19. $script:Failures++
  20. }
  21. }
  22. function Invoke-HttpRequest {
  23. param([Parameter(Mandatory = $true)][string]$Uri)
  24. $request = [System.Net.HttpWebRequest]::Create($Uri)
  25. $request.Method = 'GET'
  26. $request.AllowAutoRedirect = $false
  27. $response = $null
  28. try {
  29. try {
  30. $response = $request.GetResponse()
  31. } catch [System.Net.WebException] {
  32. if ($_.Exception.Response) {
  33. $response = $_.Exception.Response
  34. } else {
  35. throw
  36. }
  37. }
  38. $reader = New-Object System.IO.StreamReader($response.GetResponseStream())
  39. try {
  40. $body = $reader.ReadToEnd()
  41. } finally {
  42. $reader.Dispose()
  43. }
  44. return [PSCustomObject]@{
  45. StatusCode = [int]$response.StatusCode
  46. ContentType = [string]$response.ContentType
  47. Body = $body
  48. }
  49. } finally {
  50. if ($response) { $response.Dispose() }
  51. }
  52. }
  53. function Read-SelfTestJson {
  54. param(
  55. [Parameter(Mandatory = $true)][string]$Uri,
  56. [Parameter(Mandatory = $true)][string]$Label,
  57. [switch]$PrintChecks
  58. )
  59. try {
  60. $response = Invoke-HttpRequest -Uri $Uri
  61. Report-Result ($response.StatusCode -eq 200) "$Label HTTP status 200" "got $($response.StatusCode)"
  62. if ($response.StatusCode -ne 200) { return }
  63. try {
  64. $document = $response.Body | ConvertFrom-Json
  65. } catch {
  66. Report-Result $false "$Label JSON parses" $_.Exception.Message
  67. return
  68. }
  69. Report-Result $true "$Label JSON parses" $null
  70. if (-not $document.PSObject.Properties['ok'] -or -not $document.PSObject.Properties['checks']) {
  71. Report-Result $false "$Label JSON contract" "expected 'ok' and 'checks'"
  72. return
  73. }
  74. Report-Result $true "$Label JSON contract" $null
  75. if ($PrintChecks) {
  76. foreach ($check in @($document.checks)) {
  77. $name = [string]$check.name
  78. if (-not $name) { $name = '<unnamed check>' }
  79. $detail = [string]$check.detail
  80. Report-Result ([bool]$check.pass) $name $detail
  81. }
  82. }
  83. Report-Result ([bool]$document.ok) "$Label reports overall pass" $null
  84. } catch {
  85. Report-Result $false "$Label request" $_.Exception.Message
  86. }
  87. }
  88. $normalizedBaseUrl = $BaseUrl.TrimEnd('/')
  89. Read-SelfTestJson -Uri "$normalizedBaseUrl/self-test" -Label 'GET /self-test' -PrintChecks
  90. Read-SelfTestJson -Uri "$normalizedBaseUrl/" -Label 'GET / (test root)'
  91. try {
  92. $directHello = Invoke-HttpRequest -Uri "$normalizedBaseUrl/Default.asp?route=/hello"
  93. Report-Result ($directHello.StatusCode -eq 404) 'Direct Default.asp production route is rejected' "got HTTP $($directHello.StatusCode)"
  94. } catch {
  95. Report-Result $false 'Direct Default.asp production route is rejected' $_.Exception.Message
  96. }
  97. Write-Output '---'
  98. if ($script:Failures -eq 0) {
  99. Write-Output 'RESULT: ALL PASS'
  100. exit 0
  101. }
  102. Write-Output "RESULT: $($script:Failures) FAILURE(S)"
  103. exit 1

Powered by TurnKey Linux.