Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

178 lignes
6.7KB

  1. <!-- #include file="../aspunit/Lib/ASPUnit.asp" -->
  2. <!-- #include file="../bootstrap.asp" -->
  3. <!-- #include file="../../core/lib.ControllerFactory.asp" -->
  4. <!-- #include file="../../core/lib.Routes.asp" -->
  5. <%
  6. Class TestDispatchController_Class
  7. Private m_useLayout
  8. Private Sub Class_Initialize()
  9. m_useLayout = False
  10. End Sub
  11. Public Property Get useLayout
  12. useLayout = m_useLayout
  13. End Property
  14. Public Property Let useLayout(value)
  15. m_useLayout = value
  16. End Property
  17. Public Sub Smoke()
  18. dispatchActionRan = True
  19. End Sub
  20. End Class
  21. Dim TestDispatchController_Class__Singleton
  22. Function TestDispatchController()
  23. If IsEmpty(TestDispatchController_Class__Singleton) Then
  24. Set TestDispatchController_Class__Singleton = New TestDispatchController_Class
  25. End If
  26. Set TestDispatchController = TestDispatchController_Class__Singleton
  27. End Function
  28. ' Named distinctly from TestDispatchController so the "Controller" suffix
  29. ' stripping performed by MVC.ControllerName is unambiguous to assert on.
  30. Class TitleTestController_Class
  31. Private m_useLayout
  32. Private Sub Class_Initialize()
  33. m_useLayout = False
  34. End Sub
  35. Public Property Get useLayout
  36. useLayout = m_useLayout
  37. End Property
  38. Public Property Let useLayout(value)
  39. m_useLayout = value
  40. End Property
  41. Public Sub Smoke()
  42. dispatchActionRan = True
  43. End Sub
  44. End Class
  45. Dim TitleTestController_Class__Singleton
  46. Function TitleTestController()
  47. If IsEmpty(TitleTestController_Class__Singleton) Then
  48. Set TitleTestController_Class__Singleton = New TitleTestController_Class
  49. End If
  50. Set TitleTestController = TitleTestController_Class__Singleton
  51. End Function
  52. ' MVC is now a Windows Script Component (core/mvc.wsc) with its own isolated
  53. ' script engine, so - like router.wsc via EnsureTestRouter() below - it is
  54. ' instantiated once and has its dependencies (Router, ControllerRegistry,
  55. ' ControllerFactory, Routes) injected explicitly rather than reaching into
  56. ' this page's globals itself.
  57. Dim MVC_Instance
  58. Function MVC()
  59. Set MVC = MVC_Instance
  60. End Function
  61. Sub EnsureTestMvc()
  62. Call EnsureTestRouter()
  63. ' IsObject(Nothing) is True in VBScript, so - matching EnsureTestRouter()
  64. ' above - both conditions are needed: "never assigned" and "explicitly
  65. ' reset to Nothing" are different states here.
  66. If (Not IsObject(MVC_Instance)) Then
  67. Set MVC_Instance = GetObject("script:" & ResolveProjectPath("core\\mvc.wsc"))
  68. ElseIf MVC_Instance Is Nothing Then
  69. Set MVC_Instance = GetObject("script:" & ResolveProjectPath("core\\mvc.wsc"))
  70. End If
  71. Set MVC_Instance.Router = router
  72. Set MVC_Instance.ControllerRegistry = ControllerRegistry()
  73. Set MVC_Instance.ControllerFactory = ControllerFactory()
  74. Set MVC_Instance.Routes = Routes()
  75. End Sub
  76. Call ASPUnit.AddModule( _
  77. ASPUnit.CreateModule( _
  78. "MVC Dispatch Smoke Tests", _
  79. Array( _
  80. ASPUnit.CreateTest("RootRouteResolvesToHomeController"), _
  81. ASPUnit.CreateTest("KnownRouteDispatchCompletesWithoutLookupFailure"), _
  82. ASPUnit.CreateTest("ControllerNameReturnsDispatchedControllerNameAsString"), _
  83. ASPUnit.CreateTest("ControllerNameFeedsUrlGenerationForRedirects") _
  84. ), _
  85. ASPUnit.CreateLifeCycle("SetupMvcDispatch", "TeardownMvcDispatch") _
  86. ) _
  87. )
  88. Call ASPUnit.Run()
  89. Sub SetupMvcDispatch()
  90. Call ResetTestRuntime()
  91. On Error Resume Next
  92. TestDispatchController_Class__Singleton = Empty
  93. TitleTestController_Class__Singleton = Empty
  94. Set MVC_Instance = Nothing
  95. On Error GoTo 0
  96. Call ExecuteGlobal("Dim dispatchActionRan")
  97. dispatchActionRan = False
  98. Call RegisterDefaultRoutes()
  99. Call EnsureTestMvc()
  100. Call ControllerRegistry().RegisterController("testdispatchcontroller")
  101. Call router.AddRoute("GET", "/dispatch-smoke", "testdispatchcontroller", "Smoke")
  102. Call ControllerFactory().Register("testdispatchcontroller", GetRef("TestDispatchController"))
  103. Call ControllerRegistry().RegisterController("titletestcontroller")
  104. Call router.AddRoute("GET", "/title-test", "TitleTestController", "Smoke")
  105. Call ControllerFactory().Register("TitleTestController", GetRef("TitleTestController"))
  106. End Sub
  107. Sub TeardownMvcDispatch()
  108. On Error Resume Next
  109. Set MVC_Instance = Nothing
  110. TestDispatchController_Class__Singleton = Empty
  111. TitleTestController_Class__Singleton = Empty
  112. Response.Status = "200 OK"
  113. On Error GoTo 0
  114. Call ResetTestRuntime()
  115. End Sub
  116. Function RootRouteResolvesToHomeController()
  117. Dim routeArray
  118. routeArray = router.Resolve("GET", "/")
  119. Call ASPUnit.Ok((LCase(routeArray(0)) = "homecontroller" And LCase(routeArray(1)) = "index"), "Root route should resolve to HomeController.Index")
  120. End Function
  121. Function KnownRouteDispatchCompletesWithoutLookupFailure()
  122. Call MVC().DispatchRequest("GET", "/dispatch-smoke")
  123. Call ASPUnit.Ok(dispatchActionRan, "Dispatch should reach a registered controller action without whitelist or lookup failures")
  124. End Function
  125. ' Regression test: MVC.ControllerName used to do "ControllerName = CurrentController",
  126. ' assigning an object to a property return without Set. Reading MVC.ControllerName
  127. ' after any controller had been dispatched raised "Object doesn't support this
  128. ' property or method" instead of returning a value. This confirms it now returns
  129. ' the dispatched controller's name as a plain string, with the "Controller" suffix
  130. ' stripped (matching the convention Active() and *StylesheetTag already rely on).
  131. Function ControllerNameReturnsDispatchedControllerNameAsString()
  132. Call MVC().DispatchRequest("GET", "/title-test")
  133. Dim controllerNameValue
  134. controllerNameValue = MVC().ControllerName
  135. Call ASPUnit.Ok((VarType(controllerNameValue) = vbString), "MVC.ControllerName should return a String, not the controller object")
  136. Call ASPUnit.Equal(controllerNameValue, "TitleTest", "MVC.ControllerName should return the dispatched controller's name with the Controller suffix stripped")
  137. End Function
  138. ' ControllerStylesheetTag, RedirectToAction, and the CSRF-invalid-token redirect
  139. ' helper all build a URL/asset path from MVC.ControllerName. This confirms that
  140. ' downstream use actually works now that ControllerName returns a real string,
  141. ' without needing to trigger a live Response.Redirect inside the test run.
  142. Function ControllerNameFeedsUrlGenerationForRedirects()
  143. Call MVC().DispatchRequest("GET", "/title-test")
  144. Dim generatedUrl
  145. generatedUrl = Routes.UrlTo(MVC().ControllerName, "Smoke", Empty)
  146. Call ASPUnit.Ok((InStr(LCase(generatedUrl), "/titletest/smoke") > 0), "URL built from MVC.ControllerName should contain the lowercase controller/action path, got: " & generatedUrl)
  147. End Function
  148. %>

Powered by TurnKey Linux.