blob: cf1d6c5665af7903716e82d8b4a01bcf9edb70c6 [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"
9
jrperritt3d966162016-06-06 14:08:54 -050010 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
Jon Perritt27249f42016-02-18 10:35:59 -060011 th "github.com/gophercloud/gophercloud/testhelper"
12 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt457f8ca2014-10-15 00:28:23 -050013)
14
15// HandleDownloadObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
16// responds with a `Download` response.
17func HandleDownloadObjectSuccessfully(t *testing.T) {
18 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21 th.TestHeader(t, r, "Accept", "application/json")
22 w.WriteHeader(http.StatusOK)
23 fmt.Fprintf(w, "Successful download with Gophercloud")
24 })
25}
26
27// ExpectedListInfo is the result expected from a call to `List` when full
28// info is requested.
jrperritt3d966162016-06-06 14:08:54 -050029var ExpectedListInfo = []objects.Object{
30 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050031 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
32 LastModified: "2009-11-10 23:00:00 +0000 UTC",
33 Bytes: 14,
34 Name: "goodbye",
35 ContentType: "application/octet-stream",
36 },
jrperritt3d966162016-06-06 14:08:54 -050037 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050038 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
39 LastModified: "2009-11-10 23:00:00 +0000 UTC",
40 Bytes: 14,
41 Name: "hello",
42 ContentType: "application/octet-stream",
43 },
44}
45
46// ExpectedListNames is the result expected from a call to `List` when just
47// object names are requested.
48var ExpectedListNames = []string{"hello", "goodbye"}
49
50// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
51// responds with a `List` response when full info is requested.
52func HandleListObjectsInfoSuccessfully(t *testing.T) {
53 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
54 th.TestMethod(t, r, "GET")
55 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
56 th.TestHeader(t, r, "Accept", "application/json")
57
58 w.Header().Set("Content-Type", "application/json")
59 r.ParseForm()
60 marker := r.Form.Get("marker")
61 switch marker {
62 case "":
63 fmt.Fprintf(w, `[
64 {
65 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
66 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
67 "bytes": 14,
68 "name": "goodbye",
69 "content_type": "application/octet-stream"
70 },
71 {
72 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
73 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
74 "bytes": 14,
75 "name": "hello",
76 "content_type": "application/octet-stream"
77 }
78 ]`)
79 case "hello":
80 fmt.Fprintf(w, `[]`)
81 default:
82 t.Fatalf("Unexpected marker: [%s]", marker)
83 }
84 })
85}
86
87// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
88// responds with a `List` response when only object names are requested.
89func HandleListObjectNamesSuccessfully(t *testing.T) {
90 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
91 th.TestMethod(t, r, "GET")
92 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
93 th.TestHeader(t, r, "Accept", "text/plain")
94
95 w.Header().Set("Content-Type", "text/plain")
96 r.ParseForm()
97 marker := r.Form.Get("marker")
98 switch marker {
99 case "":
100 fmt.Fprintf(w, "hello\ngoodbye\n")
101 case "goodbye":
102 fmt.Fprintf(w, "")
103 default:
104 t.Fatalf("Unexpected marker: [%s]", marker)
105 }
106 })
107}
108
Ash Wilson93beae02015-01-23 14:14:48 -0500109// HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux
110// that responds with a `Create` response. A Content-Type of "text/plain" is expected.
Jamie Hannaford08096232015-07-13 12:47:28 +0200111func HandleCreateTextObjectSuccessfully(t *testing.T, content string) {
Ash Wilson93beae02015-01-23 14:14:48 -0500112 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
113 th.TestMethod(t, r, "PUT")
114 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
115 th.TestHeader(t, r, "Content-Type", "text/plain")
116 th.TestHeader(t, r, "Accept", "application/json")
Jamie Hannaford08096232015-07-13 12:47:28 +0200117
118 hash := md5.New()
119 io.WriteString(hash, content)
120 localChecksum := hash.Sum(nil)
121
122 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Ash Wilson93beae02015-01-23 14:14:48 -0500123 w.WriteHeader(http.StatusCreated)
124 })
125}
126
jrperritt9b7b9e62016-07-11 22:30:50 -0500127// HandleCreateTextWithCacheControlSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
128// mux that responds with a `Create` response. A Cache-Control of `max-age="3600", public` is expected.
129func HandleCreateTextWithCacheControlSuccessfully(t *testing.T, content string) {
130 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
131 th.TestMethod(t, r, "PUT")
132 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
133 th.TestHeader(t, r, "Cache-Control", `max-age="3600", public`)
134 th.TestHeader(t, r, "Accept", "application/json")
135
136 hash := md5.New()
137 io.WriteString(hash, content)
138 localChecksum := hash.Sum(nil)
139
140 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
141 w.WriteHeader(http.StatusCreated)
142 })
143}
144
Ash Wilson93beae02015-01-23 14:14:48 -0500145// HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
146// mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server-
147// side content-type detection will be triggered properly.
Jamie Hannaford08096232015-07-13 12:47:28 +0200148func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500149 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
150 th.TestMethod(t, r, "PUT")
151 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
152 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilson93beae02015-01-23 14:14:48 -0500153
154 if contentType, present := r.Header["Content-Type"]; present {
155 t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType)
156 }
157
Jamie Hannaford08096232015-07-13 12:47:28 +0200158 hash := md5.New()
159 io.WriteString(hash, content)
160 localChecksum := hash.Sum(nil)
161
162 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Jon Perritt457f8ca2014-10-15 00:28:23 -0500163 w.WriteHeader(http.StatusCreated)
164 })
165}
166
167// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
168// responds with a `Copy` response.
169func HandleCopyObjectSuccessfully(t *testing.T) {
170 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
171 th.TestMethod(t, r, "COPY")
172 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
173 th.TestHeader(t, r, "Accept", "application/json")
174 th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
175 w.WriteHeader(http.StatusCreated)
176 })
177}
178
179// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
180// responds with a `Delete` response.
181func HandleDeleteObjectSuccessfully(t *testing.T) {
182 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
183 th.TestMethod(t, r, "DELETE")
184 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
185 th.TestHeader(t, r, "Accept", "application/json")
186 w.WriteHeader(http.StatusNoContent)
187 })
188}
189
190// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
191// responds with a `Update` response.
192func HandleUpdateObjectSuccessfully(t *testing.T) {
193 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
194 th.TestMethod(t, r, "POST")
195 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
196 th.TestHeader(t, r, "Accept", "application/json")
197 th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
198 w.WriteHeader(http.StatusAccepted)
199 })
200}
201
202// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
203// responds with a `Get` response.
204func HandleGetObjectSuccessfully(t *testing.T) {
205 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
206 th.TestMethod(t, r, "HEAD")
207 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
208 th.TestHeader(t, r, "Accept", "application/json")
209 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
210 w.WriteHeader(http.StatusNoContent)
211 })
212}