Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

217 Zeilen
7.2KB

  1. Option Explicit
  2. Dim fso, logDir, pass
  3. pass = True
  4. Set fso = CreateObject("Scripting.FileSystemObject")
  5. logDir = fso.GetParentFolderName(WScript.ScriptFullName) & "\test-logs"
  6. If fso.FolderExists(logDir) Then
  7. fso.DeleteFolder logDir, True
  8. End If
  9. Function NewContext(path, httpMethod)
  10. Dim ctx
  11. Set ctx = CreateObject("WscMvc.RequestContext")
  12. ctx.Initialize path, httpMethod, logDir
  13. Set NewContext = ctx
  14. End Function
  15. Sub RunApplication(app, path, httpMethod, applicationName, statusLine, contentType, body, allowHeader)
  16. Dim ctx
  17. Set ctx = NewContext(path, httpMethod)
  18. statusLine = "" : contentType = "" : body = "" : allowHeader = ""
  19. app.Run ctx, applicationName, statusLine, contentType, body, allowHeader
  20. Set ctx = Nothing
  21. End Sub
  22. Sub CheckEqual(actual, expected, label)
  23. If actual = expected Then
  24. WScript.Echo "PASS: " & label
  25. Else
  26. WScript.Echo "FAIL: " & label & " -> expected [" & expected & "] got [" & actual & "]"
  27. pass = False
  28. End If
  29. End Sub
  30. ' --- RequestContext contract ---
  31. Dim ctx1
  32. On Error Resume Next
  33. Set ctx1 = NewContext("/hello", "GET")
  34. If Err.Number <> 0 Then
  35. WScript.Echo "FAIL: could not create/initialize WscMvc.RequestContext - " & Err.Description
  36. pass = False
  37. Err.Clear
  38. End If
  39. On Error Goto 0
  40. If pass Then
  41. CheckEqual ctx1.Path, "/hello", "RequestContext.Path"
  42. CheckEqual ctx1.HttpMethod, "GET", "RequestContext.HttpMethod"
  43. If Len(ctx1.CorrelationId) > 0 Then
  44. WScript.Echo "PASS: RequestContext.CorrelationId non-empty (" & ctx1.CorrelationId & ")"
  45. Else
  46. WScript.Echo "FAIL: RequestContext.CorrelationId empty"
  47. pass = False
  48. End If
  49. If ctx1.ElapsedMs() >= 0 Then
  50. WScript.Echo "PASS: RequestContext.ElapsedMs non-negative (" & ctx1.ElapsedMs() & ")"
  51. Else
  52. WScript.Echo "FAIL: RequestContext.ElapsedMs negative"
  53. pass = False
  54. End If
  55. End If
  56. ' --- Two independent contexts must not share correlation IDs or state ---
  57. Dim ctx2
  58. If pass Then
  59. Set ctx2 = NewContext("/does-not-exist", "GET")
  60. If ctx1.CorrelationId <> ctx2.CorrelationId Then
  61. WScript.Echo "PASS: two RequestContext instances have distinct CorrelationIds"
  62. Else
  63. WScript.Echo "FAIL: two RequestContext instances produced the same CorrelationId - " & ctx1.CorrelationId
  64. pass = False
  65. End If
  66. End If
  67. ' --- Application.Run happy path via ctx ---
  68. Dim app, statusLine, contentType, body, allowHeader
  69. If pass Then
  70. On Error Resume Next
  71. Set app = CreateObject("WscMvc.Application")
  72. If Err.Number <> 0 Then
  73. WScript.Echo "FAIL: could not create WscMvc.Application - " & Err.Description
  74. pass = False
  75. Err.Clear
  76. End If
  77. On Error Goto 0
  78. End If
  79. If pass Then
  80. On Error Resume Next
  81. RunApplication app, "/hello", "GET", "production", statusLine, contentType, body, allowHeader
  82. If Err.Number <> 0 Then
  83. WScript.Echo "FAIL: Application.Run raised error on /hello - " & Err.Description
  84. pass = False
  85. Err.Clear
  86. End If
  87. On Error Goto 0
  88. End If
  89. If pass Then
  90. CheckEqual statusLine, "200 OK", "Application.Run(/hello) statusLine"
  91. CheckEqual contentType, "text/html; charset=utf-8", "Application.Run(/hello) contentType"
  92. CheckEqual body, "Hello from WSC-MVC!", "Application.Run(/hello) body"
  93. CheckEqual allowHeader, "", "Application.Run(/hello) Allow header"
  94. End If
  95. ' --- Application.Run unknown route via ctx (expected 404, not an error) ---
  96. If pass Then
  97. On Error Resume Next
  98. RunApplication app, "/does-not-exist", "GET", "production", statusLine, contentType, body, allowHeader
  99. If Err.Number <> 0 Then
  100. WScript.Echo "FAIL: Application.Run raised error on unknown route - " & Err.Description
  101. pass = False
  102. Err.Clear
  103. End If
  104. On Error Goto 0
  105. End If
  106. If pass Then
  107. CheckEqual statusLine, "404 Not Found", "Application.Run(unknown route) statusLine"
  108. CheckEqual allowHeader, "", "Application.Run(unknown route) Allow header"
  109. End If
  110. ' --- Router method contract: existing route with unsupported method is 405 + Allow ---
  111. If pass Then
  112. On Error Resume Next
  113. RunApplication app, "/hello", "POST", "production", statusLine, contentType, body, allowHeader
  114. If Err.Number <> 0 Then
  115. WScript.Echo "FAIL: Application.Run raised error on method mismatch - " & Err.Description
  116. pass = False
  117. Err.Clear
  118. End If
  119. On Error Goto 0
  120. End If
  121. If pass Then
  122. CheckEqual statusLine, "405 Method Not Allowed", "Application.Run(POST /hello) statusLine"
  123. CheckEqual contentType, "text/plain; charset=utf-8", "Application.Run(POST /hello) contentType"
  124. CheckEqual body, "Method Not Allowed", "Application.Run(POST /hello) body"
  125. CheckEqual allowHeader, "GET", "Application.Run(POST /hello) Allow header"
  126. End If
  127. ' --- Router app isolation and POST support for test app route ---
  128. If pass Then
  129. On Error Resume Next
  130. RunApplication app, "/self-test", "POST", "tests", statusLine, contentType, body, allowHeader
  131. If Err.Number <> 0 Then
  132. WScript.Echo "FAIL: Application.Run raised error on POST /self-test - " & Err.Description
  133. pass = False
  134. Err.Clear
  135. End If
  136. On Error Goto 0
  137. End If
  138. If pass Then
  139. CheckEqual statusLine, "200 OK", "Application.Run(POST /self-test tests) statusLine"
  140. CheckEqual contentType, "application/json; charset=utf-8", "Application.Run(POST /self-test tests) contentType"
  141. CheckEqual allowHeader, "", "Application.Run(POST /self-test tests) Allow header"
  142. End If
  143. ' --- Router malformed path contract ---
  144. If pass Then
  145. On Error Resume Next
  146. RunApplication app, "/hello/../secret", "GET", "production", statusLine, contentType, body, allowHeader
  147. If Err.Number <> 0 Then
  148. WScript.Echo "FAIL: Application.Run raised error on malformed path - " & Err.Description
  149. pass = False
  150. Err.Clear
  151. End If
  152. On Error Goto 0
  153. End If
  154. If pass Then
  155. CheckEqual statusLine, "400 Bad Request", "Application.Run(malformed path) statusLine"
  156. CheckEqual contentType, "text/plain; charset=utf-8", "Application.Run(malformed path) contentType"
  157. CheckEqual body, "Bad Request", "Application.Run(malformed path) body"
  158. CheckEqual allowHeader, "", "Application.Run(malformed path) Allow header"
  159. End If
  160. ' --- Logging: best-effort log file was written with both outcomes ---
  161. If pass Then
  162. Dim logPath, logContent
  163. logPath = logDir & "\app.log"
  164. If fso.FileExists(logPath) Then
  165. Dim logStream
  166. Set logStream = fso.OpenTextFile(logPath, 1)
  167. logContent = logStream.ReadAll
  168. logStream.Close
  169. If InStr(logContent, "200 OK") > 0 And InStr(logContent, "404 Not Found") > 0 Then
  170. WScript.Echo "PASS: app.log contains both 200 OK and 404 Not Found outcomes"
  171. Else
  172. WScript.Echo "FAIL: app.log missing expected outcomes -> " & logContent
  173. pass = False
  174. End If
  175. Else
  176. WScript.Echo "FAIL: app.log was not created at " & logPath
  177. pass = False
  178. End If
  179. End If
  180. Set app = Nothing
  181. Set ctx1 = Nothing
  182. Set ctx2 = Nothing
  183. If fso.FolderExists(logDir) Then
  184. fso.DeleteFolder logDir, True
  185. End If
  186. Set fso = Nothing
  187. If pass Then
  188. WScript.Echo "RESULT: ALL PASS"
  189. WScript.Quit 0
  190. Else
  191. WScript.Echo "RESULT: FAILURE"
  192. WScript.Quit 1
  193. End If

Powered by TurnKey Linux.