blob: d3144cb2e31402166c2aa745c3ad8853db91bc68 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jon Perritt457f8ca2014-10-15 00:28:23 -05002
3import (
Jamie Hannaford08096232015-07-13 12:47:28 +02004 "crypto/md5"
Jon Perritt457f8ca2014-10-15 00:28:23 -05005 "fmt"
Jamie Hannaford08096232015-07-13 12:47:28 +02006 "io"
Jon Perritt457f8ca2014-10-15 00:28:23 -05007 "net/http"
8 "testing"
jrperritt655245a2016-08-31 15:30:27 -05009 "time"
Jon Perritt457f8ca2014-10-15 00:28:23 -050010
jrperritt655245a2016-08-31 15:30:27 -050011 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -050012 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
Jon Perritt27249f42016-02-18 10:35:59 -060013 th "github.com/gophercloud/gophercloud/testhelper"
14 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt457f8ca2014-10-15 00:28:23 -050015)
16
jrperritt655245a2016-08-31 15:30:27 -050017var (
18 loc, _ = time.LoadLocation("GMT")
19)
20
Jon Perritt457f8ca2014-10-15 00:28:23 -050021// HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
22// responds with a `Download` response.
23func HandleDownloadObjectSuccessfully(t *testing.T) {
24 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
25 th.TestMethod(t, r, "GET")
26 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
27 th.TestHeader(t, r, "Accept", "application/json")
jrperritt655245a2016-08-31 15:30:27 -050028 w.Header().Set("Date", "Wed, 10 Nov 2009 23:00:00 GMT")
Jon Perritt457f8ca2014-10-15 00:28:23 -050029 w.WriteHeader(http.StatusOK)
30 fmt.Fprintf(w, "Successful download with Gophercloud")
31 })
32}
33
34// ExpectedListInfo is the result expected from a call to `List` when full
35// info is requested.
jrperritt3d966162016-06-06 14:08:54 -050036var ExpectedListInfo = []objects.Object{
37 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050038 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt655245a2016-08-31 15:30:27 -050039 LastModified: gophercloud.JSONRFC1123(time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)), //"2009-11-10 23:00:00 +0000 UTC"
Jon Perritt457f8ca2014-10-15 00:28:23 -050040 Bytes: 14,
41 Name: "goodbye",
42 ContentType: "application/octet-stream",
43 },
jrperritt3d966162016-06-06 14:08:54 -050044 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050045 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt655245a2016-08-31 15:30:27 -050046 LastModified: gophercloud.JSONRFC1123(time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)),
Jon Perritt457f8ca2014-10-15 00:28:23 -050047 Bytes: 14,
48 Name: "hello",
49 ContentType: "application/octet-stream",
50 },
51}
52
53// ExpectedListNames is the result expected from a call to `List` when just
54// object names are requested.
55var ExpectedListNames = []string{"hello", "goodbye"}
56
57// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
58// responds with a `List` response when full info is requested.
59func HandleListObjectsInfoSuccessfully(t *testing.T) {
60 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
61 th.TestMethod(t, r, "GET")
62 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
63 th.TestHeader(t, r, "Accept", "application/json")
64
65 w.Header().Set("Content-Type", "application/json")
66 r.ParseForm()
67 marker := r.Form.Get("marker")
68 switch marker {
69 case "":
70 fmt.Fprintf(w, `[
71 {
72 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt655245a2016-08-31 15:30:27 -050073 "last_modified": "Wed, 10 Nov 2009 23:00:00 GMT",
Jon Perritt457f8ca2014-10-15 00:28:23 -050074 "bytes": 14,
75 "name": "goodbye",
76 "content_type": "application/octet-stream"
77 },
78 {
79 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt655245a2016-08-31 15:30:27 -050080 "last_modified": "Wed, 10 Nov 2009 23:00:00 GMT",
Jon Perritt457f8ca2014-10-15 00:28:23 -050081 "bytes": 14,
82 "name": "hello",
83 "content_type": "application/octet-stream"
84 }
85 ]`)
86 case "hello":
87 fmt.Fprintf(w, `[]`)
88 default:
89 t.Fatalf("Unexpected marker: [%s]", marker)
90 }
91 })
92}
93
94// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
95// responds with a `List` response when only object names are requested.
96func HandleListObjectNamesSuccessfully(t *testing.T) {
97 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
98 th.TestMethod(t, r, "GET")
99 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
100 th.TestHeader(t, r, "Accept", "text/plain")
101
102 w.Header().Set("Content-Type", "text/plain")
103 r.ParseForm()
104 marker := r.Form.Get("marker")
105 switch marker {
106 case "":
107 fmt.Fprintf(w, "hello\ngoodbye\n")
108 case "goodbye":
109 fmt.Fprintf(w, "")
110 default:
111 t.Fatalf("Unexpected marker: [%s]", marker)
112 }
113 })
114}
115
Ash Wilson93beae02015-01-23 14:14:48 -0500116// HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux
117// that responds with a `Create` response. A Content-Type of "text/plain" is expected.
Jamie Hannaford08096232015-07-13 12:47:28 +0200118func HandleCreateTextObjectSuccessfully(t *testing.T, content string) {
Ash Wilson93beae02015-01-23 14:14:48 -0500119 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
120 th.TestMethod(t, r, "PUT")
121 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
122 th.TestHeader(t, r, "Content-Type", "text/plain")
123 th.TestHeader(t, r, "Accept", "application/json")
Jamie Hannaford08096232015-07-13 12:47:28 +0200124
125 hash := md5.New()
126 io.WriteString(hash, content)
127 localChecksum := hash.Sum(nil)
128
129 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Ash Wilson93beae02015-01-23 14:14:48 -0500130 w.WriteHeader(http.StatusCreated)
131 })
132}
133
jrperritt9b7b9e62016-07-11 22:30:50 -0500134// HandleCreateTextWithCacheControlSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
135// mux that responds with a `Create` response. A Cache-Control of `max-age="3600", public` is expected.
136func HandleCreateTextWithCacheControlSuccessfully(t *testing.T, content string) {
137 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
138 th.TestMethod(t, r, "PUT")
139 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
140 th.TestHeader(t, r, "Cache-Control", `max-age="3600", public`)
141 th.TestHeader(t, r, "Accept", "application/json")
142
143 hash := md5.New()
144 io.WriteString(hash, content)
145 localChecksum := hash.Sum(nil)
146
147 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
148 w.WriteHeader(http.StatusCreated)
149 })
150}
151
Ash Wilson93beae02015-01-23 14:14:48 -0500152// HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
153// mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server-
154// side content-type detection will be triggered properly.
Jamie Hannaford08096232015-07-13 12:47:28 +0200155func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500156 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
157 th.TestMethod(t, r, "PUT")
158 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
159 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilson93beae02015-01-23 14:14:48 -0500160
161 if contentType, present := r.Header["Content-Type"]; present {
162 t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType)
163 }
164
Jamie Hannaford08096232015-07-13 12:47:28 +0200165 hash := md5.New()
166 io.WriteString(hash, content)
167 localChecksum := hash.Sum(nil)
168
169 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Jon Perritt457f8ca2014-10-15 00:28:23 -0500170 w.WriteHeader(http.StatusCreated)
171 })
172}
173
174// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
175// responds with a `Copy` response.
176func HandleCopyObjectSuccessfully(t *testing.T) {
177 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
178 th.TestMethod(t, r, "COPY")
179 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
180 th.TestHeader(t, r, "Accept", "application/json")
181 th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
182 w.WriteHeader(http.StatusCreated)
183 })
184}
185
186// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
187// responds with a `Delete` response.
188func HandleDeleteObjectSuccessfully(t *testing.T) {
189 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
190 th.TestMethod(t, r, "DELETE")
191 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
192 th.TestHeader(t, r, "Accept", "application/json")
193 w.WriteHeader(http.StatusNoContent)
194 })
195}
196
197// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
198// responds with a `Update` response.
199func HandleUpdateObjectSuccessfully(t *testing.T) {
200 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
201 th.TestMethod(t, r, "POST")
202 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
203 th.TestHeader(t, r, "Accept", "application/json")
204 th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
205 w.WriteHeader(http.StatusAccepted)
206 })
207}
208
209// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
210// responds with a `Get` response.
211func HandleGetObjectSuccessfully(t *testing.T) {
212 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
213 th.TestMethod(t, r, "HEAD")
214 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
215 th.TestHeader(t, r, "Accept", "application/json")
216 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
217 w.WriteHeader(http.StatusNoContent)
218 })
219}