github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/vpcep/v1/endpoints/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/tags" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/vpcep/v1/endpoints" 10 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 11 "github.com/opentelekomcloud/gophertelekomcloud/testhelper/client" 12 ) 13 14 func TestCreateRequest(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/vpc-endpoints", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "POST") 20 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 21 th.TestJSONRequest(t, r, createRequest) 22 23 w.Header().Add("Content-Type", "application/json") 24 w.WriteHeader(http.StatusCreated) 25 _, _ = fmt.Fprint(w, createResponse) 26 }) 27 28 opts := endpoints.CreateOpts{ 29 NetworkID: "68bfbcc1-dff2-47e4-a9d4-332b9bc1b8de", 30 ServiceID: expected.ServiceID, 31 RouterID: expected.RouterID, 32 EnableDNS: true, 33 Tags: []tags.ResourceTag{{Key: "test1", Value: "test1"}}, 34 } 35 36 ep, err := endpoints.Create(client.ServiceClient(), opts).Extract() 37 th.AssertNoErr(t, err) 38 th.AssertDeepEquals(t, expected, ep) 39 } 40 41 func TestGetRequest(t *testing.T) { 42 th.SetupHTTP() 43 defer th.TeardownHTTP() 44 45 id := "4189d3c2-8882-4871-a3c2-d380272eed83" 46 th.Mux.HandleFunc(fmt.Sprintf("/vpc-endpoints/%s", id), func(w http.ResponseWriter, r *http.Request) { 47 th.TestMethod(t, r, "GET") 48 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 49 50 w.Header().Add("Content-Type", "application/json") 51 w.WriteHeader(http.StatusOK) 52 _, _ = fmt.Fprint(w, createResponse) 53 }) 54 55 ep, err := endpoints.Get(client.ServiceClient(), id).Extract() 56 th.AssertNoErr(t, err) 57 th.AssertDeepEquals(t, expected, ep) 58 } 59 60 func TestListRequest(t *testing.T) { 61 th.SetupHTTP() 62 defer th.TeardownHTTP() 63 64 th.Mux.HandleFunc("/vpc-endpoints", func(w http.ResponseWriter, r *http.Request) { 65 th.TestMethod(t, r, "GET") 66 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 67 68 w.Header().Add("Content-Type", "application/json") 69 w.WriteHeader(http.StatusOK) 70 _, _ = fmt.Fprint(w, listResponse) 71 }) 72 73 opts := endpoints.ListOpts{ 74 RouterID: "84758cf5-9c62-43ae-a778-3dbd8370c0a4", 75 } 76 pages, err := endpoints.List(client.ServiceClient(), opts).AllPages() 77 th.AssertNoErr(t, err) 78 79 eps, err := endpoints.ExtractEndpoints(pages) 80 th.AssertNoErr(t, err) 81 82 th.AssertEquals(t, 2, len(eps)) 83 } 84 85 func TestDeleteRequest(t *testing.T) { 86 th.SetupHTTP() 87 defer th.TeardownHTTP() 88 89 id := "4189d3c2-8882-4871-a3c2-d380272eed83" 90 th.Mux.HandleFunc(fmt.Sprintf("/vpc-endpoints/%s", id), func(w http.ResponseWriter, r *http.Request) { 91 th.TestMethod(t, r, "DELETE") 92 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 93 94 w.Header().Add("Content-Type", "application/json") 95 w.WriteHeader(http.StatusNoContent) 96 }) 97 98 th.AssertNoErr(t, endpoints.Delete(client.ServiceClient(), id).ExtractErr()) 99 }