blob: 6747544fe0b369d7365cf158bf40d18ceb402b3f [file] [log] [blame]
Jon Perrittf81e17a2014-09-15 01:29:41 -05001package objects
2
3import (
4 "bytes"
Jon Perritt8c93a302014-09-28 22:35:57 -05005 "fmt"
Jon Perrittf81e17a2014-09-15 01:29:41 -05006 "net/http"
7 "testing"
8
9 "github.com/rackspace/gophercloud"
Jon Perritt8c93a302014-09-28 22:35:57 -050010 "github.com/rackspace/gophercloud/pagination"
Jon Perrittf81e17a2014-09-15 01:29:41 -050011 "github.com/rackspace/gophercloud/testhelper"
12)
13
Jon Perritt8c93a302014-09-28 22:35:57 -050014const (
Jon Perrittf81e17a2014-09-15 01:29:41 -050015 tokenId = "abcabcabcabc"
16)
17
Jon Perritt8aa40262014-09-29 15:41:32 -050018var metadata = map[string]string{"Gophercloud-Test": "objects"}
Jon Perrittf81e17a2014-09-15 01:29:41 -050019
20func serviceClient() *gophercloud.ServiceClient {
21 return &gophercloud.ServiceClient{
22 Provider: &gophercloud.ProviderClient{TokenID: tokenId},
23 Endpoint: testhelper.Endpoint(),
24 }
25}
26
Jon Perritt8c93a302014-09-28 22:35:57 -050027func TestDownloadObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -050028 testhelper.SetupHTTP()
29 defer testhelper.TeardownHTTP()
Jon Perritt8c93a302014-09-28 22:35:57 -050030
Jon Perrittf81e17a2014-09-15 01:29:41 -050031 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 Perritt8c93a302014-09-28 22:35:57 -050035 w.WriteHeader(http.StatusOK)
36 fmt.Fprintf(w, "Successful download with Gophercloud")
Jon Perrittf81e17a2014-09-15 01:29:41 -050037 })
38
39 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -050040 content, err := Download(client, "testContainer", "testObject", nil).ExtractContent()
Jon Perrittf81e17a2014-09-15 01:29:41 -050041 if err != nil {
42 t.Fatalf("Unexpected error downloading object: %v", err)
43 }
Jon Perritt8c93a302014-09-28 22:35:57 -050044 if string(content) != "Successful download with Gophercloud" {
45 t.Errorf("Expected %s, got %s", "Successful download with Gophercloud", content)
46 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050047}
48
49func 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 Perrittde47eac2014-09-30 15:34:17 -050056 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8c93a302014-09-28 22:35:57 -050057
Jon Perritt83c116b2014-09-30 16:30:24 -050058 w.Header().Set("Content-Type", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -050059 r.ParseForm()
60 marker := r.Form.Get("marker")
61 switch marker {
62 case "":
Jon Perrittde47eac2014-09-30 15:34:17 -050063 fmt.Fprintf(w, `[
64 {
65 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
66 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
67 "bytes": 14,
Jon Perrittf3171c12014-09-30 17:39:31 -050068 "name": "goodbye",
Jon Perrittde47eac2014-09-30 15:34:17 -050069 "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 Perritt8aa40262014-09-29 15:41:32 -050081 default:
82 t.Fatalf("Unexpected marker: [%s]", marker)
83 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050084 })
85
86 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -050087 count := 0
Jon Perrittf3171c12014-09-30 17:39:31 -050088 err := List(client, "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050089 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050090 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 Perritt8aa40262014-09-29 15:41:32 -050098 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
Jon Perrittfdac6e52014-09-29 19:43:45 -050099 LastModified: "2009-11-10 23:00:00 +0000 UTC",
Jon Perritt8aa40262014-09-29 15:41:32 -0500100 Bytes: 14,
101 Name: "goodbye",
102 ContentType: "application/octet-stream",
Jon Perritt8c93a302014-09-28 22:35:57 -0500103 },
Jon Perrittde47eac2014-09-30 15:34:17 -0500104 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 Perritt8c93a302014-09-28 22:35:57 -0500111 }
112
Jon Perrittf3171c12014-09-30 17:39:31 -0500113 testhelper.CheckDeepEquals(t, actual, expected)
Jon Perritt8c93a302014-09-28 22:35:57 -0500114
115 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500116 })
Jon Perrittf3171c12014-09-30 17:39:31 -0500117 if err != nil {
118 t.Error(err)
119 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500120
121 if count != 1 {
122 t.Errorf("Expected 1 page, got %d", count)
123 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500124}
125
126func TestListObjectNames(t *testing.T) {
127 testhelper.SetupHTTP()
128 defer testhelper.TeardownHTTP()
129
130 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
131 testhelper.TestMethod(t, r, "GET")
132 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
133 testhelper.TestHeader(t, r, "Accept", "text/plain")
Jon Perritt8c93a302014-09-28 22:35:57 -0500134
Jon Perritt83c116b2014-09-30 16:30:24 -0500135 w.Header().Set("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500136 r.ParseForm()
137 marker := r.Form.Get("marker")
138 switch marker {
139 case "":
Jon Perrittde47eac2014-09-30 15:34:17 -0500140 fmt.Fprintf(w, "hello\ngoodbye\n")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500141 case "goodbye":
142 fmt.Fprintf(w, "")
143 default:
144 t.Fatalf("Unexpected marker: [%s]", marker)
145 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500146 })
147
148 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500149 count := 0
Jon Perritt83c116b2014-09-30 16:30:24 -0500150 List(client, "testContainer", &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -0500151 count++
Jon Perritt8c93a302014-09-28 22:35:57 -0500152 actual, err := ExtractNames(page)
153 if err != nil {
154 t.Errorf("Failed to extract object names: %v", err)
155 return false, err
156 }
157
Jon Perrittde47eac2014-09-30 15:34:17 -0500158 expected := []string{"hello", "goodbye"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500159
160 testhelper.CheckDeepEquals(t, expected, actual)
161
162 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500163 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500164
165 if count != 1 {
166 t.Errorf("Expected 1 page, got %d", count)
167 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500168}
Jon Perritt8aa40262014-09-29 15:41:32 -0500169
Jon Perrittf81e17a2014-09-15 01:29:41 -0500170func TestCreateObject(t *testing.T) {
171 testhelper.SetupHTTP()
172 defer testhelper.TeardownHTTP()
173
174 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
175 testhelper.TestMethod(t, r, "PUT")
176 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
177 testhelper.TestHeader(t, r, "Accept", "application/json")
178 w.WriteHeader(http.StatusCreated)
179 })
180
181 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500182 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jon Perrittde47eac2014-09-30 15:34:17 -0500183 err := Create(client, "testContainer", "testObject", content, nil)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500184 if err != nil {
185 t.Fatalf("Unexpected error creating object: %v", err)
186 }
187}
188
189func TestCopyObject(t *testing.T) {
190 testhelper.SetupHTTP()
191 defer testhelper.TeardownHTTP()
192
193 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
194 testhelper.TestMethod(t, r, "COPY")
195 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
196 testhelper.TestHeader(t, r, "Accept", "application/json")
197 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
198 w.WriteHeader(http.StatusCreated)
199 })
200
201 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500202 err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500203 if err != nil {
204 t.Fatalf("Unexpected error copying object: %v", err)
205 }
206}
207
208func TestDeleteObject(t *testing.T) {
209 testhelper.SetupHTTP()
210 defer testhelper.TeardownHTTP()
211
212 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
213 testhelper.TestMethod(t, r, "DELETE")
214 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
215 testhelper.TestHeader(t, r, "Accept", "application/json")
216 w.WriteHeader(http.StatusNoContent)
217 })
218
219 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500220 err := Delete(client, "testContainer", "testObject", nil)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500221 if err != nil {
222 t.Fatalf("Unexpected error deleting object: %v", err)
223 }
224}
225
226func TestUpateObjectMetadata(t *testing.T) {
227 testhelper.SetupHTTP()
228 defer testhelper.TeardownHTTP()
229
230 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
231 testhelper.TestMethod(t, r, "POST")
232 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
233 testhelper.TestHeader(t, r, "Accept", "application/json")
234 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
235 w.WriteHeader(http.StatusAccepted)
236 })
237
238 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500239 err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500240 if err != nil {
241 t.Fatalf("Unexpected error updating object metadata: %v", err)
242 }
243}
244
Jon Perritt8c93a302014-09-28 22:35:57 -0500245func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500246 testhelper.SetupHTTP()
247 defer testhelper.TeardownHTTP()
248
249 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
250 testhelper.TestMethod(t, r, "HEAD")
251 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
252 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500253 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500254 w.WriteHeader(http.StatusNoContent)
255 })
256
257 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500258 expected := metadata
Jon Perrittde47eac2014-09-30 15:34:17 -0500259 actual, err := Get(client, "testContainer", "testObject", nil).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500260 if err != nil {
261 t.Fatalf("Unexpected error getting object metadata: %v", err)
262 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500263 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500264}