github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/endpoints/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/endpoints" 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("/endpoints", func(w http.ResponseWriter, r *http.Request) { 21 th.TestMethod(t, r, "POST") 22 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 23 th.TestJSONRequest(t, r, ` 24 { 25 "endpoint": { 26 "interface": "public", 27 "name": "the-endiest-of-points", 28 "region": "underground", 29 "url": "https://1.2.3.4:9000/", 30 "service_id": "asdfasdfasdfasdf" 31 } 32 } 33 `) 34 35 w.WriteHeader(http.StatusCreated) 36 fmt.Fprint(w, ` 37 { 38 "endpoint": { 39 "id": "12", 40 "interface": "public", 41 "enabled": true, 42 "links": { 43 "self": "https://localhost:5000/v3/endpoints/12" 44 }, 45 "name": "the-endiest-of-points", 46 "region": "underground", 47 "service_id": "asdfasdfasdfasdf", 48 "url": "https://1.2.3.4:9000/" 49 } 50 } 51 `) 52 }) 53 54 actual, err := endpoints.Create(context.TODO(), client.ServiceClient(), endpoints.CreateOpts{ 55 Availability: gophercloud.AvailabilityPublic, 56 Name: "the-endiest-of-points", 57 Region: "underground", 58 URL: "https://1.2.3.4:9000/", 59 ServiceID: "asdfasdfasdfasdf", 60 }).Extract() 61 th.AssertNoErr(t, err) 62 63 expected := &endpoints.Endpoint{ 64 ID: "12", 65 Availability: gophercloud.AvailabilityPublic, 66 Enabled: true, 67 Name: "the-endiest-of-points", 68 Region: "underground", 69 ServiceID: "asdfasdfasdfasdf", 70 URL: "https://1.2.3.4:9000/", 71 } 72 73 th.AssertDeepEquals(t, expected, actual) 74 } 75 76 func TestListEndpoints(t *testing.T) { 77 th.SetupHTTP() 78 defer th.TeardownHTTP() 79 80 th.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { 81 th.TestMethod(t, r, "GET") 82 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 83 84 w.Header().Add("Content-Type", "application/json") 85 fmt.Fprint(w, ` 86 { 87 "endpoints": [ 88 { 89 "id": "12", 90 "interface": "public", 91 "enabled": true, 92 "links": { 93 "self": "https://localhost:5000/v3/endpoints/12" 94 }, 95 "name": "the-endiest-of-points", 96 "region": "underground", 97 "service_id": "asdfasdfasdfasdf", 98 "url": "https://1.2.3.4:9000/" 99 }, 100 { 101 "id": "13", 102 "interface": "internal", 103 "enabled": false, 104 "links": { 105 "self": "https://localhost:5000/v3/endpoints/13" 106 }, 107 "name": "shhhh", 108 "region": "underground", 109 "service_id": "asdfasdfasdfasdf", 110 "url": "https://1.2.3.4:9001/" 111 } 112 ], 113 "links": { 114 "next": null, 115 "previous": null 116 } 117 } 118 `) 119 }) 120 121 count := 0 122 err := endpoints.List(client.ServiceClient(), endpoints.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 123 count++ 124 actual, err := endpoints.ExtractEndpoints(page) 125 if err != nil { 126 t.Errorf("Failed to extract endpoints: %v", err) 127 return false, err 128 } 129 130 expected := []endpoints.Endpoint{ 131 { 132 ID: "12", 133 Availability: gophercloud.AvailabilityPublic, 134 Enabled: true, 135 Name: "the-endiest-of-points", 136 Region: "underground", 137 ServiceID: "asdfasdfasdfasdf", 138 URL: "https://1.2.3.4:9000/", 139 }, 140 { 141 ID: "13", 142 Availability: gophercloud.AvailabilityInternal, 143 Enabled: false, 144 Name: "shhhh", 145 Region: "underground", 146 ServiceID: "asdfasdfasdfasdf", 147 URL: "https://1.2.3.4:9001/", 148 }, 149 } 150 th.AssertDeepEquals(t, expected, actual) 151 return true, nil 152 }) 153 th.AssertNoErr(t, err) 154 th.AssertEquals(t, 1, count) 155 } 156 157 func TestUpdateEndpoint(t *testing.T) { 158 th.SetupHTTP() 159 defer th.TeardownHTTP() 160 161 th.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) { 162 th.TestMethod(t, r, "PATCH") 163 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 164 th.TestJSONRequest(t, r, ` 165 { 166 "endpoint": { 167 "name": "renamed", 168 "region": "somewhere-else" 169 } 170 } 171 `) 172 173 fmt.Fprint(w, ` 174 { 175 "endpoint": { 176 "id": "12", 177 "interface": "public", 178 "enabled": true, 179 "links": { 180 "self": "https://localhost:5000/v3/endpoints/12" 181 }, 182 "name": "renamed", 183 "region": "somewhere-else", 184 "service_id": "asdfasdfasdfasdf", 185 "url": "https://1.2.3.4:9000/" 186 } 187 } 188 `) 189 }) 190 191 actual, err := endpoints.Update(context.TODO(), client.ServiceClient(), "12", endpoints.UpdateOpts{ 192 Name: "renamed", 193 Region: "somewhere-else", 194 }).Extract() 195 if err != nil { 196 t.Fatalf("Unexpected error from Update: %v", err) 197 } 198 199 expected := &endpoints.Endpoint{ 200 ID: "12", 201 Availability: gophercloud.AvailabilityPublic, 202 Enabled: true, 203 Name: "renamed", 204 Region: "somewhere-else", 205 ServiceID: "asdfasdfasdfasdf", 206 URL: "https://1.2.3.4:9000/", 207 } 208 th.AssertDeepEquals(t, expected, actual) 209 } 210 211 func TestDeleteEndpoint(t *testing.T) { 212 th.SetupHTTP() 213 defer th.TeardownHTTP() 214 215 th.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) { 216 th.TestMethod(t, r, "DELETE") 217 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 218 219 w.WriteHeader(http.StatusNoContent) 220 }) 221 222 res := endpoints.Delete(context.TODO(), client.ServiceClient(), "34") 223 th.AssertNoErr(t, res.Err) 224 }