blob: 089081fe316b81996e7ae311e28b19b1f3ab07ab [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
Jon Perritt8c93a302014-09-28 22:35:57 -05009 "github.com/rackspace/gophercloud/pagination"
Jon Perrittf81e17a2014-09-15 01:29:41 -050010 "github.com/rackspace/gophercloud/testhelper"
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020011 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittf81e17a2014-09-15 01:29:41 -050012)
13
Jon Perritt8aa40262014-09-29 15:41:32 -050014var metadata = map[string]string{"Gophercloud-Test": "objects"}
Jon Perrittf81e17a2014-09-15 01:29:41 -050015
Jon Perritt8c93a302014-09-28 22:35:57 -050016func TestDownloadObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -050017 testhelper.SetupHTTP()
18 defer testhelper.TeardownHTTP()
Jon Perritt8c93a302014-09-28 22:35:57 -050019
Jon Perrittf81e17a2014-09-15 01:29:41 -050020 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
21 testhelper.TestMethod(t, r, "GET")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020022 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -050023 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8c93a302014-09-28 22:35:57 -050024 w.WriteHeader(http.StatusOK)
25 fmt.Fprintf(w, "Successful download with Gophercloud")
Jon Perrittf81e17a2014-09-15 01:29:41 -050026 })
27
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020028 content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
Jon Perrittf81e17a2014-09-15 01:29:41 -050029 if err != nil {
30 t.Fatalf("Unexpected error downloading object: %v", err)
31 }
Jon Perritt8c93a302014-09-28 22:35:57 -050032 if string(content) != "Successful download with Gophercloud" {
33 t.Errorf("Expected %s, got %s", "Successful download with Gophercloud", content)
34 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050035}
36
37func TestListObjectInfo(t *testing.T) {
38 testhelper.SetupHTTP()
39 defer testhelper.TeardownHTTP()
40
41 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
42 testhelper.TestMethod(t, r, "GET")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020043 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittde47eac2014-09-30 15:34:17 -050044 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8c93a302014-09-28 22:35:57 -050045
Jon Perritt83c116b2014-09-30 16:30:24 -050046 w.Header().Set("Content-Type", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -050047 r.ParseForm()
48 marker := r.Form.Get("marker")
49 switch marker {
50 case "":
Jon Perrittde47eac2014-09-30 15:34:17 -050051 fmt.Fprintf(w, `[
52 {
53 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
54 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
55 "bytes": 14,
Jon Perrittf3171c12014-09-30 17:39:31 -050056 "name": "goodbye",
Jon Perrittde47eac2014-09-30 15:34:17 -050057 "content_type": "application/octet-stream"
58 },
59 {
60 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
61 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
62 "bytes": 14,
63 "name": "hello",
64 "content_type": "application/octet-stream"
65 }
66 ]`)
67 case "hello":
68 fmt.Fprintf(w, `[]`)
Jon Perritt8aa40262014-09-29 15:41:32 -050069 default:
70 t.Fatalf("Unexpected marker: [%s]", marker)
71 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050072 })
73
Jon Perrittde47eac2014-09-30 15:34:17 -050074 count := 0
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +020075
76 err := List(fake.ServiceClient(), "testContainer", &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050077 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050078 actual, err := ExtractInfo(page)
79 if err != nil {
80 t.Errorf("Failed to extract object info: %v", err)
81 return false, err
82 }
83
84 expected := []Object{
85 Object{
Jon Perritt8aa40262014-09-29 15:41:32 -050086 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
Jon Perrittfdac6e52014-09-29 19:43:45 -050087 LastModified: "2009-11-10 23:00:00 +0000 UTC",
Jon Perritt8aa40262014-09-29 15:41:32 -050088 Bytes: 14,
89 Name: "goodbye",
90 ContentType: "application/octet-stream",
Jon Perritt8c93a302014-09-28 22:35:57 -050091 },
Jon Perrittde47eac2014-09-30 15:34:17 -050092 Object{
93 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
94 LastModified: "2009-11-10 23:00:00 +0000 UTC",
95 Bytes: 14,
96 Name: "hello",
97 ContentType: "application/octet-stream",
98 },
Jon Perritt8c93a302014-09-28 22:35:57 -050099 }
100
Jon Perrittf3171c12014-09-30 17:39:31 -0500101 testhelper.CheckDeepEquals(t, actual, expected)
Jon Perritt8c93a302014-09-28 22:35:57 -0500102
103 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500104 })
Jon Perrittf3171c12014-09-30 17:39:31 -0500105 if err != nil {
106 t.Error(err)
107 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500108
109 if count != 1 {
110 t.Errorf("Expected 1 page, got %d", count)
111 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500112}
113
114func TestListObjectNames(t *testing.T) {
115 testhelper.SetupHTTP()
116 defer testhelper.TeardownHTTP()
117
118 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
119 testhelper.TestMethod(t, r, "GET")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200120 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500121 testhelper.TestHeader(t, r, "Accept", "text/plain")
Jon Perritt8c93a302014-09-28 22:35:57 -0500122
Jon Perritt83c116b2014-09-30 16:30:24 -0500123 w.Header().Set("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500124 r.ParseForm()
125 marker := r.Form.Get("marker")
126 switch marker {
127 case "":
Jon Perrittde47eac2014-09-30 15:34:17 -0500128 fmt.Fprintf(w, "hello\ngoodbye\n")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500129 case "goodbye":
130 fmt.Fprintf(w, "")
131 default:
132 t.Fatalf("Unexpected marker: [%s]", marker)
133 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500134 })
135
Jon Perrittde47eac2014-09-30 15:34:17 -0500136 count := 0
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200137 List(fake.ServiceClient(), "testContainer", &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -0500138 count++
Jon Perritt8c93a302014-09-28 22:35:57 -0500139 actual, err := ExtractNames(page)
140 if err != nil {
141 t.Errorf("Failed to extract object names: %v", err)
142 return false, err
143 }
144
Jon Perrittde47eac2014-09-30 15:34:17 -0500145 expected := []string{"hello", "goodbye"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500146
147 testhelper.CheckDeepEquals(t, expected, actual)
148
149 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500150 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500151
152 if count != 1 {
153 t.Errorf("Expected 1 page, got %d", count)
154 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500155}
Jon Perritt8aa40262014-09-29 15:41:32 -0500156
Jon Perrittf81e17a2014-09-15 01:29:41 -0500157func TestCreateObject(t *testing.T) {
158 testhelper.SetupHTTP()
159 defer testhelper.TeardownHTTP()
160
161 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
162 testhelper.TestMethod(t, r, "PUT")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200163 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500164 testhelper.TestHeader(t, r, "Accept", "application/json")
165 w.WriteHeader(http.StatusCreated)
166 })
167
Jon Perritt8c93a302014-09-28 22:35:57 -0500168 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200169 _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, nil).ExtractHeaders()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500170 if err != nil {
171 t.Fatalf("Unexpected error creating object: %v", err)
172 }
173}
174
175func TestCopyObject(t *testing.T) {
176 testhelper.SetupHTTP()
177 defer testhelper.TeardownHTTP()
178
179 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
180 testhelper.TestMethod(t, r, "COPY")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200181 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500182 testhelper.TestHeader(t, r, "Accept", "application/json")
183 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
184 w.WriteHeader(http.StatusCreated)
185 })
186
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200187 _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"}).ExtractHeaders()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500188 if err != nil {
189 t.Fatalf("Unexpected error copying object: %v", err)
190 }
191}
192
193func TestDeleteObject(t *testing.T) {
194 testhelper.SetupHTTP()
195 defer testhelper.TeardownHTTP()
196
197 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
198 testhelper.TestMethod(t, r, "DELETE")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200199 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500200 testhelper.TestHeader(t, r, "Accept", "application/json")
201 w.WriteHeader(http.StatusNoContent)
202 })
203
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200204 _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractHeaders()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500205 if err != nil {
206 t.Fatalf("Unexpected error deleting object: %v", err)
207 }
208}
209
210func TestUpateObjectMetadata(t *testing.T) {
211 testhelper.SetupHTTP()
212 defer testhelper.TeardownHTTP()
213
214 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
215 testhelper.TestMethod(t, r, "POST")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200216 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500217 testhelper.TestHeader(t, r, "Accept", "application/json")
218 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
219 w.WriteHeader(http.StatusAccepted)
220 })
221
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200222 _, err := Update(fake.ServiceClient(), "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).ExtractHeaders()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500223 if err != nil {
224 t.Fatalf("Unexpected error updating object metadata: %v", err)
225 }
226}
227
Jon Perritt8c93a302014-09-28 22:35:57 -0500228func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500229 testhelper.SetupHTTP()
230 defer testhelper.TeardownHTTP()
231
232 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
233 testhelper.TestMethod(t, r, "HEAD")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200234 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500235 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500236 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500237 w.WriteHeader(http.StatusNoContent)
238 })
239
Jon Perritt8c93a302014-09-28 22:35:57 -0500240 expected := metadata
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200241 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500242 if err != nil {
243 t.Fatalf("Unexpected error getting object metadata: %v", err)
244 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500245 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500246}