blob: 714778248a1ca726bc52af862b12308b0e54cd4d [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")
Jon Perrittd3080ee2014-10-13 21:08:55 -0500169 options := &CreateOpts{ContentType: "application/json"}
Ash Wilsonaf262872014-10-20 09:32:29 -0400170 _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).Extract()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500171 if err != nil {
172 t.Fatalf("Unexpected error creating object: %v", err)
173 }
174}
175
176func TestCopyObject(t *testing.T) {
177 testhelper.SetupHTTP()
178 defer testhelper.TeardownHTTP()
179
180 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
181 testhelper.TestMethod(t, r, "COPY")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200182 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500183 testhelper.TestHeader(t, r, "Accept", "application/json")
184 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
185 w.WriteHeader(http.StatusCreated)
186 })
187
Jon Perrittd3080ee2014-10-13 21:08:55 -0500188 options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
Ash Wilsonaf262872014-10-20 09:32:29 -0400189 _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).Extract()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500190 if err != nil {
191 t.Fatalf("Unexpected error copying object: %v", err)
192 }
193}
194
195func TestDeleteObject(t *testing.T) {
196 testhelper.SetupHTTP()
197 defer testhelper.TeardownHTTP()
198
199 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
200 testhelper.TestMethod(t, r, "DELETE")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200201 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500202 testhelper.TestHeader(t, r, "Accept", "application/json")
203 w.WriteHeader(http.StatusNoContent)
204 })
205
Ash Wilsonaf262872014-10-20 09:32:29 -0400206 _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).Extract()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500207 if err != nil {
208 t.Fatalf("Unexpected error deleting object: %v", err)
209 }
210}
211
212func TestUpateObjectMetadata(t *testing.T) {
213 testhelper.SetupHTTP()
214 defer testhelper.TeardownHTTP()
215
216 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
217 testhelper.TestMethod(t, r, "POST")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200218 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500219 testhelper.TestHeader(t, r, "Accept", "application/json")
220 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
221 w.WriteHeader(http.StatusAccepted)
222 })
223
Ash Wilsonaf262872014-10-20 09:32:29 -0400224 _, err := Update(fake.ServiceClient(), "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).Extract()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500225 if err != nil {
226 t.Fatalf("Unexpected error updating object metadata: %v", err)
227 }
228}
229
Jon Perritt8c93a302014-09-28 22:35:57 -0500230func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500231 testhelper.SetupHTTP()
232 defer testhelper.TeardownHTTP()
233
234 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
235 testhelper.TestMethod(t, r, "HEAD")
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200236 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500237 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500238 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500239 w.WriteHeader(http.StatusNoContent)
240 })
241
Jon Perritt8c93a302014-09-28 22:35:57 -0500242 expected := metadata
Jamie Hannafordc9cdc8f2014-10-06 16:32:56 +0200243 actual, err := Get(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500244 if err != nil {
245 t.Fatalf("Unexpected error getting object metadata: %v", err)
246 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500247 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500248}