jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 4 | "crypto/md5" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 5 | "fmt" |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 6 | "io" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 7 | "net/http" |
| 8 | "testing" |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 9 | "time" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 10 | |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 11 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/objectstorage/v1/objects" |
| 12 | th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper" |
| 13 | fake "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that |
| 17 | // responds with a `Download` response. |
| 18 | func HandleDownloadObjectSuccessfully(t *testing.T) { |
| 19 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 20 | th.TestMethod(t, r, "GET") |
| 21 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 22 | th.TestHeader(t, r, "Accept", "application/json") |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 23 | w.Header().Set("Date", "Wed, 10 Nov 2009 23:00:00 GMT") |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 24 | w.WriteHeader(http.StatusOK) |
| 25 | fmt.Fprintf(w, "Successful download with Gophercloud") |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | // ExpectedListInfo is the result expected from a call to `List` when full |
| 30 | // info is requested. |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 31 | var ExpectedListInfo = []objects.Object{ |
| 32 | { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 33 | Hash: "451e372e48e0f6b1114fa0724aa79fa1", |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 34 | LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC), //"2016-08-17T22:11:58.602650" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 35 | Bytes: 14, |
| 36 | Name: "goodbye", |
| 37 | ContentType: "application/octet-stream", |
| 38 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 39 | { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 40 | Hash: "451e372e48e0f6b1114fa0724aa79fa1", |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 41 | LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC), |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 42 | Bytes: 14, |
| 43 | Name: "hello", |
| 44 | ContentType: "application/octet-stream", |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | // ExpectedListNames is the result expected from a call to `List` when just |
| 49 | // object names are requested. |
| 50 | var ExpectedListNames = []string{"hello", "goodbye"} |
| 51 | |
| 52 | // HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 53 | // responds with a `List` response when full info is requested. |
| 54 | func HandleListObjectsInfoSuccessfully(t *testing.T) { |
| 55 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 56 | th.TestMethod(t, r, "GET") |
| 57 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 58 | th.TestHeader(t, r, "Accept", "application/json") |
| 59 | |
| 60 | w.Header().Set("Content-Type", "application/json") |
| 61 | r.ParseForm() |
| 62 | marker := r.Form.Get("marker") |
| 63 | switch marker { |
| 64 | case "": |
| 65 | fmt.Fprintf(w, `[ |
| 66 | { |
| 67 | "hash": "451e372e48e0f6b1114fa0724aa79fa1", |
jrperritt | 20c0852 | 2016-08-31 15:56:38 -0500 | [diff] [blame] | 68 | "last_modified": "2016-08-17T22:11:58.602650", |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 69 | "bytes": 14, |
| 70 | "name": "goodbye", |
| 71 | "content_type": "application/octet-stream" |
| 72 | }, |
| 73 | { |
| 74 | "hash": "451e372e48e0f6b1114fa0724aa79fa1", |
jrperritt | 20c0852 | 2016-08-31 15:56:38 -0500 | [diff] [blame] | 75 | "last_modified": "2016-08-17T22:11:58.602650", |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 76 | "bytes": 14, |
| 77 | "name": "hello", |
| 78 | "content_type": "application/octet-stream" |
| 79 | } |
| 80 | ]`) |
| 81 | case "hello": |
| 82 | fmt.Fprintf(w, `[]`) |
| 83 | default: |
| 84 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | // HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 90 | // responds with a `List` response when only object names are requested. |
| 91 | func HandleListObjectNamesSuccessfully(t *testing.T) { |
| 92 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 93 | th.TestMethod(t, r, "GET") |
| 94 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 95 | th.TestHeader(t, r, "Accept", "text/plain") |
| 96 | |
| 97 | w.Header().Set("Content-Type", "text/plain") |
| 98 | r.ParseForm() |
| 99 | marker := r.Form.Get("marker") |
| 100 | switch marker { |
| 101 | case "": |
| 102 | fmt.Fprintf(w, "hello\ngoodbye\n") |
| 103 | case "goodbye": |
| 104 | fmt.Fprintf(w, "") |
| 105 | default: |
| 106 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 111 | // HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux |
| 112 | // that responds with a `Create` response. A Content-Type of "text/plain" is expected. |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 113 | func HandleCreateTextObjectSuccessfully(t *testing.T, content string) { |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 114 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 115 | th.TestMethod(t, r, "PUT") |
| 116 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 117 | th.TestHeader(t, r, "Content-Type", "text/plain") |
| 118 | th.TestHeader(t, r, "Accept", "application/json") |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 119 | |
| 120 | hash := md5.New() |
| 121 | io.WriteString(hash, content) |
| 122 | localChecksum := hash.Sum(nil) |
| 123 | |
| 124 | w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 125 | w.WriteHeader(http.StatusCreated) |
| 126 | }) |
| 127 | } |
| 128 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 129 | // HandleCreateTextWithCacheControlSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler |
| 130 | // mux that responds with a `Create` response. A Cache-Control of `max-age="3600", public` is expected. |
| 131 | func HandleCreateTextWithCacheControlSuccessfully(t *testing.T, content string) { |
| 132 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 133 | th.TestMethod(t, r, "PUT") |
| 134 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 135 | th.TestHeader(t, r, "Cache-Control", `max-age="3600", public`) |
| 136 | th.TestHeader(t, r, "Accept", "application/json") |
| 137 | |
| 138 | hash := md5.New() |
| 139 | io.WriteString(hash, content) |
| 140 | localChecksum := hash.Sum(nil) |
| 141 | |
| 142 | w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) |
| 143 | w.WriteHeader(http.StatusCreated) |
| 144 | }) |
| 145 | } |
| 146 | |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 147 | // HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler |
| 148 | // mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server- |
| 149 | // side content-type detection will be triggered properly. |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 150 | func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) { |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 151 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 152 | th.TestMethod(t, r, "PUT") |
| 153 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 154 | th.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | 93beae0 | 2015-01-23 14:14:48 -0500 | [diff] [blame] | 155 | |
| 156 | if contentType, present := r.Header["Content-Type"]; present { |
| 157 | t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType) |
| 158 | } |
| 159 | |
Jamie Hannaford | 0809623 | 2015-07-13 12:47:28 +0200 | [diff] [blame] | 160 | hash := md5.New() |
| 161 | io.WriteString(hash, content) |
| 162 | localChecksum := hash.Sum(nil) |
| 163 | |
| 164 | w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum)) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 165 | w.WriteHeader(http.StatusCreated) |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | // HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that |
| 170 | // responds with a `Copy` response. |
| 171 | func HandleCopyObjectSuccessfully(t *testing.T) { |
| 172 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 173 | th.TestMethod(t, r, "COPY") |
| 174 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 175 | th.TestHeader(t, r, "Accept", "application/json") |
| 176 | th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 177 | w.WriteHeader(http.StatusCreated) |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | // HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that |
| 182 | // responds with a `Delete` response. |
| 183 | func HandleDeleteObjectSuccessfully(t *testing.T) { |
| 184 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 185 | th.TestMethod(t, r, "DELETE") |
| 186 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 187 | th.TestHeader(t, r, "Accept", "application/json") |
| 188 | w.WriteHeader(http.StatusNoContent) |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | // HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that |
| 193 | // responds with a `Update` response. |
| 194 | func HandleUpdateObjectSuccessfully(t *testing.T) { |
| 195 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 196 | th.TestMethod(t, r, "POST") |
| 197 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 198 | th.TestHeader(t, r, "Accept", "application/json") |
| 199 | th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 200 | w.WriteHeader(http.StatusAccepted) |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | // HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that |
| 205 | // responds with a `Get` response. |
| 206 | func HandleGetObjectSuccessfully(t *testing.T) { |
| 207 | th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 208 | th.TestMethod(t, r, "HEAD") |
| 209 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 210 | th.TestHeader(t, r, "Accept", "application/json") |
| 211 | w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects") |
| 212 | w.WriteHeader(http.StatusNoContent) |
| 213 | }) |
| 214 | } |