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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 40 | content, err := Download(client, "testContainer", "testObject", DownloadOpts{}).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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 56 | |
| 57 | w.Header().Set("Content-Type", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 58 | r.ParseForm() |
| 59 | marker := r.Form.Get("marker") |
| 60 | switch marker { |
| 61 | case "": |
| 62 | fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2009-11-10 23:00:00 +0000 UTC','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`) |
| 63 | default: |
| 64 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 65 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 66 | }) |
| 67 | |
| 68 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 69 | List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | actual, err := ExtractInfo(page) |
| 71 | if err != nil { |
| 72 | t.Errorf("Failed to extract object info: %v", err) |
| 73 | return false, err |
| 74 | } |
| 75 | |
| 76 | expected := []Object{ |
| 77 | Object{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 78 | Hash: "451e372e48e0f6b1114fa0724aa79fa1", |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame^] | 79 | LastModified: "2009-11-10 23:00:00 +0000 UTC", |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 80 | Bytes: 14, |
| 81 | Name: "goodbye", |
| 82 | ContentType: "application/octet-stream", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 83 | }, |
| 84 | } |
| 85 | |
| 86 | testhelper.CheckDeepEquals(t, expected, actual) |
| 87 | |
| 88 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 89 | }) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func TestListObjectNames(t *testing.T) { |
| 93 | testhelper.SetupHTTP() |
| 94 | defer testhelper.TeardownHTTP() |
| 95 | |
| 96 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 97 | testhelper.TestMethod(t, r, "GET") |
| 98 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 99 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 100 | |
| 101 | w.Header().Add("Content-Type", "text/plain") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame^] | 102 | r.ParseForm() |
| 103 | marker := r.Form.Get("marker") |
| 104 | switch marker { |
| 105 | case "": |
| 106 | fmt.Fprintf(w, "goodbye\n") |
| 107 | case "goodbye": |
| 108 | fmt.Fprintf(w, "") |
| 109 | default: |
| 110 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 111 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 112 | }) |
| 113 | |
| 114 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 115 | List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 116 | actual, err := ExtractNames(page) |
| 117 | if err != nil { |
| 118 | t.Errorf("Failed to extract object names: %v", err) |
| 119 | return false, err |
| 120 | } |
| 121 | |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame^] | 122 | expected := []string{"goodbye"} |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 123 | |
| 124 | testhelper.CheckDeepEquals(t, expected, actual) |
| 125 | |
| 126 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 127 | }) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 128 | } |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 129 | |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 130 | func TestCreateObject(t *testing.T) { |
| 131 | testhelper.SetupHTTP() |
| 132 | defer testhelper.TeardownHTTP() |
| 133 | |
| 134 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 135 | testhelper.TestMethod(t, r, "PUT") |
| 136 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 137 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 138 | w.WriteHeader(http.StatusCreated) |
| 139 | }) |
| 140 | |
| 141 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 142 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
| 143 | err := Create(client, "testContainer", "testObject", content, CreateOpts{}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 144 | if err != nil { |
| 145 | t.Fatalf("Unexpected error creating object: %v", err) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestCopyObject(t *testing.T) { |
| 150 | testhelper.SetupHTTP() |
| 151 | defer testhelper.TeardownHTTP() |
| 152 | |
| 153 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 154 | testhelper.TestMethod(t, r, "COPY") |
| 155 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 156 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 157 | testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 158 | w.WriteHeader(http.StatusCreated) |
| 159 | }) |
| 160 | |
| 161 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 162 | err := Copy(client, "testContainer", "testObject", CopyOpts{Destination: "/newTestContainer/newTestObject"}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 163 | if err != nil { |
| 164 | t.Fatalf("Unexpected error copying object: %v", err) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestDeleteObject(t *testing.T) { |
| 169 | testhelper.SetupHTTP() |
| 170 | defer testhelper.TeardownHTTP() |
| 171 | |
| 172 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 173 | testhelper.TestMethod(t, r, "DELETE") |
| 174 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 175 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 176 | w.WriteHeader(http.StatusNoContent) |
| 177 | }) |
| 178 | |
| 179 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 180 | err := Delete(client, "testContainer", "testObject", DeleteOpts{}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 181 | if err != nil { |
| 182 | t.Fatalf("Unexpected error deleting object: %v", err) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestUpateObjectMetadata(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, "POST") |
| 192 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 193 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 194 | testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 195 | w.WriteHeader(http.StatusAccepted) |
| 196 | }) |
| 197 | |
| 198 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 199 | err := Update(client, "testContainer", "testObject", UpdateOpts{Metadata: metadata}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 200 | if err != nil { |
| 201 | t.Fatalf("Unexpected error updating object metadata: %v", err) |
| 202 | } |
| 203 | } |
| 204 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 205 | func TestGetObject(t *testing.T) { |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 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, "HEAD") |
| 211 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 212 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 213 | w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 214 | w.WriteHeader(http.StatusNoContent) |
| 215 | }) |
| 216 | |
| 217 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 218 | expected := metadata |
| 219 | actual, err := Get(client, "testContainer", "testObject", GetOpts{}).ExtractMetadata() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 220 | if err != nil { |
| 221 | t.Fatalf("Unexpected error getting object metadata: %v", err) |
| 222 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 223 | testhelper.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 224 | } |