github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/projectendpoints/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/vnpaycloud-console/gophercloud/v2" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/projectendpoints" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 14 ) 15 16 func TestCreateSuccessful(t *testing.T) { 17 th.SetupHTTP() 18 defer th.TeardownHTTP() 19 20 th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints/endpoint-id", func(w http.ResponseWriter, r *http.Request) { 21 th.TestMethod(t, r, "PUT") 22 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 23 24 w.WriteHeader(http.StatusNoContent) 25 }) 26 27 err := projectendpoints.Create(context.TODO(), client.ServiceClient(), "project-id", "endpoint-id").Err 28 th.AssertNoErr(t, err) 29 } 30 31 func TestListEndpoints(t *testing.T) { 32 th.SetupHTTP() 33 defer th.TeardownHTTP() 34 35 th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints", func(w http.ResponseWriter, r *http.Request) { 36 th.TestMethod(t, r, "GET") 37 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 38 39 w.Header().Add("Content-Type", "application/json") 40 fmt.Fprint(w, ` 41 { 42 "endpoints": [ 43 { 44 "id": "6fedc0", 45 "interface": "public", 46 "url": "http://example.com/identity/", 47 "region": "north", 48 "links": { 49 "self": "http://example.com/identity/v3/endpoints/6fedc0" 50 }, 51 "service_id": "1b501a" 52 }, 53 { 54 "id": "6fedc0", 55 "interface": "internal", 56 "region": "south", 57 "url": "http://example.com/identity/", 58 "links": { 59 "self": "http://example.com/identity/v3/endpoints/6fedc0" 60 }, 61 "service_id": "1b501a" 62 } 63 ], 64 "links": { 65 "self": "http://example.com/identity/v3/OS-EP-FILTER/projects/263fd9/endpoints", 66 "previous": null, 67 "next": null 68 } 69 } 70 `) 71 }) 72 73 count := 0 74 err := projectendpoints.List(client.ServiceClient(), "project-id").EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 75 count++ 76 actual, err := projectendpoints.ExtractEndpoints(page) 77 if err != nil { 78 t.Errorf("Failed to extract endpoints: %v", err) 79 return false, err 80 } 81 82 expected := []projectendpoints.Endpoint{ 83 { 84 ID: "6fedc0", 85 Availability: gophercloud.AvailabilityPublic, 86 Region: "north", 87 ServiceID: "1b501a", 88 URL: "http://example.com/identity/", 89 }, 90 { 91 ID: "6fedc0", 92 Availability: gophercloud.AvailabilityInternal, 93 Region: "south", 94 ServiceID: "1b501a", 95 URL: "http://example.com/identity/", 96 }, 97 } 98 th.AssertDeepEquals(t, expected, actual) 99 return true, nil 100 }) 101 th.AssertNoErr(t, err) 102 th.AssertEquals(t, 1, count) 103 } 104 105 func TestDeleteEndpoint(t *testing.T) { 106 th.SetupHTTP() 107 defer th.TeardownHTTP() 108 109 th.Mux.HandleFunc("/OS-EP-FILTER/projects/project-id/endpoints/endpoint-id", func(w http.ResponseWriter, r *http.Request) { 110 th.TestMethod(t, r, "DELETE") 111 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 112 113 w.WriteHeader(http.StatusNoContent) 114 }) 115 116 res := projectendpoints.Delete(context.TODO(), client.ServiceClient(), "project-id", "endpoint-id") 117 th.AssertNoErr(t, res.Err) 118 }