blob: c3047c6c4c380d87c2c8695910d561dcdaa394db [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
18var metadata = map[string]string{"gophercloud-test": "objects"}
19
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")
58 w.WriteHeader(http.StatusOK)
59 fmt.Fprintf(w, `[{'hash': '451e372e48e0f6b1114fa0724aa79fa1','last_modified': '2014-01-15T16:41:49.390270','bytes': 14,'name': 'goodbye','content_type': 'application/octet-stream'}]`)
Jon Perrittf81e17a2014-09-15 01:29:41 -050060 })
61
62 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050063 count := 0
64 List(client, "testContainer", ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
65 count++
66 actual, err := ExtractInfo(page)
67 if err != nil {
68 t.Errorf("Failed to extract object info: %v", err)
69 return false, err
70 }
71
72 expected := []Object{
73 Object{
74 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
75 "last_modified": "2014-01-15T16:41:49.390270",
76 "bytes": 14,
77 "name": "goodbye",
78 "content_type": "application/octet-stream",
79 },
80 }
81
82 testhelper.CheckDeepEquals(t, expected, actual)
83
84 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -050085 })
Jon Perritt8c93a302014-09-28 22:35:57 -050086
87 if count != 1 {
88 t.Errorf("Expected 1 page, got %d", count)
Jon Perrittf81e17a2014-09-15 01:29:41 -050089 }
90}
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")
102 w.WriteHeader(http.StatusOK)
103 fmt.Fprintf(w, "")
Jon Perrittf81e17a2014-09-15 01:29:41 -0500104 })
105
106 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500107 count := 0
108 List(client, "testContainer", ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
109 count++
110 actual, err := ExtractNames(page)
111 if err != nil {
112 t.Errorf("Failed to extract object names: %v", err)
113 return false, err
114 }
115
116 expected := []string{"helloworld", "goodbye"}
117
118 testhelper.CheckDeepEquals(t, expected, actual)
119
120 return true, nil
Jon Perrittf81e17a2014-09-15 01:29:41 -0500121 })
Jon Perritt8c93a302014-09-28 22:35:57 -0500122
123 if count != 0 {
124 t.Fatalf("Expected 0 pages, got %d", count)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500125 }
126}
Jon Perrittf81e17a2014-09-15 01:29:41 -0500127func TestCreateObject(t *testing.T) {
128 testhelper.SetupHTTP()
129 defer testhelper.TeardownHTTP()
130
131 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
132 testhelper.TestMethod(t, r, "PUT")
133 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
134 testhelper.TestHeader(t, r, "Accept", "application/json")
135 w.WriteHeader(http.StatusCreated)
136 })
137
138 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500139 content := bytes.NewBufferString("Did gyre and gimble in the wabe")
140 err := Create(client, "testContainer", "testObject", content, CreateOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500141 if err != nil {
142 t.Fatalf("Unexpected error creating object: %v", err)
143 }
144}
145
146func TestCopyObject(t *testing.T) {
147 testhelper.SetupHTTP()
148 defer testhelper.TeardownHTTP()
149
150 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
151 testhelper.TestMethod(t, r, "COPY")
152 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
153 testhelper.TestHeader(t, r, "Accept", "application/json")
154 testhelper.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
155 w.WriteHeader(http.StatusCreated)
156 })
157
158 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500159 err := Copy(client, "testContainer", "testObject", CopyOpts{Destination: "/newTestContainer/newTestObject"})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500160 if err != nil {
161 t.Fatalf("Unexpected error copying object: %v", err)
162 }
163}
164
165func TestDeleteObject(t *testing.T) {
166 testhelper.SetupHTTP()
167 defer testhelper.TeardownHTTP()
168
169 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
170 testhelper.TestMethod(t, r, "DELETE")
171 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
172 testhelper.TestHeader(t, r, "Accept", "application/json")
173 w.WriteHeader(http.StatusNoContent)
174 })
175
176 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500177 err := Delete(client, "testContainer", "testObject", DeleteOpts{})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500178 if err != nil {
179 t.Fatalf("Unexpected error deleting object: %v", err)
180 }
181}
182
183func TestUpateObjectMetadata(t *testing.T) {
184 testhelper.SetupHTTP()
185 defer testhelper.TeardownHTTP()
186
187 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
188 testhelper.TestMethod(t, r, "POST")
189 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
190 testhelper.TestHeader(t, r, "Accept", "application/json")
191 testhelper.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
192 w.WriteHeader(http.StatusAccepted)
193 })
194
195 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500196 err := Update(client, "testContainer", "testObject", UpdateOpts{Metadata: metadata})
Jon Perrittf81e17a2014-09-15 01:29:41 -0500197 if err != nil {
198 t.Fatalf("Unexpected error updating object metadata: %v", err)
199 }
200}
201
Jon Perritt8c93a302014-09-28 22:35:57 -0500202func TestGetObject(t *testing.T) {
Jon Perrittf81e17a2014-09-15 01:29:41 -0500203 testhelper.SetupHTTP()
204 defer testhelper.TeardownHTTP()
205
206 testhelper.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
207 testhelper.TestMethod(t, r, "HEAD")
208 testhelper.TestHeader(t, r, "X-Auth-Token", tokenId)
209 testhelper.TestHeader(t, r, "Accept", "application/json")
210 w.WriteHeader(http.StatusNoContent)
211 })
212
213 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500214 expected := metadata
215 actual, err := Get(client, "testContainer", "testObject", GetOpts{}).ExtractMetadata()
Jon Perrittf81e17a2014-09-15 01:29:41 -0500216 if err != nil {
217 t.Fatalf("Unexpected error getting object metadata: %v", err)
218 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500219 testhelper.CheckDeepEquals(t, expected, actual)
Jon Perrittf81e17a2014-09-15 01:29:41 -0500220}