github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/l7policies/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/l7policies" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 // SingleL7PolicyBody is the canned body of a Get request on an existing l7policy. 14 const SingleL7PolicyBody = ` 15 { 16 "l7policy": { 17 "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d", 18 "description": "", 19 "admin_state_up": true, 20 "redirect_pool_id": null, 21 "redirect_url": "http://www.example.com", 22 "action": "REDIRECT_TO_URL", 23 "position": 1, 24 "project_id": "e3cd678b11784734bc366148aa37580e", 25 "id": "8a1412f0-4c32-4257-8b07-af4770b604fd", 26 "name": "redirect-example.com", 27 "rules": [] 28 } 29 } 30 ` 31 32 var ( 33 L7PolicyToURL = l7policies.L7Policy{ 34 ID: "8a1412f0-4c32-4257-8b07-af4770b604fd", 35 Name: "redirect-example.com", 36 ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 37 Action: "REDIRECT_TO_URL", 38 Position: 1, 39 Description: "", 40 ProjectID: "e3cd678b11784734bc366148aa37580e", 41 RedirectPoolID: "", 42 RedirectURL: "http://www.example.com", 43 AdminStateUp: true, 44 Rules: []l7policies.Rule{}, 45 } 46 L7PolicyToPool = l7policies.L7Policy{ 47 ID: "964f4ba4-f6cd-405c-bebd-639460af7231", 48 Name: "redirect-pool", 49 ListenerID: "be3138a3-5cf7-4513-a4c2-bb137e668bab", 50 Action: "REDIRECT_TO_POOL", 51 Position: 1, 52 Description: "", 53 ProjectID: "c1f7910086964990847dc6c8b128f63c", 54 RedirectPoolID: "bac433c6-5bea-4311-80da-bd1cd90fbd25", 55 RedirectURL: "", 56 AdminStateUp: true, 57 Rules: []l7policies.Rule{}, 58 } 59 L7PolicyUpdated = l7policies.L7Policy{ 60 ID: "8a1412f0-4c32-4257-8b07-af4770b604fd", 61 Name: "NewL7PolicyName", 62 ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 63 Action: "REDIRECT_TO_URL", 64 Position: 1, 65 Description: "Redirect requests to example.com", 66 ProjectID: "e3cd678b11784734bc366148aa37580e", 67 RedirectPoolID: "", 68 RedirectURL: "http://www.new-example.com", 69 AdminStateUp: true, 70 Rules: []l7policies.Rule{}, 71 } 72 L7PolicyNullRedirectURLUpdated = l7policies.L7Policy{ 73 ID: "8a1412f0-4c32-4257-8b07-af4770b604fd", 74 Name: "NewL7PolicyName", 75 ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 76 Action: "REDIRECT_TO_URL", 77 Position: 1, 78 Description: "Redirect requests to example.com", 79 ProjectID: "e3cd678b11784734bc366148aa37580e", 80 RedirectPoolID: "", 81 RedirectURL: "", 82 AdminStateUp: true, 83 Rules: []l7policies.Rule{}, 84 } 85 RulePath = l7policies.Rule{ 86 ID: "16621dbb-a736-4888-a57a-3ecd53df784c", 87 RuleType: "PATH", 88 CompareType: "REGEX", 89 Value: "/images*", 90 ProjectID: "e3cd678b11784734bc366148aa37580e", 91 Key: "", 92 Invert: true, 93 AdminStateUp: true, 94 } 95 RuleHostName = l7policies.Rule{ 96 ID: "d24521a0-df84-4468-861a-a531af116d1e", 97 RuleType: "HOST_NAME", 98 CompareType: "EQUAL_TO", 99 Value: "www.example.com", 100 ProjectID: "e3cd678b11784734bc366148aa37580e", 101 Key: "", 102 Invert: false, 103 AdminStateUp: true, 104 } 105 RuleUpdated = l7policies.Rule{ 106 ID: "16621dbb-a736-4888-a57a-3ecd53df784c", 107 RuleType: "PATH", 108 CompareType: "REGEX", 109 Value: "/images/special*", 110 ProjectID: "e3cd678b11784734bc366148aa37580e", 111 Key: "", 112 Invert: false, 113 AdminStateUp: true, 114 } 115 ) 116 117 // HandleL7PolicyCreationSuccessfully sets up the test server to respond to a l7policy creation request 118 // with a given response. 119 func HandleL7PolicyCreationSuccessfully(t *testing.T, response string) { 120 th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) { 121 th.TestMethod(t, r, "POST") 122 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 123 th.TestJSONRequest(t, r, `{ 124 "l7policy": { 125 "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d", 126 "redirect_url": "http://www.example.com", 127 "name": "redirect-example.com", 128 "action": "REDIRECT_TO_URL" 129 } 130 }`) 131 132 w.WriteHeader(http.StatusAccepted) 133 w.Header().Add("Content-Type", "application/json") 134 fmt.Fprintf(w, response) 135 }) 136 } 137 138 // L7PoliciesListBody contains the canned body of a l7policy list response. 139 const L7PoliciesListBody = ` 140 { 141 "l7policies": [ 142 { 143 "redirect_pool_id": null, 144 "description": "", 145 "admin_state_up": true, 146 "rules": [], 147 "project_id": "e3cd678b11784734bc366148aa37580e", 148 "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d", 149 "redirect_url": "http://www.example.com", 150 "action": "REDIRECT_TO_URL", 151 "position": 1, 152 "id": "8a1412f0-4c32-4257-8b07-af4770b604fd", 153 "name": "redirect-example.com" 154 }, 155 { 156 "redirect_pool_id": "bac433c6-5bea-4311-80da-bd1cd90fbd25", 157 "description": "", 158 "admin_state_up": true, 159 "rules": [], 160 "project_id": "c1f7910086964990847dc6c8b128f63c", 161 "listener_id": "be3138a3-5cf7-4513-a4c2-bb137e668bab", 162 "action": "REDIRECT_TO_POOL", 163 "position": 1, 164 "id": "964f4ba4-f6cd-405c-bebd-639460af7231", 165 "name": "redirect-pool" 166 } 167 ] 168 } 169 ` 170 171 // PostUpdateL7PolicyBody is the canned response body of a Update request on an existing l7policy. 172 const PostUpdateL7PolicyBody = ` 173 { 174 "l7policy": { 175 "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d", 176 "description": "Redirect requests to example.com", 177 "admin_state_up": true, 178 "redirect_url": "http://www.new-example.com", 179 "action": "REDIRECT_TO_URL", 180 "position": 1, 181 "project_id": "e3cd678b11784734bc366148aa37580e", 182 "id": "8a1412f0-4c32-4257-8b07-af4770b604fd", 183 "name": "NewL7PolicyName", 184 "rules": [] 185 } 186 } 187 ` 188 189 // PostUpdateL7PolicyNullRedirectURLBody is the canned response body of a Update request 190 // on an existing l7policy with a null redirect_url . 191 const PostUpdateL7PolicyNullRedirectURLBody = ` 192 { 193 "l7policy": { 194 "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d", 195 "description": "Redirect requests to example.com", 196 "admin_state_up": true, 197 "redirect_url": null, 198 "action": "REDIRECT_TO_URL", 199 "position": 1, 200 "project_id": "e3cd678b11784734bc366148aa37580e", 201 "id": "8a1412f0-4c32-4257-8b07-af4770b604fd", 202 "name": "NewL7PolicyName", 203 "rules": [] 204 } 205 } 206 ` 207 208 // HandleL7PolicyListSuccessfully sets up the test server to respond to a l7policy List request. 209 func HandleL7PolicyListSuccessfully(t *testing.T) { 210 th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "GET") 212 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 213 214 w.Header().Add("Content-Type", "application/json") 215 r.ParseForm() 216 marker := r.Form.Get("marker") 217 switch marker { 218 case "": 219 fmt.Fprintf(w, L7PoliciesListBody) 220 case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab": 221 fmt.Fprintf(w, `{ "l7policies": [] }`) 222 default: 223 t.Fatalf("/v2.0/lbaas/l7policies invoked with unexpected marker=[%s]", marker) 224 } 225 }) 226 } 227 228 // HandleL7PolicyGetSuccessfully sets up the test server to respond to a l7policy Get request. 229 func HandleL7PolicyGetSuccessfully(t *testing.T) { 230 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) { 231 th.TestMethod(t, r, "GET") 232 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 233 th.TestHeader(t, r, "Accept", "application/json") 234 235 fmt.Fprintf(w, SingleL7PolicyBody) 236 }) 237 } 238 239 // HandleL7PolicyDeletionSuccessfully sets up the test server to respond to a l7policy deletion request. 240 func HandleL7PolicyDeletionSuccessfully(t *testing.T) { 241 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) { 242 th.TestMethod(t, r, "DELETE") 243 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 244 245 w.WriteHeader(http.StatusNoContent) 246 }) 247 } 248 249 // HandleL7PolicyUpdateSuccessfully sets up the test server to respond to a l7policy Update request. 250 func HandleL7PolicyUpdateSuccessfully(t *testing.T) { 251 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) { 252 th.TestMethod(t, r, "PUT") 253 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 254 th.TestHeader(t, r, "Accept", "application/json") 255 th.TestHeader(t, r, "Content-Type", "application/json") 256 th.TestJSONRequest(t, r, `{ 257 "l7policy": { 258 "name": "NewL7PolicyName", 259 "action": "REDIRECT_TO_URL", 260 "redirect_url": "http://www.new-example.com" 261 } 262 }`) 263 264 fmt.Fprintf(w, PostUpdateL7PolicyBody) 265 }) 266 } 267 268 // HandleL7PolicyUpdateNullRedirectURLSuccessfully sets up the test server to respond to a l7policy Update request. 269 func HandleL7PolicyUpdateNullRedirectURLSuccessfully(t *testing.T) { 270 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) { 271 th.TestMethod(t, r, "PUT") 272 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 273 th.TestHeader(t, r, "Accept", "application/json") 274 th.TestHeader(t, r, "Content-Type", "application/json") 275 th.TestJSONRequest(t, r, `{ 276 "l7policy": { 277 "name": "NewL7PolicyName", 278 "redirect_url": null 279 } 280 }`) 281 282 fmt.Fprintf(w, PostUpdateL7PolicyNullRedirectURLBody) 283 }) 284 } 285 286 // SingleRuleBody is the canned body of a Get request on an existing rule. 287 const SingleRuleBody = ` 288 { 289 "rule": { 290 "compare_type": "REGEX", 291 "invert": true, 292 "admin_state_up": true, 293 "value": "/images*", 294 "key": null, 295 "project_id": "e3cd678b11784734bc366148aa37580e", 296 "type": "PATH", 297 "id": "16621dbb-a736-4888-a57a-3ecd53df784c" 298 } 299 } 300 ` 301 302 // HandleRuleCreationSuccessfully sets up the test server to respond to a rule creation request 303 // with a given response. 304 func HandleRuleCreationSuccessfully(t *testing.T, response string) { 305 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) { 306 th.TestMethod(t, r, "POST") 307 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 308 th.TestJSONRequest(t, r, `{ 309 "rule": { 310 "compare_type": "REGEX", 311 "type": "PATH", 312 "value": "/images*" 313 } 314 }`) 315 316 w.WriteHeader(http.StatusAccepted) 317 w.Header().Add("Content-Type", "application/json") 318 fmt.Fprintf(w, response) 319 }) 320 } 321 322 // RulesListBody contains the canned body of a rule list response. 323 const RulesListBody = ` 324 { 325 "rules":[ 326 { 327 "compare_type": "REGEX", 328 "invert": true, 329 "admin_state_up": true, 330 "value": "/images*", 331 "key": null, 332 "project_id": "e3cd678b11784734bc366148aa37580e", 333 "type": "PATH", 334 "id": "16621dbb-a736-4888-a57a-3ecd53df784c" 335 }, 336 { 337 "compare_type": "EQUAL_TO", 338 "invert": false, 339 "admin_state_up": true, 340 "value": "www.example.com", 341 "key": null, 342 "project_id": "e3cd678b11784734bc366148aa37580e", 343 "type": "HOST_NAME", 344 "id": "d24521a0-df84-4468-861a-a531af116d1e" 345 } 346 ] 347 } 348 ` 349 350 // HandleRuleListSuccessfully sets up the test server to respond to a rule List request. 351 func HandleRuleListSuccessfully(t *testing.T) { 352 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) { 353 th.TestMethod(t, r, "GET") 354 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 355 356 w.Header().Add("Content-Type", "application/json") 357 r.ParseForm() 358 marker := r.Form.Get("marker") 359 switch marker { 360 case "": 361 fmt.Fprintf(w, RulesListBody) 362 case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab": 363 fmt.Fprintf(w, `{ "rules": [] }`) 364 default: 365 t.Fatalf("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules invoked with unexpected marker=[%s]", marker) 366 } 367 }) 368 } 369 370 // HandleRuleGetSuccessfully sets up the test server to respond to a rule Get request. 371 func HandleRuleGetSuccessfully(t *testing.T) { 372 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) { 373 th.TestMethod(t, r, "GET") 374 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 375 th.TestHeader(t, r, "Accept", "application/json") 376 377 fmt.Fprintf(w, SingleRuleBody) 378 }) 379 } 380 381 // HandleRuleDeletionSuccessfully sets up the test server to respond to a rule deletion request. 382 func HandleRuleDeletionSuccessfully(t *testing.T) { 383 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) { 384 th.TestMethod(t, r, "DELETE") 385 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 386 387 w.WriteHeader(http.StatusNoContent) 388 }) 389 } 390 391 // PostUpdateRuleBody is the canned response body of a Update request on an existing rule. 392 const PostUpdateRuleBody = ` 393 { 394 "rule": { 395 "compare_type": "REGEX", 396 "invert": false, 397 "admin_state_up": true, 398 "value": "/images/special*", 399 "key": null, 400 "project_id": "e3cd678b11784734bc366148aa37580e", 401 "type": "PATH", 402 "id": "16621dbb-a736-4888-a57a-3ecd53df784c" 403 } 404 } 405 ` 406 407 // HandleRuleUpdateSuccessfully sets up the test server to respond to a rule Update request. 408 func HandleRuleUpdateSuccessfully(t *testing.T) { 409 th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) { 410 th.TestMethod(t, r, "PUT") 411 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 412 th.TestHeader(t, r, "Accept", "application/json") 413 th.TestHeader(t, r, "Content-Type", "application/json") 414 th.TestJSONRequest(t, r, `{ 415 "rule": { 416 "compare_type": "REGEX", 417 "invert": false, 418 "key": null, 419 "type": "PATH", 420 "value": "/images/special*" 421 } 422 }`) 423 424 fmt.Fprintf(w, PostUpdateRuleBody) 425 }) 426 }