unregroute_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "strings"
  19. "testing"
  20. )
  21. //
  22. // The unregroute_test.go contains tests for the unregister route
  23. // functionality, that allows overriding route paths in children project
  24. // that embed parent routers.
  25. //
  26. const contentRootOriginal = "ok-original-root"
  27. const contentLevel1Original = "ok-original-level1"
  28. const contentLevel2Original = "ok-original-level2"
  29. const contentRootReplacement = "ok-replacement-root"
  30. const contentLevel1Replacement = "ok-replacement-level1"
  31. const contentLevel2Replacement = "ok-replacement-level2"
  32. // TestPreUnregController will supply content for the original routes,
  33. // before unregistration
  34. type TestPreUnregController struct {
  35. Controller
  36. }
  37. func (tc *TestPreUnregController) GetFixedRoot() {
  38. tc.Ctx.Output.Body([]byte(contentRootOriginal))
  39. }
  40. func (tc *TestPreUnregController) GetFixedLevel1() {
  41. tc.Ctx.Output.Body([]byte(contentLevel1Original))
  42. }
  43. func (tc *TestPreUnregController) GetFixedLevel2() {
  44. tc.Ctx.Output.Body([]byte(contentLevel2Original))
  45. }
  46. // TestPostUnregController will supply content for the overriding routes,
  47. // after the original ones are unregistered.
  48. type TestPostUnregController struct {
  49. Controller
  50. }
  51. func (tc *TestPostUnregController) GetFixedRoot() {
  52. tc.Ctx.Output.Body([]byte(contentRootReplacement))
  53. }
  54. func (tc *TestPostUnregController) GetFixedLevel1() {
  55. tc.Ctx.Output.Body([]byte(contentLevel1Replacement))
  56. }
  57. func (tc *TestPostUnregController) GetFixedLevel2() {
  58. tc.Ctx.Output.Body([]byte(contentLevel2Replacement))
  59. }
  60. // TestUnregisterFixedRouteRoot replaces just the root fixed route path.
  61. // In this case, for a path like "/level1/level2" or "/level1", those actions
  62. // should remain intact, and continue to serve the original content.
  63. func TestUnregisterFixedRouteRoot(t *testing.T) {
  64. var method = "GET"
  65. handler := NewControllerRegister()
  66. handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot")
  67. handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1")
  68. handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2")
  69. // Test original root
  70. testHelperFnContentCheck(t, handler, "Test original root",
  71. method, "/", contentRootOriginal)
  72. // Test original level 1
  73. testHelperFnContentCheck(t, handler, "Test original level 1",
  74. method, "/level1", contentLevel1Original)
  75. // Test original level 2
  76. testHelperFnContentCheck(t, handler, "Test original level 2",
  77. method, "/level1/level2", contentLevel2Original)
  78. // Remove only the root path
  79. findAndRemoveSingleTree(handler.routers[method])
  80. // Replace the root path TestPreUnregController action with the action from
  81. // TestPostUnregController
  82. handler.Add("/", &TestPostUnregController{}, "get:GetFixedRoot")
  83. // Test replacement root (expect change)
  84. testHelperFnContentCheck(t, handler, "Test replacement root (expect change)", method, "/", contentRootReplacement)
  85. // Test level 1 (expect no change from the original)
  86. testHelperFnContentCheck(t, handler, "Test level 1 (expect no change from the original)", method, "/level1", contentLevel1Original)
  87. // Test level 2 (expect no change from the original)
  88. testHelperFnContentCheck(t, handler, "Test level 2 (expect no change from the original)", method, "/level1/level2", contentLevel2Original)
  89. }
  90. // TestUnregisterFixedRouteLevel1 replaces just the "/level1" fixed route path.
  91. // In this case, for a path like "/level1/level2" or "/", those actions
  92. // should remain intact, and continue to serve the original content.
  93. func TestUnregisterFixedRouteLevel1(t *testing.T) {
  94. var method = "GET"
  95. handler := NewControllerRegister()
  96. handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot")
  97. handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1")
  98. handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2")
  99. // Test original root
  100. testHelperFnContentCheck(t, handler,
  101. "TestUnregisterFixedRouteLevel1.Test original root",
  102. method, "/", contentRootOriginal)
  103. // Test original level 1
  104. testHelperFnContentCheck(t, handler,
  105. "TestUnregisterFixedRouteLevel1.Test original level 1",
  106. method, "/level1", contentLevel1Original)
  107. // Test original level 2
  108. testHelperFnContentCheck(t, handler,
  109. "TestUnregisterFixedRouteLevel1.Test original level 2",
  110. method, "/level1/level2", contentLevel2Original)
  111. // Remove only the level1 path
  112. subPaths := splitPath("/level1")
  113. if handler.routers[method].prefix == strings.Trim("/level1", "/ ") {
  114. findAndRemoveSingleTree(handler.routers[method])
  115. } else {
  116. findAndRemoveTree(subPaths, handler.routers[method], method)
  117. }
  118. // Replace the "level1" path TestPreUnregController action with the action from
  119. // TestPostUnregController
  120. handler.Add("/level1", &TestPostUnregController{}, "get:GetFixedLevel1")
  121. // Test replacement root (expect no change from the original)
  122. testHelperFnContentCheck(t, handler, "Test replacement root (expect no change from the original)", method, "/", contentRootOriginal)
  123. // Test level 1 (expect change)
  124. testHelperFnContentCheck(t, handler, "Test level 1 (expect change)", method, "/level1", contentLevel1Replacement)
  125. // Test level 2 (expect no change from the original)
  126. testHelperFnContentCheck(t, handler, "Test level 2 (expect no change from the original)", method, "/level1/level2", contentLevel2Original)
  127. }
  128. // TestUnregisterFixedRouteLevel2 unregisters just the "/level1/level2" fixed
  129. // route path. In this case, for a path like "/level1" or "/", those actions
  130. // should remain intact, and continue to serve the original content.
  131. func TestUnregisterFixedRouteLevel2(t *testing.T) {
  132. var method = "GET"
  133. handler := NewControllerRegister()
  134. handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot")
  135. handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1")
  136. handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2")
  137. // Test original root
  138. testHelperFnContentCheck(t, handler,
  139. "TestUnregisterFixedRouteLevel1.Test original root",
  140. method, "/", contentRootOriginal)
  141. // Test original level 1
  142. testHelperFnContentCheck(t, handler,
  143. "TestUnregisterFixedRouteLevel1.Test original level 1",
  144. method, "/level1", contentLevel1Original)
  145. // Test original level 2
  146. testHelperFnContentCheck(t, handler,
  147. "TestUnregisterFixedRouteLevel1.Test original level 2",
  148. method, "/level1/level2", contentLevel2Original)
  149. // Remove only the level2 path
  150. subPaths := splitPath("/level1/level2")
  151. if handler.routers[method].prefix == strings.Trim("/level1/level2", "/ ") {
  152. findAndRemoveSingleTree(handler.routers[method])
  153. } else {
  154. findAndRemoveTree(subPaths, handler.routers[method], method)
  155. }
  156. // Replace the "/level1/level2" path TestPreUnregController action with the action from
  157. // TestPostUnregController
  158. handler.Add("/level1/level2", &TestPostUnregController{}, "get:GetFixedLevel2")
  159. // Test replacement root (expect no change from the original)
  160. testHelperFnContentCheck(t, handler, "Test replacement root (expect no change from the original)", method, "/", contentRootOriginal)
  161. // Test level 1 (expect no change from the original)
  162. testHelperFnContentCheck(t, handler, "Test level 1 (expect no change from the original)", method, "/level1", contentLevel1Original)
  163. // Test level 2 (expect change)
  164. testHelperFnContentCheck(t, handler, "Test level 2 (expect change)", method, "/level1/level2", contentLevel2Replacement)
  165. }
  166. func testHelperFnContentCheck(t *testing.T, handler *ControllerRegister,
  167. testName, method, path, expectedBodyContent string) {
  168. r, err := http.NewRequest(method, path, nil)
  169. if err != nil {
  170. t.Errorf("httpRecorderBodyTest NewRequest error: %v", err)
  171. return
  172. }
  173. w := httptest.NewRecorder()
  174. handler.ServeHTTP(w, r)
  175. body := w.Body.String()
  176. if body != expectedBodyContent {
  177. t.Errorf("%s: expected [%s], got [%s];", testName, expectedBodyContent, body)
  178. }
  179. }