| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints | 
|  | 2 |  | 
|  | 3 | import ( | 
|  | 4 | "fmt" | 
|  | 5 | "net/http" | 
|  | 6 | "reflect" | 
|  | 7 | "testing" | 
|  | 8 |  | 
|  | 9 | "github.com/rackspace/gophercloud" | 
|  | 10 | "github.com/rackspace/gophercloud/testhelper" | 
|  | 11 | ) | 
|  | 12 |  | 
|  | 13 | const tokenID = "abcabcabcabc" | 
|  | 14 |  | 
|  | 15 | func serviceClient() *gophercloud.ServiceClient { | 
|  | 16 | return &gophercloud.ServiceClient{ | 
|  | 17 | Provider: &gophercloud.ProviderClient{TokenID: tokenID}, | 
|  | 18 | Endpoint: testhelper.Endpoint(), | 
|  | 19 | } | 
|  | 20 | } | 
|  | 21 |  | 
|  | 22 | func TestCreateSuccessful(t *testing.T) { | 
|  | 23 | testhelper.SetupHTTP() | 
|  | 24 | defer testhelper.TeardownHTTP() | 
|  | 25 |  | 
|  | 26 | testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { | 
|  | 27 | testhelper.TestMethod(t, r, "POST") | 
|  | 28 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
|  | 29 | testhelper.TestJSONRequest(t, r, ` | 
|  | 30 | { | 
|  | 31 | "endpoint": { | 
|  | 32 | "interface": "public", | 
|  | 33 | "name": "the-endiest-of-points", | 
|  | 34 | "region": "underground", | 
|  | 35 | "url": "https://1.2.3.4:9000/", | 
|  | 36 | "service_id": "asdfasdfasdfasdf" | 
|  | 37 | } | 
|  | 38 | } | 
|  | 39 | `) | 
|  | 40 |  | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 41 | w.WriteHeader(http.StatusCreated) | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 42 | fmt.Fprintf(w, ` | 
|  | 43 | { | 
|  | 44 | "endpoint": { | 
|  | 45 | "id": "12", | 
|  | 46 | "interface": "public", | 
|  | 47 | "links": { | 
|  | 48 | "self": "https://localhost:5000/v3/endpoints/12" | 
|  | 49 | }, | 
|  | 50 | "name": "the-endiest-of-points", | 
|  | 51 | "region": "underground", | 
|  | 52 | "service_id": "asdfasdfasdfasdf", | 
|  | 53 | "url": "https://1.2.3.4:9000/" | 
|  | 54 | } | 
|  | 55 | } | 
|  | 56 | `) | 
|  | 57 | }) | 
|  | 58 |  | 
|  | 59 | client := serviceClient() | 
|  | 60 |  | 
|  | 61 | result, err := Create(client, EndpointOpts{ | 
|  | 62 | Interface: InterfacePublic, | 
|  | 63 | Name:      "the-endiest-of-points", | 
|  | 64 | Region:    "underground", | 
|  | 65 | URL:       "https://1.2.3.4:9000/", | 
|  | 66 | ServiceID: "asdfasdfasdfasdf", | 
|  | 67 | }) | 
|  | 68 | if err != nil { | 
|  | 69 | t.Fatalf("Unable to create an endpoint: %v", err) | 
|  | 70 | } | 
|  | 71 |  | 
| Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 72 | expected := &Endpoint{ | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 73 | ID:        "12", | 
|  | 74 | Interface: InterfacePublic, | 
|  | 75 | Name:      "the-endiest-of-points", | 
|  | 76 | Region:    "underground", | 
|  | 77 | ServiceID: "asdfasdfasdfasdf", | 
|  | 78 | URL:       "https://1.2.3.4:9000/", | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | if !reflect.DeepEqual(result, expected) { | 
|  | 82 | t.Errorf("Expected %#v, was %#v", expected, result) | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | func TestListEndpoints(t *testing.T) { | 
|  | 87 | testhelper.SetupHTTP() | 
|  | 88 | defer testhelper.TeardownHTTP() | 
|  | 89 |  | 
|  | 90 | testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { | 
|  | 91 | testhelper.TestMethod(t, r, "GET") | 
|  | 92 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
|  | 93 |  | 
|  | 94 | fmt.Fprintf(w, ` | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 95 | { | 
|  | 96 | "endpoints": [ | 
|  | 97 | { | 
|  | 98 | "id": "12", | 
|  | 99 | "interface": "public", | 
|  | 100 | "links": { | 
|  | 101 | "self": "https://localhost:5000/v3/endpoints/12" | 
|  | 102 | }, | 
|  | 103 | "name": "the-endiest-of-points", | 
|  | 104 | "region": "underground", | 
|  | 105 | "service_id": "asdfasdfasdfasdf", | 
|  | 106 | "url": "https://1.2.3.4:9000/" | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 107 | }, | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 108 | { | 
|  | 109 | "id": "13", | 
|  | 110 | "interface": "internal", | 
|  | 111 | "links": { | 
|  | 112 | "self": "https://localhost:5000/v3/endpoints/13" | 
|  | 113 | }, | 
|  | 114 | "name": "shhhh", | 
|  | 115 | "region": "underground", | 
|  | 116 | "service_id": "asdfasdfasdfasdf", | 
|  | 117 | "url": "https://1.2.3.4:9001/" | 
|  | 118 | } | 
|  | 119 | ], | 
|  | 120 | "links": { | 
|  | 121 | "next": null, | 
|  | 122 | "previous": null | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 123 | } | 
| Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 124 | } | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 125 | `) | 
|  | 126 | }) | 
|  | 127 |  | 
|  | 128 | client := serviceClient() | 
|  | 129 |  | 
|  | 130 | actual, err := List(client, ListOpts{}) | 
|  | 131 | if err != nil { | 
|  | 132 | t.Fatalf("Unexpected error listing endpoints: %v", err) | 
|  | 133 | } | 
|  | 134 |  | 
| Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 135 | expected := &EndpointList{ | 
|  | 136 | Endpoints: []Endpoint{ | 
|  | 137 | Endpoint{ | 
|  | 138 | ID:        "12", | 
|  | 139 | Interface: InterfacePublic, | 
|  | 140 | Name:      "the-endiest-of-points", | 
|  | 141 | Region:    "underground", | 
|  | 142 | ServiceID: "asdfasdfasdfasdf", | 
|  | 143 | URL:       "https://1.2.3.4:9000/", | 
|  | 144 | }, | 
|  | 145 | Endpoint{ | 
|  | 146 | ID:        "13", | 
|  | 147 | Interface: InterfaceInternal, | 
|  | 148 | Name:      "shhhh", | 
|  | 149 | Region:    "underground", | 
|  | 150 | ServiceID: "asdfasdfasdfasdf", | 
|  | 151 | URL:       "https://1.2.3.4:9001/", | 
|  | 152 | }, | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 153 | }, | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | if !reflect.DeepEqual(expected, actual) { | 
|  | 157 | t.Errorf("Expected %#v, got %#v", expected, actual) | 
|  | 158 | } | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | func TestUpdateEndpoint(t *testing.T) { | 
|  | 162 | testhelper.SetupHTTP() | 
|  | 163 | defer testhelper.TeardownHTTP() | 
|  | 164 |  | 
|  | 165 | testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 166 | testhelper.TestMethod(t, r, "PATCH") | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 167 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
|  | 168 | testhelper.TestJSONRequest(t, r, ` | 
|  | 169 | { | 
|  | 170 | "endpoint": { | 
|  | 171 | "name": "renamed", | 
|  | 172 | "region": "somewhere-else" | 
|  | 173 | } | 
|  | 174 | } | 
|  | 175 | `) | 
|  | 176 |  | 
|  | 177 | fmt.Fprintf(w, ` | 
|  | 178 | { | 
|  | 179 | "endpoint": { | 
|  | 180 | "id": "12", | 
|  | 181 | "interface": "public", | 
|  | 182 | "links": { | 
|  | 183 | "self": "https://localhost:5000/v3/endpoints/12" | 
|  | 184 | }, | 
|  | 185 | "name": "renamed", | 
|  | 186 | "region": "somewhere-else", | 
|  | 187 | "service_id": "asdfasdfasdfasdf", | 
|  | 188 | "url": "https://1.2.3.4:9000/" | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 | `) | 
|  | 192 | }) | 
|  | 193 |  | 
|  | 194 | client := serviceClient() | 
|  | 195 | actual, err := Update(client, "12", EndpointOpts{ | 
|  | 196 | Name:   "renamed", | 
|  | 197 | Region: "somewhere-else", | 
|  | 198 | }) | 
|  | 199 | if err != nil { | 
|  | 200 | t.Fatalf("Unexpected error from Update: %v", err) | 
|  | 201 | } | 
|  | 202 |  | 
| Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 203 | expected := &Endpoint{ | 
| Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 204 | ID:        "12", | 
|  | 205 | Interface: InterfacePublic, | 
|  | 206 | Name:      "renamed", | 
|  | 207 | Region:    "somewhere-else", | 
|  | 208 | ServiceID: "asdfasdfasdfasdf", | 
|  | 209 | URL:       "https://1.2.3.4:9000/", | 
|  | 210 | } | 
|  | 211 | if !reflect.DeepEqual(expected, actual) { | 
|  | 212 | t.Errorf("Expected %#v, was %#v", expected, actual) | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | func TestDeleteEndpoint(t *testing.T) { | 
|  | 217 | testhelper.SetupHTTP() | 
|  | 218 | defer testhelper.TeardownHTTP() | 
|  | 219 |  | 
|  | 220 | testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) { | 
|  | 221 | testhelper.TestMethod(t, r, "DELETE") | 
|  | 222 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) | 
|  | 223 |  | 
|  | 224 | w.WriteHeader(http.StatusNoContent) | 
|  | 225 | }) | 
|  | 226 |  | 
|  | 227 | client := serviceClient() | 
|  | 228 |  | 
|  | 229 | err := Delete(client, "34") | 
|  | 230 | if err != nil { | 
|  | 231 | t.Fatalf("Unexpected error from Delete: %v", err) | 
|  | 232 | } | 
|  | 233 | } |