blob: 6f61a98b4bfe791330233c635bcd9c36ee5bff5f [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,
68 "name": 'goodbye",
69 "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
88 List(client, "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
89 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
113 testhelper.CheckDeepEquals(t, expected, actual)
114
115 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500116 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500117
118 if count != 1 {
119 t.Errorf("Expected 1 page, got %d", count)
120 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500121}
122
123func TestListObjectNames(t *testing.T) {
124 testhelper.SetupHTTP()
125 defer testhelper.TeardownHTTP()
126
127 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
128 testhelper.TestMethod(t, r, "GET")
129 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
130 testhelper.TestHeader(t, r, "Accept", "text/plain")
Jon Perritt8c93a302014-09-28 22:35:57 -0500131
Jon Perritt83c116b2014-09-30 16:30:24 -0500132 w.Header().Set("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500133 r.ParseForm()
134 marker := r.Form.Get("marker")
135 switch marker {
136 case "":
Jon Perrittde47eac2014-09-30 15:34:17 -0500137 fmt.Fprintf(w, "hello\ngoodbye\n")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500138 case "goodbye":
139 fmt.Fprintf(w, "")
140 default:
141 t.Fatalf("Unexpected marker: [%s]", marker)
142 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500143 })
144
145 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500146 count := 0
Jon Perritt83c116b2014-09-30 16:30:24 -0500147 List(client, "testContainer", &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -0500148 count++
Jon Perritt8c93a302014-09-28 22:35:57 -0500149 actual, err := ExtractNames(page)
150 if err != nil {
151 t.Errorf("Failed to extract object names: %v", err)
152 return false, err
153 }
154
Jon Perrittde47eac2014-09-30 15:34:17 -0500155 expected := []string{"hello", "goodbye"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500156
157 testhelper.CheckDeepEquals(t, expected, actual)
158
159 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500160 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500161
162 if count != 1 {
163 t.Errorf("Expected 1 page, got %d", count)
164 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500165}
Jon Perritt8aa40262014-09-29 15:41:32 -0500166
Jon Perrittf81e17a2014-09-15 01:29:41 -0500167func TestCreateObject(t *testing.T) {
168 testhelper.SetupHTTP()
169 defer testhelper.TeardownHTTP()
170
171 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
172 testhelper.TestMethod(t, r, "PUT")
173 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
174 testhelper.TestHeader(t, r, "Accept", "application/json")
175 w.WriteHeader(http.StatusCreated)
176 })
177
178 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500179 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jon Perrittde47eac2014-09-30 15:34:17 -0500180 err := Create(client, "testContainer", "testObject", content, nil)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500181 if err != nil {
182 t.Fatalf("Unexpected error creating object: %v", err)
183 }
184}
185
186func TestCopyObject(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, "COPY")
192 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
193 testhelper.TestHeader(t, r, "Accept", "application/json")
194 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
195 w.WriteHeader(http.StatusCreated)
196 })
197
198 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500199 err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500200 if err != nil {
201 t.Fatalf("Unexpected error copying object: %v", err)
202 }
203}
204
205func TestDeleteObject(t *testing.T) {
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, "DELETE")
211 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
212 testhelper.TestHeader(t, r, "Accept", "application/json")
213 w.WriteHeader(http.StatusNoContent)
214 })
215
216 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500217 err := Delete(client, "testContainer", "testObject", nil)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500218 if err != nil {
219 t.Fatalf("Unexpected error deleting object: %v", err)
220 }
221}
222
223func TestUpateObjectMetadata(t *testing.T) {
224 testhelper.SetupHTTP()
225 defer testhelper.TeardownHTTP()
226
227 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
228 testhelper.TestMethod(t, r, "POST")
229 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
230 testhelper.TestHeader(t, r, "Accept", "application/json")
231 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
232 w.WriteHeader(http.StatusAccepted)
233 })
234
235 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500236 err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500237 if err != nil {
238 t.Fatalf("Unexpected error updating object metadata: %v", err)
239 }
240}
241
Jon Perritt8c93a302014-09-28 22:35:57 -0500242func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500243 testhelper.SetupHTTP()
244 defer testhelper.TeardownHTTP()
245
246 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
247 testhelper.TestMethod(t, r, "HEAD")
248 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
249 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500250 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500251 w.WriteHeader(http.StatusNoContent)
252 })
253
254 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500255 expected := metadata
Jon Perrittde47eac2014-09-30 15:34:17 -0500256 actual, err := Get(client, "testContainer", "testObject", nil).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500257 if err != nil {
258 t.Fatalf("Unexpected error getting object metadata: %v", err)
259 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500260 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500261}