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