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 | |
| 18 | var metadata = map[string]string{"gophercloud-test": "objects"} |
| 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") |
| 58 | w.WriteHeader(http.StatusOK) |
| 59 | fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2014-01-15T16:41:49.390270','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 60 | }) |
| 61 | |
| 62 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 63 | count := 0 |
| 64 | List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
| 65 | count++ |
| 66 | actual, err := ExtractInfo(page) |
| 67 | if err != nil { |
| 68 | t.Errorf("Failed to extract object info: %v", err) |
| 69 | return false, err |
| 70 | } |
| 71 | |
| 72 | expected := []Object{ |
| 73 | Object{ |
| 74 | "hash": "451e372e48e0f6b1114fa0724aa79fa1", |
| 75 | "last_modified": "2014-01-15T16:41:49.390270", |
| 76 | "bytes": 14, |
| 77 | "name": "goodbye", |
| 78 | "content_type": "application/octet-stream", |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | testhelper.CheckDeepEquals(t, expected, actual) |
| 83 | |
| 84 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 85 | }) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 86 | |
| 87 | if count != 1 { |
| 88 | t.Errorf("Expected 1 page, got %d", count) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 89 | } |
| 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") |
| 102 | w.WriteHeader(http.StatusOK) |
| 103 | fmt.Fprintf(w, "") |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 104 | }) |
| 105 | |
| 106 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 107 | count := 0 |
| 108 | List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 109 | count++ |
| 110 | actual, err := ExtractNames(page) |
| 111 | if err != nil { |
| 112 | t.Errorf("Failed to extract object names: %v", err) |
| 113 | return false, err |
| 114 | } |
| 115 | |
| 116 | expected := []string{"helloworld", "goodbye"} |
| 117 | |
| 118 | testhelper.CheckDeepEquals(t, expected, actual) |
| 119 | |
| 120 | return true, nil |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 121 | }) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 122 | |
| 123 | if count != 0 { |
| 124 | t.Fatalf("Expected 0 pages, got %d", count) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 125 | } |
| 126 | } |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 127 | func TestCreateObject(t *testing.T) { |
| 128 | testhelper.SetupHTTP() |
| 129 | defer testhelper.TeardownHTTP() |
| 130 | |
| 131 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 132 | testhelper.TestMethod(t, r, "PUT") |
| 133 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 134 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 135 | w.WriteHeader(http.StatusCreated) |
| 136 | }) |
| 137 | |
| 138 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 139 | content := bytes.NewBufferString("Did gyre and gimble in the wabe") |
| 140 | err := Create(client, "testContainer", "testObject", content, CreateOpts{}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 141 | if err != nil { |
| 142 | t.Fatalf("Unexpected error creating object: %v", err) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestCopyObject(t *testing.T) { |
| 147 | testhelper.SetupHTTP() |
| 148 | defer testhelper.TeardownHTTP() |
| 149 | |
| 150 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 151 | testhelper.TestMethod(t, r, "COPY") |
| 152 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 153 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 154 | testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject") |
| 155 | w.WriteHeader(http.StatusCreated) |
| 156 | }) |
| 157 | |
| 158 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 159 | err := Copy(client, "testContainer", "testObject", CopyOpts{Destination: "/newTestContainer/newTestObject"}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 160 | if err != nil { |
| 161 | t.Fatalf("Unexpected error copying object: %v", err) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestDeleteObject(t *testing.T) { |
| 166 | testhelper.SetupHTTP() |
| 167 | defer testhelper.TeardownHTTP() |
| 168 | |
| 169 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 170 | testhelper.TestMethod(t, r, "DELETE") |
| 171 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 172 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 173 | w.WriteHeader(http.StatusNoContent) |
| 174 | }) |
| 175 | |
| 176 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 177 | err := Delete(client, "testContainer", "testObject", DeleteOpts{}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 178 | if err != nil { |
| 179 | t.Fatalf("Unexpected error deleting object: %v", err) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func TestUpateObjectMetadata(t *testing.T) { |
| 184 | testhelper.SetupHTTP() |
| 185 | defer testhelper.TeardownHTTP() |
| 186 | |
| 187 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 188 | testhelper.TestMethod(t, r, "POST") |
| 189 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 190 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 191 | testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects") |
| 192 | w.WriteHeader(http.StatusAccepted) |
| 193 | }) |
| 194 | |
| 195 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 196 | err := Update(client, "testContainer", "testObject", UpdateOpts{Metadata: metadata}) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 197 | if err != nil { |
| 198 | t.Fatalf("Unexpected error updating object metadata: %v", err) |
| 199 | } |
| 200 | } |
| 201 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 202 | func TestGetObject(t *testing.T) { |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 203 | testhelper.SetupHTTP() |
| 204 | defer testhelper.TeardownHTTP() |
| 205 | |
| 206 | testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) { |
| 207 | testhelper.TestMethod(t, r, "HEAD") |
| 208 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 209 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 210 | w.WriteHeader(http.StatusNoContent) |
| 211 | }) |
| 212 | |
| 213 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 214 | expected := metadata |
| 215 | actual, err := Get(client, "testContainer", "testObject", GetOpts{}).ExtractMetadata() |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 216 | if err != nil { |
| 217 | t.Fatalf("Unexpected error getting object metadata: %v", err) |
| 218 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 219 | testhelper.CheckDeepEquals(t, expected, actual) |
Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 220 | } |