github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/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/vnpaycloud-console/gophercloud/v2/openstack/objectstorage/v1/containers" 9 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 10 fake "github.com/vnpaycloud-console/gophercloud/v2/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 if err := r.ParseForm(); err != nil { 54 t.Errorf("Failed to parse request form %v", err) 55 } 56 marker := r.Form.Get("marker") 57 switch marker { 58 case "": 59 fmt.Fprint(w, `[ 60 { 61 "count": 0, 62 "bytes": 0, 63 "name": "janeausten" 64 }, 65 { 66 "count": 1, 67 "bytes": 14, 68 "name": "marktwain" 69 } 70 ]`) 71 case "janeausten": 72 fmt.Fprint(w, `[ 73 { 74 "count": 1, 75 "bytes": 14, 76 "name": "marktwain" 77 } 78 ]`) 79 case "marktwain": 80 fmt.Fprint(w, `[]`) 81 default: 82 t.Fatalf("Unexpected marker: [%s]", marker) 83 } 84 }) 85 } 86 87 // HandleListZeroContainerNames204 creates an HTTP handler at `/` on the test handler mux that 88 // responds with "204 No Content" when container names are requested. This happens on some, but not all, 89 // objectstorage instances. This case is peculiar in that the server sends no `content-type` header. 90 func HandleListZeroContainerNames204(t *testing.T) { 91 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 92 th.TestMethod(t, r, "GET") 93 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 94 th.TestHeader(t, r, "Accept", "application/json") 95 96 w.WriteHeader(http.StatusNoContent) 97 }) 98 } 99 100 // HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 101 // responds with a `Create` response. 102 func HandleCreateContainerSuccessfully(t *testing.T) { 103 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { 104 th.TestMethod(t, r, "PUT") 105 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 106 th.TestHeader(t, r, "Accept", "application/json") 107 108 w.Header().Add("X-Container-Meta-Foo", "bar") 109 w.Header().Set("Content-Length", "0") 110 w.Header().Set("Content-Type", "text/html; charset=UTF-8") 111 w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 UTC") 112 w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0058b4ba37") 113 w.Header().Set("X-Storage-Policy", "multi-region-three-replicas") 114 w.WriteHeader(http.StatusNoContent) 115 }) 116 } 117 118 // HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 119 // responds with a `Delete` response. 120 func HandleDeleteContainerSuccessfully(t *testing.T, options ...option) { 121 ho := handlerOptions{ 122 path: "/testContainer", 123 } 124 for _, apply := range options { 125 apply(&ho) 126 } 127 128 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 129 th.TestMethod(t, r, "DELETE") 130 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 131 th.TestHeader(t, r, "Accept", "application/json") 132 w.WriteHeader(http.StatusNoContent) 133 }) 134 } 135 136 const bulkDeleteResponse = ` 137 { 138 "Response Status": "foo", 139 "Response Body": "bar", 140 "Errors": [], 141 "Number Deleted": 2, 142 "Number Not Found": 0 143 } 144 ` 145 146 // HandleBulkDeleteSuccessfully creates an HTTP handler at `/` on the test 147 // handler mux that responds with a `Delete` response. 148 func HandleBulkDeleteSuccessfully(t *testing.T) { 149 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 150 th.TestMethod(t, r, "POST") 151 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 152 th.TestHeader(t, r, "Accept", "application/json") 153 th.TestHeader(t, r, "Content-Type", "text/plain") 154 th.TestFormValues(t, r, map[string]string{ 155 "bulk-delete": "true", 156 }) 157 th.TestBody(t, r, "testContainer1\ntestContainer2\n") 158 159 w.Header().Set("Content-Type", "application/json") 160 w.WriteHeader(http.StatusOK) 161 fmt.Fprint(w, bulkDeleteResponse) 162 }) 163 } 164 165 // HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 166 // responds with a `Update` response. 167 func HandleUpdateContainerSuccessfully(t *testing.T, options ...option) { 168 ho := handlerOptions{ 169 path: "/testContainer", 170 } 171 for _, apply := range options { 172 apply(&ho) 173 } 174 175 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 176 th.TestMethod(t, r, "POST") 177 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 178 th.TestHeader(t, r, "Accept", "application/json") 179 th.TestHeader(t, r, "X-Container-Write", "") 180 th.TestHeader(t, r, "X-Container-Read", "") 181 th.TestHeader(t, r, "X-Container-Sync-To", "") 182 th.TestHeader(t, r, "X-Container-Sync-Key", "") 183 th.TestHeader(t, r, "Content-Type", "text/plain") 184 w.WriteHeader(http.StatusNoContent) 185 }) 186 } 187 188 // HandleUpdateContainerVersioningOn creates an HTTP handler at `/testVersioning` on the test handler mux that 189 // responds with a `Update` response. 190 func HandleUpdateContainerVersioningOn(t *testing.T, options ...option) { 191 ho := handlerOptions{ 192 path: "/testVersioning", 193 } 194 for _, apply := range options { 195 apply(&ho) 196 } 197 198 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 199 th.TestMethod(t, r, "POST") 200 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 201 th.TestHeader(t, r, "Accept", "application/json") 202 th.TestHeader(t, r, "X-Container-Write", "") 203 th.TestHeader(t, r, "X-Container-Read", "") 204 th.TestHeader(t, r, "X-Container-Sync-To", "") 205 th.TestHeader(t, r, "X-Container-Sync-Key", "") 206 th.TestHeader(t, r, "Content-Type", "text/plain") 207 th.TestHeader(t, r, "X-Versions-Enabled", "true") 208 w.WriteHeader(http.StatusNoContent) 209 }) 210 } 211 212 // HandleUpdateContainerVersioningOff creates an HTTP handler at `/testVersioning` on the test handler mux that 213 // responds with a `Update` response. 214 func HandleUpdateContainerVersioningOff(t *testing.T, options ...option) { 215 ho := handlerOptions{ 216 path: "/testVersioning", 217 } 218 for _, apply := range options { 219 apply(&ho) 220 } 221 222 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 223 th.TestMethod(t, r, "POST") 224 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 225 th.TestHeader(t, r, "Accept", "application/json") 226 th.TestHeader(t, r, "X-Container-Write", "") 227 th.TestHeader(t, r, "X-Container-Read", "") 228 th.TestHeader(t, r, "X-Container-Sync-To", "") 229 th.TestHeader(t, r, "X-Container-Sync-Key", "") 230 th.TestHeader(t, r, "Content-Type", "text/plain") 231 th.TestHeader(t, r, "X-Versions-Enabled", "false") 232 w.WriteHeader(http.StatusNoContent) 233 }) 234 } 235 236 // HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that 237 // responds with a `Get` response. 238 func HandleGetContainerSuccessfully(t *testing.T, options ...option) { 239 ho := handlerOptions{ 240 path: "/testContainer", 241 } 242 for _, apply := range options { 243 apply(&ho) 244 } 245 246 th.Mux.HandleFunc(ho.path, func(w http.ResponseWriter, r *http.Request) { 247 th.TestMethod(t, r, "HEAD") 248 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 249 th.TestHeader(t, r, "Accept", "application/json") 250 w.Header().Set("Accept-Ranges", "bytes") 251 w.Header().Set("Content-Type", "application/json; charset=utf-8") 252 w.Header().Set("Date", "Wed, 17 Aug 2016 19:25:43 UTC") 253 w.Header().Set("X-Container-Bytes-Used", "100") 254 w.Header().Set("X-Container-Object-Count", "4") 255 w.Header().Set("X-Container-Read", "test") 256 w.Header().Set("X-Container-Write", "test2,user4") 257 w.Header().Set("X-Timestamp", "1471298837.95721") 258 w.Header().Set("X-Trans-Id", "tx554ed59667a64c61866f1-0057b4ba37") 259 w.Header().Set("X-Storage-Policy", "test_policy") 260 w.Header().Set("X-Versions-Enabled", "True") 261 w.Header().Set("X-Sync-Key", "272465181849") 262 w.Header().Set("X-Sync-To", "anotherContainer") 263 w.WriteHeader(http.StatusNoContent) 264 }) 265 }