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