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, |
| 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, `[]`) |
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 |
| 88 | List(client, "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
| 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 | |
| 113 | testhelper.CheckDeepEquals(t, expected, actual) |
| 114 | |
| 115 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 116 | }) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 117 | |
| 118 | if count != 1 { |
| 119 | t.Errorf("Expected 1 page, got %d", count) |
| 120 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func TestListObjectNames(t *testing.T) { |
| 124 | testhelper.SetupHTTP() |
| 125 | defer testhelper.TeardownHTTP() |
| 126 | |
| 127 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 128 | testhelper.TestMethod(t, r, "GET") |
| 129 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 130 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 131 | |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame^] | 132 | w.Header().Set("Content-Type", "text/plain") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 133 | r.ParseForm() |
| 134 | marker := r.Form.Get("marker") |
| 135 | switch marker { |
| 136 | case "": |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 137 | fmt.Fprintf(w, "hello\ngoodbye\n") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 138 | case "goodbye": |
| 139 | fmt.Fprintf(w, "") |
| 140 | default: |
| 141 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 142 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 143 | }) |
| 144 | |
| 145 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 146 | count := 0 |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame^] | 147 | 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] | 148 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 149 | actual, err := ExtractNames(page) |
| 150 | if err != nil { |
| 151 | t.Errorf("Failed to extract object names: %v", err) |
| 152 | return false, err |
| 153 | } |
| 154 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 155 | expected := []string{"hello", "goodbye"} |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 156 | |
| 157 | testhelper.CheckDeepEquals(t, expected, actual) |
| 158 | |
| 159 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 160 | }) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 161 | |
| 162 | if count != 1 { |
| 163 | t.Errorf("Expected 1 page, got %d", count) |
| 164 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 165 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 166 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 167 | func TestCreateObject(t *testing.T) { |
| 168 | testhelper.SetupHTTP() |
| 169 | defer testhelper.TeardownHTTP() |
| 170 | |
| 171 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 172 | testhelper.TestMethod(t, r, "PUT") |
| 173 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 174 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 175 | w.WriteHeader(http.StatusCreated) |
| 176 | }) |
| 177 | |
| 178 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 179 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 180 | err := Create(client, "testContainer", "testObject", content, nil) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 181 | if err != nil { |
| 182 | t.Fatalf("Unexpected error creating object: %v", err) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestCopyObject(t *testing.T) { |
| 187 | testhelper.SetupHTTP() |
| 188 | defer testhelper.TeardownHTTP() |
| 189 | |
| 190 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 191 | testhelper.TestMethod(t, r, "COPY") |
| 192 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 193 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 194 | testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 195 | w.WriteHeader(http.StatusCreated) |
| 196 | }) |
| 197 | |
| 198 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 199 | err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 200 | if err != nil { |
| 201 | t.Fatalf("Unexpected error copying object: %v", err) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestDeleteObject(t *testing.T) { |
| 206 | testhelper.SetupHTTP() |
| 207 | defer testhelper.TeardownHTTP() |
| 208 | |
| 209 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 210 | testhelper.TestMethod(t, r, "DELETE") |
| 211 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 212 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 213 | w.WriteHeader(http.StatusNoContent) |
| 214 | }) |
| 215 | |
| 216 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 217 | err := Delete(client, "testContainer", "testObject", nil) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 218 | if err != nil { |
| 219 | t.Fatalf("Unexpected error deleting object: %v", err) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestUpateObjectMetadata(t *testing.T) { |
| 224 | testhelper.SetupHTTP() |
| 225 | defer testhelper.TeardownHTTP() |
| 226 | |
| 227 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 228 | testhelper.TestMethod(t, r, "POST") |
| 229 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 230 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 231 | testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 232 | w.WriteHeader(http.StatusAccepted) |
| 233 | }) |
| 234 | |
| 235 | client := serviceClient() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 236 | err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 237 | if err != nil { |
| 238 | t.Fatalf("Unexpected error updating object metadata: %v", err) |
| 239 | } |
| 240 | } |
| 241 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 242 | func TestGetObject(t *testing.T) { |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 243 | testhelper.SetupHTTP() |
| 244 | defer testhelper.TeardownHTTP() |
| 245 | |
| 246 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 247 | testhelper.TestMethod(t, r, "HEAD") |
| 248 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 249 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 250 | w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 251 | w.WriteHeader(http.StatusNoContent) |
| 252 | }) |
| 253 | |
| 254 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 255 | expected := metadata |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 256 | actual, err := Get(client, "testContainer", "testObject", nil).ExtractMetadata() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 257 | if err != nil { |
| 258 | t.Fatalf("Unexpected error getting object metadata: %v", err) |
| 259 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 260 | testhelper.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 261 | } |