github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/elb/v3/policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/structs" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/elb/v3/policies" 10 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 11 "github.com/opentelekomcloud/gophertelekomcloud/testhelper/client" 12 ) 13 14 func expectedResult() *policies.Policy { 15 return &policies.Policy{ 16 ID: "cf4360fd-8631-41ff-a6f5-b72c35da74be", 17 Action: "REDIRECT_TO_LISTENER", 18 ListenerID: "e2220d2a-3faf-44f3-8cd6-0c42952bd0ab", 19 Position: 100, 20 ProjectID: "99a3fff0d03c428eac3678da6a7d0f24", 21 Status: "ACTIVE", 22 RedirectListenerID: "48a97732-449e-4aab-b561-828d29e45050", 23 Rules: []structs.ResourceRef{}, 24 } 25 } 26 27 func TestCreateRequest(t *testing.T) { 28 th.SetupHTTP() 29 defer th.TeardownHTTP() 30 31 th.Mux.HandleFunc("/l7policies", func(w http.ResponseWriter, r *http.Request) { 32 th.TestMethod(t, r, "POST") 33 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 34 th.TestJSONRequest(t, r, createRequestBody) 35 36 w.Header().Add("Content-Type", "application/json") 37 w.WriteHeader(http.StatusCreated) 38 _, _ = fmt.Fprint(w, createResponseBody) 39 }) 40 41 opts := policies.CreateOpts{ 42 Action: "REDIRECT_TO_LISTENER", 43 ListenerID: "e2220d2a-3faf-44f3-8cd6-0c42952bd0ab", 44 RedirectListenerID: "48a97732-449e-4aab-b561-828d29e45050", 45 } 46 created, err := policies.Create(client.ServiceClient(), opts).Extract() 47 th.AssertNoErr(t, err) 48 49 th.AssertDeepEquals(t, expectedResult(), created) 50 } 51 52 func TestGetRequest(t *testing.T) { 53 th.SetupHTTP() 54 defer th.TeardownHTTP() 55 56 expected := expectedResult() 57 th.Mux.HandleFunc(fmt.Sprintf("/l7policies/%s", expected.ID), func(w http.ResponseWriter, r *http.Request) { 58 th.TestMethod(t, r, "GET") 59 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 60 61 w.Header().Add("Content-Type", "application/json") 62 w.WriteHeader(http.StatusOK) 63 _, _ = fmt.Fprint(w, createResponseBody) 64 }) 65 66 policy, err := policies.Get(client.ServiceClient(), expected.ID).Extract() 67 th.AssertNoErr(t, err) 68 69 th.AssertDeepEquals(t, expected, policy) 70 } 71 72 func TestListRequest(t *testing.T) { 73 th.SetupHTTP() 74 defer th.TeardownHTTP() 75 76 th.Mux.HandleFunc("/l7policies", func(w http.ResponseWriter, r *http.Request) { 77 th.TestMethod(t, r, "GET") 78 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 79 80 w.Header().Add("Content-Type", "application/json") 81 w.WriteHeader(http.StatusOK) 82 _, _ = fmt.Fprint(w, listResponseBody) 83 }) 84 85 pages, err := policies.List(client.ServiceClient(), nil).AllPages() 86 th.AssertNoErr(t, err) 87 88 policySlice, err := policies.ExtractPolicies(pages) 89 th.AssertNoErr(t, err) 90 th.AssertEquals(t, 2, len(policySlice)) 91 } 92 93 func TestDeleteRequest(t *testing.T) { 94 th.SetupHTTP() 95 defer th.TeardownHTTP() 96 97 id := expectedResult().ID 98 th.Mux.HandleFunc(fmt.Sprintf("/l7policies/%s", id), func(w http.ResponseWriter, r *http.Request) { 99 th.TestMethod(t, r, "DELETE") 100 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 101 102 w.Header().Add("Content-Type", "application/json") 103 w.WriteHeader(http.StatusNoContent) 104 }) 105 106 th.AssertNoErr(t, policies.Delete(client.ServiceClient(), id).ExtractErr()) 107 }