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