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

186 行
6.7KB

  1. <?xml version="1.0"?>
  2. <?component error="true" debug="false"?>
  3. <component>
  4. <registration
  5. description="WscMvc SelfTestController"
  6. progid="WscMvc.SelfTestController"
  7. version="1.00"
  8. classid="{D2634944-4646-4C55-956E-4C05E7E10904}">
  9. </registration>
  10. <public>
  11. <method name="RunSelfTest">
  12. <parameter name="logDir"/>
  13. <parameter name="body"/>
  14. </method>
  15. </public>
  16. <script language="VBScript">
  17. <![CDATA[
  18. Option Explicit
  19. ' HTTP+JSON test harness, callable as GET /self-test from any CLI (curl,
  20. ' Invoke-WebRequest, etc.) - no cscript/PowerShell/WSH access to the VM
  21. ' required. Runs the same checks as tests/Test-Components.vbs, in-process,
  22. ' against the live registered components. Intentionally returns raw
  23. ' Err.Description in failure details: unlike Default.asp's own error
  24. ' handling (which must never leak internals to an arbitrary client), this
  25. ' route IS the diagnostics surface, so its whole job is to say what broke.
  26. ' This is a dev/test-milestone tool, not a production data endpoint -
  27. ' revisit whether it should be gated/removed during M6 hardening.
  28. Sub RunSelfTest(logDir, body)
  29. Dim checks, allPass
  30. checks = ""
  31. allPass = True
  32. ' --- RequestContext basic contract ---
  33. Dim ctx1, ctx1Ok, ctx1Detail
  34. ctx1Ok = False
  35. ctx1Detail = ""
  36. On Error Resume Next
  37. Set ctx1 = CreateObject("WscMvc.RequestContext")
  38. If Err.Number = 0 Then ctx1.Initialize "/self-test-a", "GET", logDir
  39. If Err.Number <> 0 Then
  40. ctx1Detail = Err.Description
  41. Err.Clear
  42. ElseIf ctx1.Path <> "/self-test-a" Then
  43. ctx1Detail = "Path roundtrip mismatch: got [" & ctx1.Path & "]"
  44. ElseIf ctx1.HttpMethod <> "GET" Then
  45. ctx1Detail = "HttpMethod roundtrip mismatch: got [" & ctx1.HttpMethod & "]"
  46. ElseIf Len(ctx1.CorrelationId) = 0 Then
  47. ctx1Detail = "CorrelationId was empty"
  48. ElseIf ctx1.ElapsedMs() < 0 Then
  49. ctx1Detail = "ElapsedMs was negative"
  50. Else
  51. ctx1Ok = True
  52. End If
  53. On Error Goto 0
  54. checks = AppendCheck(checks, "request_context_contract", ctx1Ok, ctx1Detail)
  55. allPass = allPass And ctx1Ok
  56. ' --- Two contexts must not collide on correlation id ---
  57. Dim ctx2, distinctOk, distinctDetail
  58. distinctOk = False
  59. distinctDetail = ""
  60. On Error Resume Next
  61. Set ctx2 = CreateObject("WscMvc.RequestContext")
  62. ctx2.Initialize "/self-test-b", "GET", logDir
  63. If Err.Number <> 0 Then
  64. distinctDetail = Err.Description
  65. Err.Clear
  66. ElseIf ctx1.CorrelationId = ctx2.CorrelationId Then
  67. distinctDetail = "Both contexts produced the same CorrelationId: " & ctx1.CorrelationId
  68. Else
  69. distinctOk = True
  70. End If
  71. On Error Goto 0
  72. checks = AppendCheck(checks, "correlation_id_uniqueness", distinctOk, distinctDetail)
  73. allPass = allPass And distinctOk
  74. ' --- Shared Application must isolate route sets. The test app must not
  75. ' activate or test production's HomeController. ---
  76. Dim app, ctxUnknown, unkStatus, unkType, unkBody, unkAllow, unkOk, unkDetail
  77. unkOk = False
  78. unkDetail = ""
  79. On Error Resume Next
  80. Set app = CreateObject("WscMvc.Application")
  81. Set ctxUnknown = CreateObject("WscMvc.RequestContext")
  82. ctxUnknown.Initialize "/hello", "GET", logDir
  83. unkStatus = "" : unkType = "" : unkBody = "" : unkAllow = ""
  84. app.Run ctxUnknown, "tests", "", unkStatus, unkType, unkBody, unkAllow
  85. If Err.Number <> 0 Then
  86. unkDetail = Err.Description
  87. Err.Clear
  88. ElseIf unkStatus <> "404 Not Found" Then
  89. unkDetail = "Expected 404 Not Found, got [" & unkStatus & "]"
  90. Else
  91. unkOk = True
  92. End If
  93. On Error Goto 0
  94. checks = AppendCheck(checks, "test_app_rejects_production_route", unkOk, unkDetail)
  95. allPass = allPass And unkOk
  96. ' --- Method dispatch contract: test app self-test route supports both
  97. ' GET and POST, and reports an Allow header for unsupported methods. ---
  98. Dim router, ctxPost, postStatus, postType, postBody, postAllow, postHandler, postOk, postDetail
  99. postOk = False
  100. postDetail = ""
  101. On Error Resume Next
  102. Set router = CreateObject("WscMvc.Router")
  103. Set ctxPost = CreateObject("WscMvc.RequestContext")
  104. ctxPost.Initialize "/self-test", "POST", logDir
  105. postStatus = "" : postType = "" : postBody = "" : postAllow = "" : postHandler = ""
  106. router.Match ctxPost, "tests", postStatus, postType, postBody, postAllow, postHandler
  107. If Err.Number <> 0 Then
  108. postDetail = Err.Description
  109. Err.Clear
  110. ElseIf postHandler <> "SelfTest.RunSelfTest" Then
  111. postDetail = "Expected SelfTest.RunSelfTest handler, got [" & postHandler & "]"
  112. ElseIf postStatus <> "" Then
  113. postDetail = "Expected empty status for matched route, got [" & postStatus & "]"
  114. Else
  115. postOk = True
  116. End If
  117. On Error Goto 0
  118. checks = AppendCheck(checks, "post_self_test_route", postOk, postDetail)
  119. allPass = allPass And postOk
  120. Dim ctxDelete, delStatus, delType, delBody, delAllow, delHandler, delOk, delDetail
  121. delOk = False
  122. delDetail = ""
  123. On Error Resume Next
  124. Set ctxDelete = CreateObject("WscMvc.RequestContext")
  125. ctxDelete.Initialize "/self-test", "DELETE", logDir
  126. delStatus = "" : delType = "" : delBody = "" : delAllow = "" : delHandler = ""
  127. router.Match ctxDelete, "tests", delStatus, delType, delBody, delAllow, delHandler
  128. If Err.Number <> 0 Then
  129. delDetail = Err.Description
  130. Err.Clear
  131. ElseIf delStatus <> "405 Method Not Allowed" Then
  132. delDetail = "Expected 405 Method Not Allowed, got [" & delStatus & "]"
  133. ElseIf delAllow <> "GET, POST" Then
  134. delDetail = "Expected Allow [GET, POST], got [" & delAllow & "]"
  135. Else
  136. delOk = True
  137. End If
  138. On Error Goto 0
  139. checks = AppendCheck(checks, "delete_self_test_allow_header", delOk, delDetail)
  140. allPass = allPass And delOk
  141. Set ctx1 = Nothing
  142. Set ctx2 = Nothing
  143. Set ctxUnknown = Nothing
  144. Set ctxPost = Nothing
  145. Set ctxDelete = Nothing
  146. Set router = Nothing
  147. Set app = Nothing
  148. body = "{""ok"":" & LCase(CStr(allPass)) & ",""checks"":[" & checks & "]}"
  149. End Sub
  150. Function AppendCheck(checksSoFar, name, pass, detail)
  151. Dim entry
  152. entry = "{""name"":""" & JsonEscape(name) & """,""pass"":" & LCase(CStr(pass)) & _
  153. ",""detail"":""" & JsonEscape(detail) & """}"
  154. If Len(checksSoFar) = 0 Then
  155. AppendCheck = entry
  156. Else
  157. AppendCheck = checksSoFar & "," & entry
  158. End If
  159. End Function
  160. Function JsonEscape(s)
  161. Dim result
  162. result = s
  163. result = Replace(result, "\", "\\")
  164. result = Replace(result, """", "\""")
  165. result = Replace(result, vbCrLf, "\n")
  166. result = Replace(result, vbCr, "\n")
  167. result = Replace(result, vbLf, "\n")
  168. result = Replace(result, vbTab, "\t")
  169. JsonEscape = result
  170. End Function
  171. ]]>
  172. </script>
  173. </component>

Powered by TurnKey Linux.