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