Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "fmt" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 6 | "net/http" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 14 | const ( |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 15 | tokenId = "abcabcabcabc" |
| 16 | ) |
| 17 | |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 18 | var metadata = map[string]string{"Gophercloud-Test": "objects"} |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 19 | |
| 20 | func serviceClient() *gophercloud.ServiceClient { |
| 21 | return &gophercloud.ServiceClient{ |
| 22 | Provider: &gophercloud.ProviderClient{TokenID: tokenId}, |
| 23 | Endpoint: testhelper.Endpoint(), |
| 24 | } |
| 25 | } |
| 26 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 27 | func TestDownloadObject(t *testing.T) { |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 28 | testhelper.SetupHTTP() |
| 29 | defer testhelper.TeardownHTTP() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 30 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 31 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 32 | testhelper.TestMethod(t, r, "GET") |
| 33 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 34 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 35 | w.WriteHeader(http.StatusOK) |
| 36 | fmt.Fprintf(w, "Successful download with Gophercloud") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 37 | }) |
| 38 | |
| 39 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 40 | content, err := Download(client, "testContainer", "testObject", nil).ExtractContent() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Fatalf("Unexpected error downloading object: %v", err) |
| 43 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 44 | if string(content) != "Successful download with Gophercloud" { |
| 45 | t.Errorf("Expected %s, got %s", "Successful download with Gophercloud", content) |
| 46 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func TestListObjectInfo(t *testing.T) { |
| 50 | testhelper.SetupHTTP() |
| 51 | defer testhelper.TeardownHTTP() |
| 52 | |
| 53 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 54 | testhelper.TestMethod(t, r, "GET") |
| 55 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 56 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 57 | |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame] | 58 | w.Header().Set("Content-Type", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 59 | r.ParseForm() |
| 60 | marker := r.Form.Get("marker") |
| 61 | switch marker { |
| 62 | case "": |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 63 | fmt.Fprintf(w, `[ |
| 64 | { |
| 65 | "hash": "451e372e48e0f6b1114fa0724aa79fa1", |
| 66 | "last_modified": "2009-11-10 23:00:00 +0000 UTC", |
| 67 | "bytes": 14, |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame^] | 68 | "name": "goodbye", |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 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, `[]`) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 81 | default: |
| 82 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 83 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 84 | }) |
| 85 | |
| 86 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 87 | count := 0 |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame^] | 88 | err := List(client, "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 89 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 90 | actual, err := ExtractInfo(page) |
| 91 | if err != nil { |
| 92 | t.Errorf("Failed to extract object info: %v", err) |
| 93 | return false, err |
| 94 | } |
| 95 | |
| 96 | expected := []Object{ |
| 97 | Object{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 98 | Hash: "451e372e48e0f6b1114fa0724aa79fa1", |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 99 | LastModified: "2009-11-10 23:00:00 +0000 UTC", |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 100 | Bytes: 14, |
| 101 | Name: "goodbye", |
| 102 | ContentType: "application/octet-stream", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 103 | }, |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 104 | Object{ |
| 105 | Hash: "451e372e48e0f6b1114fa0724aa79fa1", |
| 106 | LastModified: "2009-11-10 23:00:00 +0000 UTC", |
| 107 | Bytes: 14, |
| 108 | Name: "hello", |
| 109 | ContentType: "application/octet-stream", |
| 110 | }, |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame^] | 113 | testhelper.CheckDeepEquals(t, actual, expected) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 114 | |
| 115 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 116 | }) |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame^] | 117 | if err != nil { |
| 118 | t.Error(err) |
| 119 | } |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 120 | |
| 121 | if count != 1 { |
| 122 | t.Errorf("Expected 1 page, got %d", count) |
| 123 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func TestListObjectNames(t *testing.T) { |
| 127 | testhelper.SetupHTTP() |
| 128 | defer testhelper.TeardownHTTP() |
| 129 | |
| 130 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 131 | testhelper.TestMethod(t, r, "GET") |
| 132 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 133 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 134 | |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame] | 135 | w.Header().Set("Content-Type", "text/plain") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 136 | r.ParseForm() |
| 137 | marker := r.Form.Get("marker") |
| 138 | switch marker { |
| 139 | case "": |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 140 | fmt.Fprintf(w, "hello\ngoodbye\n") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 141 | case "goodbye": |
| 142 | fmt.Fprintf(w, "") |
| 143 | default: |
| 144 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 145 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 146 | }) |
| 147 | |
| 148 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 149 | count := 0 |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame] | 150 | List(client, "testContainer", &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 151 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 152 | actual, err := ExtractNames(page) |
| 153 | if err != nil { |
| 154 | t.Errorf("Failed to extract object names: %v", err) |
| 155 | return false, err |
| 156 | } |
| 157 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 158 | expected := []string{"hello", "goodbye"} |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 159 | |
| 160 | testhelper.CheckDeepEquals(t, expected, actual) |
| 161 | |
| 162 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 163 | }) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 164 | |
| 165 | if count != 1 { |
| 166 | t.Errorf("Expected 1 page, got %d", count) |
| 167 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 168 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 169 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 170 | func TestCreateObject(t *testing.T) { |
| 171 | testhelper.SetupHTTP() |
| 172 | defer testhelper.TeardownHTTP() |
| 173 | |
| 174 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 175 | testhelper.TestMethod(t, r, "PUT") |
| 176 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 177 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 178 | w.WriteHeader(http.StatusCreated) |
| 179 | }) |
| 180 | |
| 181 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 182 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 183 | err := Create(client, "testContainer", "testObject", content, nil) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 184 | if err != nil { |
| 185 | t.Fatalf("Unexpected error creating object: %v", err) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestCopyObject(t *testing.T) { |
| 190 | testhelper.SetupHTTP() |
| 191 | defer testhelper.TeardownHTTP() |
| 192 | |
| 193 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 194 | testhelper.TestMethod(t, r, "COPY") |
| 195 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 196 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 197 | testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 198 | w.WriteHeader(http.StatusCreated) |
| 199 | }) |
| 200 | |
| 201 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 202 | err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 203 | if err != nil { |
| 204 | t.Fatalf("Unexpected error copying object: %v", err) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestDeleteObject(t *testing.T) { |
| 209 | testhelper.SetupHTTP() |
| 210 | defer testhelper.TeardownHTTP() |
| 211 | |
| 212 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 213 | testhelper.TestMethod(t, r, "DELETE") |
| 214 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 215 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 216 | w.WriteHeader(http.StatusNoContent) |
| 217 | }) |
| 218 | |
| 219 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 220 | err := Delete(client, "testContainer", "testObject", nil) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 221 | if err != nil { |
| 222 | t.Fatalf("Unexpected error deleting object: %v", err) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func TestUpateObjectMetadata(t *testing.T) { |
| 227 | testhelper.SetupHTTP() |
| 228 | defer testhelper.TeardownHTTP() |
| 229 | |
| 230 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 231 | testhelper.TestMethod(t, r, "POST") |
| 232 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 233 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 234 | testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 235 | w.WriteHeader(http.StatusAccepted) |
| 236 | }) |
| 237 | |
| 238 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 239 | err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 240 | if err != nil { |
| 241 | t.Fatalf("Unexpected error updating object metadata: %v", err) |
| 242 | } |
| 243 | } |
| 244 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 245 | func TestGetObject(t *testing.T) { |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 246 | testhelper.SetupHTTP() |
| 247 | defer testhelper.TeardownHTTP() |
| 248 | |
| 249 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 250 | testhelper.TestMethod(t, r, "HEAD") |
| 251 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 252 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 253 | w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 254 | w.WriteHeader(http.StatusNoContent) |
| 255 | }) |
| 256 | |
| 257 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 258 | expected := metadata |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 259 | actual, err := Get(client, "testContainer", "testObject", nil).ExtractMetadata() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 260 | if err != nil { |
| 261 | t.Fatalf("Unexpected error getting object metadata: %v", err) |
| 262 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 263 | testhelper.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 264 | } |