github.com/gophercloud/gophercloud@v1.11.0/openstack/objectstorage/v1/containers/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 fake "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 type handlerOptions struct { 14 path string 15 } 16 17 type option func(*handlerOptions) 18 19 func WithPath(s string) option { 20 return func(h *handlerOptions) { 21 h.path = s 22 } 23 } 24 25 // ExpectedListInfo is the result expected from a call to `List` when full 26 // info is requested. 27 var ExpectedListInfo = []containers.Container{ 28 { 29 Count: 0, 30 Bytes: 0, 31 Name: "janeausten", 32 }, 33 { 34 Count: 1, 35 Bytes: 14, 36 Name: "marktwain", 37 }, 38 } 39 40 // ExpectedListNames is the result expected from a call to `List` when just 41 // container names are requested. 42 var ExpectedListNames = []string{"janeausten", "marktwain"} 43 44 // HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that 45 // responds with a `List` response when full info is requested. 46 func HandleListContainerInfoSuccessfully(t *testing.T) { 47 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 48 th.TestMethod(t, r, "GET") 49 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 50 th.TestHeader(t, r, "Accept", "application/json") 51 52 w.Header().Set("Content-Type", "application/json") 53 r.ParseForm() 54 marker := r.Form.Get("marker") 55 switch marker { 56 case "": 57 fmt.Fprintf(w, `[ 58 { 59 "count": 0, 60 "bytes": 0, 61 "name": "janeausten" 62 }, 63 { 64 "count": 1, 65 "bytes": 14, 66 "name": "marktwain" 67 } 68 ]`) 69 case "janeausten": 70 fmt.Fprintf(w, `[ 71 { 72 "count": 1, 73 "bytes": 14, 74 "name": "marktwain" 75 } 76 ]`) 77 case "marktwain": 78 fmt.Fprintf(w, `[]`) 79 default: 80 t.Fatalf("Unexpected marker: [%s]", marker) 81 } 82 }) 83 } 84 85 // HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that 86 // responds with a `ListNames` response when only container names are requested. 87 func HandleListContainerNamesSuccessfully(t *testing.T) { 88 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 89 th.TestMethod(t, r, "GET") 90 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 91 th.TestHeader(t, r, "Accept", "text/plain") 92 93 w.Header().Set("Content-Type", "text/plain") 94 r.ParseForm() 95 marker := r.Form.Get("marker") 96 switch marker { 97 case "": 98 fmt.Fprintf(w, "janeausten\nmarktwain\n") 99 case "janeausten": 100 fmt.Fprintf(w, "marktwain\n") 101 case "marktwain": 102 fmt.Fprintf(w, ``) 103 default: 104 t.Fatalf("Unexpected marker: [%s]", marker) 105 } 106 }) 107 } 108 109 // HandleListZeroContainerNames204 creates an HTTP handler at `/` on the test handler mux that 110 // responds with "204 No Content" when container names are requested. This happens on some, but not all, 111 // objectstorage instances. This case is peculiar in that the server sends no `content-type` header. 112 func HandleListZeroContainerNames204(t *testing.T) { 113 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 114 th.TestMethod(t, r, "GET") 115 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 116 th.TestHeader(t, r, "Accept", "text/plain") 117 118 w.WriteHeader(http.StatusNoContent) 119 }) 120 } 121 122 // HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 123 // responds with a `Create` response. 124 func HandleCreateContainerSuccessfully(t *testing.T) { 125 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { 126 th.TestMethod(t, r, "PUT") 127 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 128 th.TestHeader(t, r, "Accept", "application/json") 129 130 w.Header().Add("X-Container-Meta-Foo", "bar") 131 w.Header().Set("Content-Length", "0") 132 w.Header().Set("Content-Type", "text/html; charset=UTF-8") 133 w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 UTC") 134 w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0058b4ba37") 135 w.Header().Set("X-Storage-Policy", "multi-region-three-replicas") 136 w.WriteHeader(http.StatusNoContent) 137 }) 138 } 139 140 // HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 141 // responds with a `Delete` response. 142 func HandleDeleteContainerSuccessfully(t *testing.T, options ...option) { 143 ho := handlerOptions{ 144 path: "/testContainer", 145 } 146 for _, apply := range options { 147 apply(&ho) 148 } 149 150 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 151 th.TestMethod(t, r, "DELETE") 152 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 153 th.TestHeader(t, r, "Accept", "application/json") 154 w.WriteHeader(http.StatusNoContent) 155 }) 156 } 157 158 const bulkDeleteResponse = ` 159 { 160 "Response Status": "foo", 161 "Response Body": "bar", 162 "Errors": [], 163 "Number Deleted": 2, 164 "Number Not Found": 0 165 } 166 ` 167 168 // HandleBulkDeleteSuccessfully creates an HTTP handler at `/` on the test 169 // handler mux that responds with a `Delete` response. 170 func HandleBulkDeleteSuccessfully(t *testing.T) { 171 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 172 th.TestMethod(t, r, "POST") 173 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 174 th.TestHeader(t, r, "Accept", "application/json") 175 th.TestHeader(t, r, "Content-Type", "text/plain") 176 th.TestFormValues(t, r, map[string]string{ 177 "bulk-delete": "true", 178 }) 179 th.TestBody(t, r, "testContainer1\ntestContainer2\n") 180 181 w.Header().Set("Content-Type", "application/json") 182 w.WriteHeader(http.StatusOK) 183 fmt.Fprintf(w, bulkDeleteResponse) 184 }) 185 } 186 187 // HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 188 // responds with a `Update` response. 189 func HandleUpdateContainerSuccessfully(t *testing.T, options ...option) { 190 ho := handlerOptions{ 191 path: "/testContainer", 192 } 193 for _, apply := range options { 194 apply(&ho) 195 } 196 197 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 198 th.TestMethod(t, r, "POST") 199 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 200 th.TestHeader(t, r, "Accept", "application/json") 201 th.TestHeader(t, r, "X-Container-Write", "") 202 th.TestHeader(t, r, "X-Container-Read", "") 203 th.TestHeader(t, r, "X-Container-Sync-To", "") 204 th.TestHeader(t, r, "X-Container-Sync-Key", "") 205 th.TestHeader(t, r, "Content-Type", "text/plain") 206 w.WriteHeader(http.StatusNoContent) 207 }) 208 } 209 210 // HandleUpdateContainerVersioningOn creates an HTTP handler at `/testVersioning` on the test handler mux that 211 // responds with a `Update` response. 212 func HandleUpdateContainerVersioningOn(t *testing.T, options ...option) { 213 ho := handlerOptions{ 214 path: "/testVersioning", 215 } 216 for _, apply := range options { 217 apply(&ho) 218 } 219 220 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 221 th.TestMethod(t, r, "POST") 222 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 223 th.TestHeader(t, r, "Accept", "application/json") 224 th.TestHeader(t, r, "X-Container-Write", "") 225 th.TestHeader(t, r, "X-Container-Read", "") 226 th.TestHeader(t, r, "X-Container-Sync-To", "") 227 th.TestHeader(t, r, "X-Container-Sync-Key", "") 228 th.TestHeader(t, r, "Content-Type", "text/plain") 229 th.TestHeader(t, r, "X-Versions-Enabled", "true") 230 w.WriteHeader(http.StatusNoContent) 231 }) 232 } 233 234 // HandleUpdateContainerVersioningOff creates an HTTP handler at `/testVersioning` on the test handler mux that 235 // responds with a `Update` response. 236 func HandleUpdateContainerVersioningOff(t *testing.T, options ...option) { 237 ho := handlerOptions{ 238 path: "/testVersioning", 239 } 240 for _, apply := range options { 241 apply(&ho) 242 } 243 244 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 245 th.TestMethod(t, r, "POST") 246 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 247 th.TestHeader(t, r, "Accept", "application/json") 248 th.TestHeader(t, r, "X-Container-Write", "") 249 th.TestHeader(t, r, "X-Container-Read", "") 250 th.TestHeader(t, r, "X-Container-Sync-To", "") 251 th.TestHeader(t, r, "X-Container-Sync-Key", "") 252 th.TestHeader(t, r, "Content-Type", "text/plain") 253 th.TestHeader(t, r, "X-Versions-Enabled", "false") 254 w.WriteHeader(http.StatusNoContent) 255 }) 256 } 257 258 // HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 259 // responds with a `Get` response. 260 func HandleGetContainerSuccessfully(t *testing.T, options ...option) { 261 ho := handlerOptions{ 262 path: "/testContainer", 263 } 264 for _, apply := range options { 265 apply(&ho) 266 } 267 268 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 269 th.TestMethod(t, r, "HEAD") 270 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 271 th.TestHeader(t, r, "Accept", "application/json") 272 w.Header().Set("Accept-Ranges", "bytes") 273 w.Header().Set("Content-Type", "application/json; charset=utf-8") 274 w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 UTC") 275 w.Header().Set("X-Container-Bytes-Used", "100") 276 w.Header().Set("X-Container-Object-Count", "4") 277 w.Header().Set("X-Container-Read", "test") 278 w.Header().Set("X-Container-Write", "test2,user4") 279 w.Header().Set("X-Timestamp", "1471298837.95721") 280 w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0057b4ba37") 281 w.Header().Set("X-Storage-Policy", "test_policy") 282 w.Header().Set("X-Versions-Enabled", "True") 283 w.WriteHeader(http.StatusNoContent) 284 }) 285 }