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