blob: 08faab89a6016450a9486d729f616ec74ff99ed5 [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
jrperritt3d966162016-06-06 14:08:54 -050011 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
Jon Perritt27249f42016-02-18 10:35:59 -060012 th "github.com/gophercloud/gophercloud/testhelper"
13 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt457f8ca2014-10-15 00:28:23 -050014)
15
16// HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
17// responds with a `Download` response.
18func HandleDownloadObjectSuccessfully(t *testing.T) {
19 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
20 th.TestMethod(t, r, "GET")
21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
22 th.TestHeader(t, r, "Accept", "application/json")
jrperritt655245a2016-08-31 15:30:27 -050023 w.Header().Set("Date", "Wed, 10 Nov 2009 23:00:00 GMT")
Jon Perritt457f8ca2014-10-15 00:28:23 -050024 w.WriteHeader(http.StatusOK)
25 fmt.Fprintf(w, "Successful download with Gophercloud")
26 })
27}
28
29// ExpectedListInfo is the result expected from a call to `List` when full
30// info is requested.
jrperritt3d966162016-06-06 14:08:54 -050031var ExpectedListInfo = []objects.Object{
32 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050033 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt98d01622017-01-12 14:24:42 -060034 LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC), //"2016-08-17T22:11:58.602650"
Jon Perritt457f8ca2014-10-15 00:28:23 -050035 Bytes: 14,
36 Name: "goodbye",
37 ContentType: "application/octet-stream",
38 },
jrperritt3d966162016-06-06 14:08:54 -050039 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050040 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt98d01622017-01-12 14:24:42 -060041 LastModified: time.Date(2016, time.August, 17, 22, 11, 58, 602650000, time.UTC),
Jon Perritt457f8ca2014-10-15 00:28:23 -050042 Bytes: 14,
43 Name: "hello",
44 ContentType: "application/octet-stream",
45 },
46}
47
48// ExpectedListNames is the result expected from a call to `List` when just
49// object names are requested.
50var ExpectedListNames = []string{"hello", "goodbye"}
51
52// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
53// responds with a `List` response when full info is requested.
54func HandleListObjectsInfoSuccessfully(t *testing.T) {
55 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
56 th.TestMethod(t, r, "GET")
57 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
58 th.TestHeader(t, r, "Accept", "application/json")
59
60 w.Header().Set("Content-Type", "application/json")
61 r.ParseForm()
62 marker := r.Form.Get("marker")
63 switch marker {
64 case "":
65 fmt.Fprintf(w, `[
66 {
67 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt20c08522016-08-31 15:56:38 -050068 "last_modified": "2016-08-17T22:11:58.602650",
Jon Perritt457f8ca2014-10-15 00:28:23 -050069 "bytes": 14,
70 "name": "goodbye",
71 "content_type": "application/octet-stream"
72 },
73 {
74 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
jrperritt20c08522016-08-31 15:56:38 -050075 "last_modified": "2016-08-17T22:11:58.602650",
Jon Perritt457f8ca2014-10-15 00:28:23 -050076 "bytes": 14,
77 "name": "hello",
78 "content_type": "application/octet-stream"
79 }
80 ]`)
81 case "hello":
82 fmt.Fprintf(w, `[]`)
83 default:
84 t.Fatalf("Unexpected marker: [%s]", marker)
85 }
86 })
87}
88
89// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
90// responds with a `List` response when only object names are requested.
91func HandleListObjectNamesSuccessfully(t *testing.T) {
92 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
93 th.TestMethod(t, r, "GET")
94 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
95 th.TestHeader(t, r, "Accept", "text/plain")
96
97 w.Header().Set("Content-Type", "text/plain")
98 r.ParseForm()
99 marker := r.Form.Get("marker")
100 switch marker {
101 case "":
102 fmt.Fprintf(w, "hello\ngoodbye\n")
103 case "goodbye":
104 fmt.Fprintf(w, "")
105 default:
106 t.Fatalf("Unexpected marker: [%s]", marker)
107 }
108 })
109}
110
Ash Wilson93beae02015-01-23 14:14:48 -0500111// HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux
112// that responds with a `Create` response. A Content-Type of "text/plain" is expected.
Jamie Hannaford08096232015-07-13 12:47:28 +0200113func HandleCreateTextObjectSuccessfully(t *testing.T, content string) {
Ash Wilson93beae02015-01-23 14:14:48 -0500114 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "PUT")
116 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
117 th.TestHeader(t, r, "Content-Type", "text/plain")
118 th.TestHeader(t, r, "Accept", "application/json")
Jamie Hannaford08096232015-07-13 12:47:28 +0200119
120 hash := md5.New()
121 io.WriteString(hash, content)
122 localChecksum := hash.Sum(nil)
123
124 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Ash Wilson93beae02015-01-23 14:14:48 -0500125 w.WriteHeader(http.StatusCreated)
126 })
127}
128
jrperritt9b7b9e62016-07-11 22:30:50 -0500129// HandleCreateTextWithCacheControlSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
130// mux that responds with a `Create` response. A Cache-Control of `max-age="3600", public` is expected.
131func HandleCreateTextWithCacheControlSuccessfully(t *testing.T, content string) {
132 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
133 th.TestMethod(t, r, "PUT")
134 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
135 th.TestHeader(t, r, "Cache-Control", `max-age="3600", public`)
136 th.TestHeader(t, r, "Accept", "application/json")
137
138 hash := md5.New()
139 io.WriteString(hash, content)
140 localChecksum := hash.Sum(nil)
141
142 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
143 w.WriteHeader(http.StatusCreated)
144 })
145}
146
Ash Wilson93beae02015-01-23 14:14:48 -0500147// HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
148// mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server-
149// side content-type detection will be triggered properly.
Jamie Hannaford08096232015-07-13 12:47:28 +0200150func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500151 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
152 th.TestMethod(t, r, "PUT")
153 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
154 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilson93beae02015-01-23 14:14:48 -0500155
156 if contentType, present := r.Header["Content-Type"]; present {
157 t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType)
158 }
159
Jamie Hannaford08096232015-07-13 12:47:28 +0200160 hash := md5.New()
161 io.WriteString(hash, content)
162 localChecksum := hash.Sum(nil)
163
164 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Jon Perritt457f8ca2014-10-15 00:28:23 -0500165 w.WriteHeader(http.StatusCreated)
166 })
167}
168
169// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
170// responds with a `Copy` response.
171func HandleCopyObjectSuccessfully(t *testing.T) {
172 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
173 th.TestMethod(t, r, "COPY")
174 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
175 th.TestHeader(t, r, "Accept", "application/json")
176 th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
177 w.WriteHeader(http.StatusCreated)
178 })
179}
180
181// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
182// responds with a `Delete` response.
183func HandleDeleteObjectSuccessfully(t *testing.T) {
184 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
185 th.TestMethod(t, r, "DELETE")
186 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
187 th.TestHeader(t, r, "Accept", "application/json")
188 w.WriteHeader(http.StatusNoContent)
189 })
190}
191
192// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
193// responds with a `Update` response.
194func HandleUpdateObjectSuccessfully(t *testing.T) {
195 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
196 th.TestMethod(t, r, "POST")
197 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
198 th.TestHeader(t, r, "Accept", "application/json")
199 th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
200 w.WriteHeader(http.StatusAccepted)
201 })
202}
203
204// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
205// responds with a `Get` response.
206func HandleGetObjectSuccessfully(t *testing.T) {
207 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
208 th.TestMethod(t, r, "HEAD")
209 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
210 th.TestHeader(t, r, "Accept", "application/json")
211 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
212 w.WriteHeader(http.StatusNoContent)
213 })
214}