Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

153 linhas
4.5KB

  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 CheckEqual(actual, expected, label)
  16. If actual = expected Then
  17. WScript.Echo "PASS: " & label
  18. Else
  19. WScript.Echo "FAIL: " & label & " -> expected [" & expected & "] got [" & actual & "]"
  20. pass = False
  21. End If
  22. End Sub
  23. ' --- RequestContext contract ---
  24. Dim ctx1
  25. On Error Resume Next
  26. Set ctx1 = NewContext("/hello", "GET")
  27. If Err.Number <> 0 Then
  28. WScript.Echo "FAIL: could not create/initialize WscMvc.RequestContext - " & Err.Description
  29. pass = False
  30. Err.Clear
  31. End If
  32. On Error Goto 0
  33. If pass Then
  34. CheckEqual ctx1.Path, "/hello", "RequestContext.Path"
  35. CheckEqual ctx1.HttpMethod, "GET", "RequestContext.HttpMethod"
  36. If Len(ctx1.CorrelationId) > 0 Then
  37. WScript.Echo "PASS: RequestContext.CorrelationId non-empty (" & ctx1.CorrelationId & ")"
  38. Else
  39. WScript.Echo "FAIL: RequestContext.CorrelationId empty"
  40. pass = False
  41. End If
  42. If ctx1.ElapsedMs() >= 0 Then
  43. WScript.Echo "PASS: RequestContext.ElapsedMs non-negative (" & ctx1.ElapsedMs() & ")"
  44. Else
  45. WScript.Echo "FAIL: RequestContext.ElapsedMs negative"
  46. pass = False
  47. End If
  48. End If
  49. ' --- Two independent contexts must not share correlation IDs or state ---
  50. Dim ctx2
  51. If pass Then
  52. Set ctx2 = NewContext("/does-not-exist", "GET")
  53. If ctx1.CorrelationId <> ctx2.CorrelationId Then
  54. WScript.Echo "PASS: two RequestContext instances have distinct CorrelationIds"
  55. Else
  56. WScript.Echo "FAIL: two RequestContext instances produced the same CorrelationId - " & ctx1.CorrelationId
  57. pass = False
  58. End If
  59. End If
  60. ' --- Application.Run happy path via ctx ---
  61. Dim app, statusLine, contentType, body
  62. If pass Then
  63. On Error Resume Next
  64. Set app = CreateObject("WscMvc.Application")
  65. If Err.Number <> 0 Then
  66. WScript.Echo "FAIL: could not create WscMvc.Application - " & Err.Description
  67. pass = False
  68. Err.Clear
  69. End If
  70. On Error Goto 0
  71. End If
  72. If pass Then
  73. statusLine = "" : contentType = "" : body = ""
  74. On Error Resume Next
  75. app.Run ctx1, "production", statusLine, contentType, body
  76. If Err.Number <> 0 Then
  77. WScript.Echo "FAIL: Application.Run raised error on /hello - " & Err.Description
  78. pass = False
  79. Err.Clear
  80. End If
  81. On Error Goto 0
  82. End If
  83. If pass Then
  84. CheckEqual statusLine, "200 OK", "Application.Run(/hello) statusLine"
  85. CheckEqual contentType, "text/html; charset=utf-8", "Application.Run(/hello) contentType"
  86. CheckEqual body, "Hello from WSC-MVC!", "Application.Run(/hello) body"
  87. End If
  88. ' --- Application.Run unknown route via ctx (expected 404, not an error) ---
  89. If pass Then
  90. statusLine = "" : contentType = "" : body = ""
  91. On Error Resume Next
  92. app.Run ctx2, "production", statusLine, contentType, body
  93. If Err.Number <> 0 Then
  94. WScript.Echo "FAIL: Application.Run raised error on unknown route - " & Err.Description
  95. pass = False
  96. Err.Clear
  97. End If
  98. On Error Goto 0
  99. End If
  100. If pass Then
  101. CheckEqual statusLine, "404 Not Found", "Application.Run(unknown route) statusLine"
  102. End If
  103. ' --- Logging: best-effort log file was written with both outcomes ---
  104. If pass Then
  105. Dim logPath, logContent
  106. logPath = logDir & "\app.log"
  107. If fso.FileExists(logPath) Then
  108. Dim logStream
  109. Set logStream = fso.OpenTextFile(logPath, 1)
  110. logContent = logStream.ReadAll
  111. logStream.Close
  112. If InStr(logContent, "200 OK") > 0 And InStr(logContent, "404 Not Found") > 0 Then
  113. WScript.Echo "PASS: app.log contains both 200 OK and 404 Not Found outcomes"
  114. Else
  115. WScript.Echo "FAIL: app.log missing expected outcomes -> " & logContent
  116. pass = False
  117. End If
  118. Else
  119. WScript.Echo "FAIL: app.log was not created at " & logPath
  120. pass = False
  121. End If
  122. End If
  123. Set app = Nothing
  124. Set ctx1 = Nothing
  125. Set ctx2 = Nothing
  126. If fso.FolderExists(logDir) Then
  127. fso.DeleteFolder logDir, True
  128. End If
  129. Set fso = Nothing
  130. If pass Then
  131. WScript.Echo "RESULT: ALL PASS"
  132. WScript.Quit 0
  133. Else
  134. WScript.Echo "RESULT: FAILURE"
  135. WScript.Quit 1
  136. End If

Powered by TurnKey Linux.