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