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