1package testing 2 3import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/identity/v3/services" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11) 12 13// ListOutput provides a single page of Service results. 14const ListOutput = ` 15{ 16 "links": { 17 "next": null, 18 "previous": null 19 }, 20 "services": [ 21 { 22 "id": "1234", 23 "links": { 24 "self": "https://example.com/identity/v3/services/1234" 25 }, 26 "type": "identity", 27 "enabled": false, 28 "extra": { 29 "name": "service-one", 30 "description": "Service One" 31 } 32 }, 33 { 34 "id": "9876", 35 "links": { 36 "self": "https://example.com/identity/v3/services/9876" 37 }, 38 "type": "compute", 39 "enabled": false, 40 "extra": { 41 "name": "service-two", 42 "description": "Service Two", 43 "email": "service@example.com" 44 } 45 } 46 ] 47} 48` 49 50// GetOutput provides a Get result. 51const GetOutput = ` 52{ 53 "service": { 54 "id": "9876", 55 "links": { 56 "self": "https://example.com/identity/v3/services/9876" 57 }, 58 "type": "compute", 59 "enabled": false, 60 "extra": { 61 "name": "service-two", 62 "description": "Service Two", 63 "email": "service@example.com" 64 } 65 } 66} 67` 68 69// CreateRequest provides the input to a Create request. 70const CreateRequest = ` 71{ 72 "service": { 73 "description": "Service Two", 74 "email": "service@example.com", 75 "name": "service-two", 76 "type": "compute" 77 } 78} 79` 80 81// UpdateRequest provides the input to as Update request. 82const UpdateRequest = ` 83{ 84 "service": { 85 "type": "compute2", 86 "description": "Service Two Updated" 87 } 88} 89` 90 91// UpdateOutput provides an update result. 92const UpdateOutput = ` 93{ 94 "service": { 95 "id": "9876", 96 "links": { 97 "self": "https://example.com/identity/v3/services/9876" 98 }, 99 "type": "compute2", 100 "enabled": false, 101 "extra": { 102 "name": "service-two", 103 "description": "Service Two Updated", 104 "email": "service@example.com" 105 } 106 } 107} 108` 109 110// FirstService is the first service in the List request. 111var FirstService = services.Service{ 112 ID: "1234", 113 Links: map[string]interface{}{ 114 "self": "https://example.com/identity/v3/services/1234", 115 }, 116 Type: "identity", 117 Enabled: false, 118 Extra: map[string]interface{}{ 119 "name": "service-one", 120 "description": "Service One", 121 }, 122} 123 124// SecondService is the second service in the List request. 125var SecondService = services.Service{ 126 ID: "9876", 127 Links: map[string]interface{}{ 128 "self": "https://example.com/identity/v3/services/9876", 129 }, 130 Type: "compute", 131 Enabled: false, 132 Extra: map[string]interface{}{ 133 "name": "service-two", 134 "description": "Service Two", 135 "email": "service@example.com", 136 }, 137} 138 139// SecondServiceUpdated is the SecondService should look after an Update. 140var SecondServiceUpdated = services.Service{ 141 ID: "9876", 142 Links: map[string]interface{}{ 143 "self": "https://example.com/identity/v3/services/9876", 144 }, 145 Type: "compute2", 146 Enabled: false, 147 Extra: map[string]interface{}{ 148 "name": "service-two", 149 "description": "Service Two Updated", 150 "email": "service@example.com", 151 }, 152} 153 154// ExpectedServicesSlice is the slice of services to be returned from ListOutput. 155var ExpectedServicesSlice = []services.Service{FirstService, SecondService} 156 157// HandleListServicesSuccessfully creates an HTTP handler at `/services` on the 158// test handler mux that responds with a list of two services. 159func HandleListServicesSuccessfully(t *testing.T) { 160 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { 161 th.TestMethod(t, r, "GET") 162 th.TestHeader(t, r, "Accept", "application/json") 163 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 164 165 w.Header().Set("Content-Type", "application/json") 166 w.WriteHeader(http.StatusOK) 167 fmt.Fprintf(w, ListOutput) 168 }) 169} 170 171// HandleGetServiceSuccessfully creates an HTTP handler at `/services` on the 172// test handler mux that responds with a single service. 173func HandleGetServiceSuccessfully(t *testing.T) { 174 th.Mux.HandleFunc("/services/9876", func(w http.ResponseWriter, r *http.Request) { 175 th.TestMethod(t, r, "GET") 176 th.TestHeader(t, r, "Accept", "application/json") 177 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 178 179 w.Header().Set("Content-Type", "application/json") 180 w.WriteHeader(http.StatusOK) 181 fmt.Fprintf(w, GetOutput) 182 }) 183} 184 185// HandleCreateServiceSuccessfully creates an HTTP handler at `/services` on the 186// test handler mux that tests service creation. 187func HandleCreateServiceSuccessfully(t *testing.T) { 188 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { 189 th.TestMethod(t, r, "POST") 190 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 191 th.TestJSONRequest(t, r, CreateRequest) 192 193 w.WriteHeader(http.StatusCreated) 194 fmt.Fprintf(w, GetOutput) 195 }) 196} 197 198// HandleUpdateServiceSuccessfully creates an HTTP handler at `/services` on the 199// test handler mux that tests service update. 200func HandleUpdateServiceSuccessfully(t *testing.T) { 201 th.Mux.HandleFunc("/services/9876", func(w http.ResponseWriter, r *http.Request) { 202 th.TestMethod(t, r, "PATCH") 203 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 204 th.TestJSONRequest(t, r, UpdateRequest) 205 206 w.WriteHeader(http.StatusOK) 207 fmt.Fprintf(w, UpdateOutput) 208 }) 209} 210