blob: 6fe4d30e16c489ffe0795a3d160e6748b52e5515 [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"
Jon Perritt8aa40262014-09-29 15:41:32 -05008 "time"
Jon Perrittf81e17a2014-09-15 01:29:41 -05009
10 "github.com/rackspace/gophercloud"
Jon Perritt8c93a302014-09-28 22:35:57 -050011 "github.com/rackspace/gophercloud/pagination"
Jon Perrittf81e17a2014-09-15 01:29:41 -050012 "github.com/rackspace/gophercloud/testhelper"
13)
14
Jon Perritt8c93a302014-09-28 22:35:57 -050015const (
Jon Perrittf81e17a2014-09-15 01:29:41 -050016 tokenId = "abcabcabcabc"
17)
18
Jon Perritt8aa40262014-09-29 15:41:32 -050019var metadata = map[string]string{"Gophercloud-Test": "objects"}
Jon Perrittf81e17a2014-09-15 01:29:41 -050020
21func serviceClient() *gophercloud.ServiceClient {
22 return &gophercloud.ServiceClient{
23 Provider: &gophercloud.ProviderClient{TokenID: tokenId},
24 Endpoint: testhelper.Endpoint(),
25 }
26}
27
Jon Perritt8c93a302014-09-28 22:35:57 -050028func TestDownloadObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -050029 testhelper.SetupHTTP()
30 defer testhelper.TeardownHTTP()
Jon Perritt8c93a302014-09-28 22:35:57 -050031
Jon Perrittf81e17a2014-09-15 01:29:41 -050032 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
33 testhelper.TestMethod(t, r, "GET")
34 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
35 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8c93a302014-09-28 22:35:57 -050036 w.WriteHeader(http.StatusOK)
37 fmt.Fprintf(w, "Successful download with Gophercloud")
Jon Perrittf81e17a2014-09-15 01:29:41 -050038 })
39
40 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050041 content, err := Download(client, "testContainer", "testObject", DownloadOpts{}).ExtractContent()
Jon Perrittf81e17a2014-09-15 01:29:41 -050042 if err != nil {
43 t.Fatalf("Unexpected error downloading object: %v", err)
44 }
Jon Perritt8c93a302014-09-28 22:35:57 -050045 if string(content) != "Successful download with Gophercloud" {
46 t.Errorf("Expected %s, got %s", "Successful download with Gophercloud", content)
47 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050048}
49
50func TestListObjectInfo(t *testing.T) {
51 testhelper.SetupHTTP()
52 defer testhelper.TeardownHTTP()
53
54 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
55 testhelper.TestMethod(t, r, "GET")
56 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
Jon Perritt8c93a302014-09-28 22:35:57 -050057
58 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 "":
63 fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2009-11-10 23:00:00 +0000 UTC','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`)
64 default:
65 t.Fatalf("Unexpected marker: [%s]", marker)
66 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050067 })
68
69 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050070 List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050071 actual, err := ExtractInfo(page)
72 if err != nil {
73 t.Errorf("Failed to extract object info: %v", err)
74 return false, err
75 }
76
77 expected := []Object{
78 Object{
Jon Perritt8aa40262014-09-29 15:41:32 -050079 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
80 LastModified: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
81 Bytes: 14,
82 Name: "goodbye",
83 ContentType: "application/octet-stream",
Jon Perritt8c93a302014-09-28 22:35:57 -050084 },
85 }
86
87 testhelper.CheckDeepEquals(t, expected, actual)
88
89 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050090 })
Jon Perrittf81e17a2014-09-15 01:29:41 -050091}
92
93func TestListObjectNames(t *testing.T) {
94 testhelper.SetupHTTP()
95 defer testhelper.TeardownHTTP()
96
97 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
98 testhelper.TestMethod(t, r, "GET")
99 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
100 testhelper.TestHeader(t, r, "Accept", "text/plain")
Jon Perritt8c93a302014-09-28 22:35:57 -0500101
102 w.Header().Add("Content-Type", "text/plain")
103 w.WriteHeader(http.StatusOK)
104 fmt.Fprintf(w, "")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500105 })
106
107 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500108 List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -0500109 actual, err := ExtractNames(page)
110 if err != nil {
111 t.Errorf("Failed to extract object names: %v", err)
112 return false, err
113 }
114
115 expected := []string{"helloworld", "goodbye"}
116
117 testhelper.CheckDeepEquals(t, expected, actual)
118
119 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500120 })
Jon Perrittf81e17a2014-09-15 01:29:41 -0500121}
Jon Perritt8aa40262014-09-29 15:41:32 -0500122
Jon Perrittf81e17a2014-09-15 01:29:41 -0500123func TestCreateObject(t *testing.T) {
124 testhelper.SetupHTTP()
125 defer testhelper.TeardownHTTP()
126
127 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
128 testhelper.TestMethod(t, r, "PUT")
129 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
130 testhelper.TestHeader(t, r, "Accept", "application/json")
131 w.WriteHeader(http.StatusCreated)
132 })
133
134 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500135 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
136 err := Create(client, "testContainer", "testObject", content, CreateOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500137 if err != nil {
138 t.Fatalf("Unexpected error creating object: %v", err)
139 }
140}
141
142func TestCopyObject(t *testing.T) {
143 testhelper.SetupHTTP()
144 defer testhelper.TeardownHTTP()
145
146 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
147 testhelper.TestMethod(t, r, "COPY")
148 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
149 testhelper.TestHeader(t, r, "Accept", "application/json")
150 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
151 w.WriteHeader(http.StatusCreated)
152 })
153
154 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500155 err := Copy(client, "testContainer", "testObject", CopyOpts{Destination: "/newTestContainer/newTestObject"})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500156 if err != nil {
157 t.Fatalf("Unexpected error copying object: %v", err)
158 }
159}
160
161func TestDeleteObject(t *testing.T) {
162 testhelper.SetupHTTP()
163 defer testhelper.TeardownHTTP()
164
165 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
166 testhelper.TestMethod(t, r, "DELETE")
167 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
168 testhelper.TestHeader(t, r, "Accept", "application/json")
169 w.WriteHeader(http.StatusNoContent)
170 })
171
172 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500173 err := Delete(client, "testContainer", "testObject", DeleteOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500174 if err != nil {
175 t.Fatalf("Unexpected error deleting object: %v", err)
176 }
177}
178
179func TestUpateObjectMetadata(t *testing.T) {
180 testhelper.SetupHTTP()
181 defer testhelper.TeardownHTTP()
182
183 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
184 testhelper.TestMethod(t, r, "POST")
185 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
186 testhelper.TestHeader(t, r, "Accept", "application/json")
187 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
188 w.WriteHeader(http.StatusAccepted)
189 })
190
191 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500192 err := Update(client, "testContainer", "testObject", UpdateOpts{Metadata: metadata})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500193 if err != nil {
194 t.Fatalf("Unexpected error updating object metadata: %v", err)
195 }
196}
197
Jon Perritt8c93a302014-09-28 22:35:57 -0500198func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500199 testhelper.SetupHTTP()
200 defer testhelper.TeardownHTTP()
201
202 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
203 testhelper.TestMethod(t, r, "HEAD")
204 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
205 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500206 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500207 w.WriteHeader(http.StatusNoContent)
208 })
209
210 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500211 expected := metadata
212 actual, err := Get(client, "testContainer", "testObject", GetOpts{}).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500213 if err != nil {
214 t.Fatalf("Unexpected error getting object metadata: %v", err)
215 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500216 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500217}