blob: 0c61550d920c2ba684dbc3075715918b1bf2e7c5 [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 Perritt8c93a302014-09-28 22:35:57 -050040 content, err := Download(client, "testContainer", "testObject", DownloadOpts{}).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 Perritt8c93a302014-09-28 22:35:57 -050056
57 w.Header().Set("Content-Type", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -050058 r.ParseForm()
59 marker := r.Form.Get("marker")
60 switch marker {
61 case "":
62 fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2009-11-10 23:00:00 +0000 UTC','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`)
63 default:
64 t.Fatalf("Unexpected marker: [%s]", marker)
65 }
Jon Perrittf81e17a2014-09-15 01:29:41 -050066 })
67
68 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050069 List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050070 actual, err := ExtractInfo(page)
71 if err != nil {
72 t.Errorf("Failed to extract object info: %v", err)
73 return false, err
74 }
75
76 expected := []Object{
77 Object{
Jon Perritt8aa40262014-09-29 15:41:32 -050078 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
Jon Perrittfdac6e52014-09-29 19:43:45 -050079 LastModified: "2009-11-10 23:00:00 +0000 UTC",
Jon Perritt8aa40262014-09-29 15:41:32 -050080 Bytes: 14,
81 Name: "goodbye",
82 ContentType: "application/octet-stream",
Jon Perritt8c93a302014-09-28 22:35:57 -050083 },
84 }
85
86 testhelper.CheckDeepEquals(t, expected, actual)
87
88 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050089 })
Jon Perrittf81e17a2014-09-15 01:29:41 -050090}
91
92func TestListObjectNames(t *testing.T) {
93 testhelper.SetupHTTP()
94 defer testhelper.TeardownHTTP()
95
96 testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
97 testhelper.TestMethod(t, r, "GET")
98 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
99 testhelper.TestHeader(t, r, "Accept", "text/plain")
Jon Perritt8c93a302014-09-28 22:35:57 -0500100
101 w.Header().Add("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500102 r.ParseForm()
103 marker := r.Form.Get("marker")
104 switch marker {
105 case "":
106 fmt.Fprintf(w, "goodbye\n")
107 case "goodbye":
108 fmt.Fprintf(w, "")
109 default:
110 t.Fatalf("Unexpected marker: [%s]", marker)
111 }
Jon Perrittf81e17a2014-09-15 01:29:41 -0500112 })
113
114 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500115 List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -0500116 actual, err := ExtractNames(page)
117 if err != nil {
118 t.Errorf("Failed to extract object names: %v", err)
119 return false, err
120 }
121
Jon Perrittfdac6e52014-09-29 19:43:45 -0500122 expected := []string{"goodbye"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500123
124 testhelper.CheckDeepEquals(t, expected, actual)
125
126 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500127 })
Jon Perrittf81e17a2014-09-15 01:29:41 -0500128}
Jon Perritt8aa40262014-09-29 15:41:32 -0500129
Jon Perrittf81e17a2014-09-15 01:29:41 -0500130func TestCreateObject(t *testing.T) {
131 testhelper.SetupHTTP()
132 defer testhelper.TeardownHTTP()
133
134 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
135 testhelper.TestMethod(t, r, "PUT")
136 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
137 testhelper.TestHeader(t, r, "Accept", "application/json")
138 w.WriteHeader(http.StatusCreated)
139 })
140
141 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500142 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
143 err := Create(client, "testContainer", "testObject", content, CreateOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500144 if err != nil {
145 t.Fatalf("Unexpected error creating object: %v", err)
146 }
147}
148
149func TestCopyObject(t *testing.T) {
150 testhelper.SetupHTTP()
151 defer testhelper.TeardownHTTP()
152
153 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
154 testhelper.TestMethod(t, r, "COPY")
155 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
156 testhelper.TestHeader(t, r, "Accept", "application/json")
157 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
158 w.WriteHeader(http.StatusCreated)
159 })
160
161 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500162 err := Copy(client, "testContainer", "testObject", CopyOpts{Destination: "/newTestContainer/newTestObject"})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500163 if err != nil {
164 t.Fatalf("Unexpected error copying object: %v", err)
165 }
166}
167
168func TestDeleteObject(t *testing.T) {
169 testhelper.SetupHTTP()
170 defer testhelper.TeardownHTTP()
171
172 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
173 testhelper.TestMethod(t, r, "DELETE")
174 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
175 testhelper.TestHeader(t, r, "Accept", "application/json")
176 w.WriteHeader(http.StatusNoContent)
177 })
178
179 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500180 err := Delete(client, "testContainer", "testObject", DeleteOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500181 if err != nil {
182 t.Fatalf("Unexpected error deleting object: %v", err)
183 }
184}
185
186func TestUpateObjectMetadata(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, "POST")
192 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
193 testhelper.TestHeader(t, r, "Accept", "application/json")
194 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
195 w.WriteHeader(http.StatusAccepted)
196 })
197
198 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500199 err := Update(client, "testContainer", "testObject", UpdateOpts{Metadata: metadata})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500200 if err != nil {
201 t.Fatalf("Unexpected error updating object metadata: %v", err)
202 }
203}
204
Jon Perritt8c93a302014-09-28 22:35:57 -0500205func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500206 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, "HEAD")
211 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
212 testhelper.TestHeader(t, r, "Accept", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -0500213 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500214 w.WriteHeader(http.StatusNoContent)
215 })
216
217 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500218 expected := metadata
219 actual, err := Get(client, "testContainer", "testObject", GetOpts{}).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500220 if err != nil {
221 t.Fatalf("Unexpected error getting object metadata: %v", err)
222 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500223 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500224}