github.com/astaxie/beego@v1.12.3/unregroute_test.go (about) 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 15 package beego 16 17 import ( 18 "net/http" 19 "net/http/httptest" 20 "strings" 21 "testing" 22 ) 23 24 // 25 // The unregroute_test.go contains tests for the unregister route 26 // functionality, that allows overriding route paths in children project 27 // that embed parent routers. 28 // 29 30 const contentRootOriginal = "ok-original-root" 31 const contentLevel1Original = "ok-original-level1" 32 const contentLevel2Original = "ok-original-level2" 33 34 const contentRootReplacement = "ok-replacement-root" 35 const contentLevel1Replacement = "ok-replacement-level1" 36 const contentLevel2Replacement = "ok-replacement-level2" 37 38 // TestPreUnregController will supply content for the original routes, 39 // before unregistration 40 type TestPreUnregController struct { 41 Controller 42 } 43 44 func (tc *TestPreUnregController) GetFixedRoot() { 45 tc.Ctx.Output.Body([]byte(contentRootOriginal)) 46 } 47 func (tc *TestPreUnregController) GetFixedLevel1() { 48 tc.Ctx.Output.Body([]byte(contentLevel1Original)) 49 } 50 func (tc *TestPreUnregController) GetFixedLevel2() { 51 tc.Ctx.Output.Body([]byte(contentLevel2Original)) 52 } 53 54 // TestPostUnregController will supply content for the overriding routes, 55 // after the original ones are unregistered. 56 type TestPostUnregController struct { 57 Controller 58 } 59 60 func (tc *TestPostUnregController) GetFixedRoot() { 61 tc.Ctx.Output.Body([]byte(contentRootReplacement)) 62 } 63 func (tc *TestPostUnregController) GetFixedLevel1() { 64 tc.Ctx.Output.Body([]byte(contentLevel1Replacement)) 65 } 66 func (tc *TestPostUnregController) GetFixedLevel2() { 67 tc.Ctx.Output.Body([]byte(contentLevel2Replacement)) 68 } 69 70 // TestUnregisterFixedRouteRoot replaces just the root fixed route path. 71 // In this case, for a path like "/level1/level2" or "/level1", those actions 72 // should remain intact, and continue to serve the original content. 73 func TestUnregisterFixedRouteRoot(t *testing.T) { 74 75 var method = "GET" 76 77 handler := NewControllerRegister() 78 handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot") 79 handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1") 80 handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2") 81 82 // Test original root 83 testHelperFnContentCheck(t, handler, "Test original root", 84 method, "/", contentRootOriginal) 85 86 // Test original level 1 87 testHelperFnContentCheck(t, handler, "Test original level 1", 88 method, "/level1", contentLevel1Original) 89 90 // Test original level 2 91 testHelperFnContentCheck(t, handler, "Test original level 2", 92 method, "/level1/level2", contentLevel2Original) 93 94 // Remove only the root path 95 findAndRemoveSingleTree(handler.routers[method]) 96 97 // Replace the root path TestPreUnregController action with the action from 98 // TestPostUnregController 99 handler.Add("/", &TestPostUnregController{}, "get:GetFixedRoot") 100 101 // Test replacement root (expect change) 102 testHelperFnContentCheck(t, handler, "Test replacement root (expect change)", method, "/", contentRootReplacement) 103 104 // Test level 1 (expect no change from the original) 105 testHelperFnContentCheck(t, handler, "Test level 1 (expect no change from the original)", method, "/level1", contentLevel1Original) 106 107 // Test level 2 (expect no change from the original) 108 testHelperFnContentCheck(t, handler, "Test level 2 (expect no change from the original)", method, "/level1/level2", contentLevel2Original) 109 110 } 111 112 // TestUnregisterFixedRouteLevel1 replaces just the "/level1" fixed route path. 113 // In this case, for a path like "/level1/level2" or "/", those actions 114 // should remain intact, and continue to serve the original content. 115 func TestUnregisterFixedRouteLevel1(t *testing.T) { 116 117 var method = "GET" 118 119 handler := NewControllerRegister() 120 handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot") 121 handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1") 122 handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2") 123 124 // Test original root 125 testHelperFnContentCheck(t, handler, 126 "TestUnregisterFixedRouteLevel1.Test original root", 127 method, "/", contentRootOriginal) 128 129 // Test original level 1 130 testHelperFnContentCheck(t, handler, 131 "TestUnregisterFixedRouteLevel1.Test original level 1", 132 method, "/level1", contentLevel1Original) 133 134 // Test original level 2 135 testHelperFnContentCheck(t, handler, 136 "TestUnregisterFixedRouteLevel1.Test original level 2", 137 method, "/level1/level2", contentLevel2Original) 138 139 // Remove only the level1 path 140 subPaths := splitPath("/level1") 141 if handler.routers[method].prefix == strings.Trim("/level1", "/ ") { 142 findAndRemoveSingleTree(handler.routers[method]) 143 } else { 144 findAndRemoveTree(subPaths, handler.routers[method], method) 145 } 146 147 // Replace the "level1" path TestPreUnregController action with the action from 148 // TestPostUnregController 149 handler.Add("/level1", &TestPostUnregController{}, "get:GetFixedLevel1") 150 151 // Test replacement root (expect no change from the original) 152 testHelperFnContentCheck(t, handler, "Test replacement root (expect no change from the original)", method, "/", contentRootOriginal) 153 154 // Test level 1 (expect change) 155 testHelperFnContentCheck(t, handler, "Test level 1 (expect change)", method, "/level1", contentLevel1Replacement) 156 157 // Test level 2 (expect no change from the original) 158 testHelperFnContentCheck(t, handler, "Test level 2 (expect no change from the original)", method, "/level1/level2", contentLevel2Original) 159 160 } 161 162 // TestUnregisterFixedRouteLevel2 unregisters just the "/level1/level2" fixed 163 // route path. In this case, for a path like "/level1" or "/", those actions 164 // should remain intact, and continue to serve the original content. 165 func TestUnregisterFixedRouteLevel2(t *testing.T) { 166 167 var method = "GET" 168 169 handler := NewControllerRegister() 170 handler.Add("/", &TestPreUnregController{}, "get:GetFixedRoot") 171 handler.Add("/level1", &TestPreUnregController{}, "get:GetFixedLevel1") 172 handler.Add("/level1/level2", &TestPreUnregController{}, "get:GetFixedLevel2") 173 174 // Test original root 175 testHelperFnContentCheck(t, handler, 176 "TestUnregisterFixedRouteLevel1.Test original root", 177 method, "/", contentRootOriginal) 178 179 // Test original level 1 180 testHelperFnContentCheck(t, handler, 181 "TestUnregisterFixedRouteLevel1.Test original level 1", 182 method, "/level1", contentLevel1Original) 183 184 // Test original level 2 185 testHelperFnContentCheck(t, handler, 186 "TestUnregisterFixedRouteLevel1.Test original level 2", 187 method, "/level1/level2", contentLevel2Original) 188 189 // Remove only the level2 path 190 subPaths := splitPath("/level1/level2") 191 if handler.routers[method].prefix == strings.Trim("/level1/level2", "/ ") { 192 findAndRemoveSingleTree(handler.routers[method]) 193 } else { 194 findAndRemoveTree(subPaths, handler.routers[method], method) 195 } 196 197 // Replace the "/level1/level2" path TestPreUnregController action with the action from 198 // TestPostUnregController 199 handler.Add("/level1/level2", &TestPostUnregController{}, "get:GetFixedLevel2") 200 201 // Test replacement root (expect no change from the original) 202 testHelperFnContentCheck(t, handler, "Test replacement root (expect no change from the original)", method, "/", contentRootOriginal) 203 204 // Test level 1 (expect no change from the original) 205 testHelperFnContentCheck(t, handler, "Test level 1 (expect no change from the original)", method, "/level1", contentLevel1Original) 206 207 // Test level 2 (expect change) 208 testHelperFnContentCheck(t, handler, "Test level 2 (expect change)", method, "/level1/level2", contentLevel2Replacement) 209 210 } 211 212 func testHelperFnContentCheck(t *testing.T, handler *ControllerRegister, 213 testName, method, path, expectedBodyContent string) { 214 215 r, err := http.NewRequest(method, path, nil) 216 if err != nil { 217 t.Errorf("httpRecorderBodyTest NewRequest error: %v", err) 218 return 219 } 220 w := httptest.NewRecorder() 221 handler.ServeHTTP(w, r) 222 body := w.Body.String() 223 if body != expectedBodyContent { 224 t.Errorf("%s: expected [%s], got [%s];", testName, expectedBodyContent, body) 225 } 226 }