blob: 7a6e6e1ee776c45071a2e5c3a9f7824f1847fd20 [file] [log] [blame]
Jon Perritt457f8ca2014-10-15 00:28:23 -05001// +build fixtures
2
3package objects
4
5import (
Jamie Hannaford08096232015-07-13 12:47:28 +02006 "crypto/md5"
Jon Perritt457f8ca2014-10-15 00:28:23 -05007 "fmt"
Jamie Hannaford08096232015-07-13 12:47:28 +02008 "io"
Jon Perritt457f8ca2014-10-15 00:28:23 -05009 "net/http"
10 "testing"
11
12 th "github.com/rackspace/gophercloud/testhelper"
13 fake "github.com/rackspace/gophercloud/testhelper/client"
14)
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")
23 w.WriteHeader(http.StatusOK)
24 fmt.Fprintf(w, "Successful download with Gophercloud")
25 })
26}
27
28// ExpectedListInfo is the result expected from a call to `List` when full
29// info is requested.
30var ExpectedListInfo = []Object{
31 Object{
32 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
33 LastModified: "2009-11-10 23:00:00 +0000 UTC",
34 Bytes: 14,
35 Name: "goodbye",
36 ContentType: "application/octet-stream",
37 },
38 Object{
39 Hash: "451e372e48e0f6b1114fa0724aa79fa1",
40 LastModified: "2009-11-10 23:00:00 +0000 UTC",
41 Bytes: 14,
42 Name: "hello",
43 ContentType: "application/octet-stream",
44 },
45}
46
47// ExpectedListNames is the result expected from a call to `List` when just
48// object names are requested.
49var ExpectedListNames = []string{"hello", "goodbye"}
50
51// HandleListObjectsInfoSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
52// responds with a `List` response when full info is requested.
53func HandleListObjectsInfoSuccessfully(t *testing.T) {
54 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
55 th.TestMethod(t, r, "GET")
56 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
57 th.TestHeader(t, r, "Accept", "application/json")
58
59 w.Header().Set("Content-Type", "application/json")
60 r.ParseForm()
61 marker := r.Form.Get("marker")
62 switch marker {
63 case "":
64 fmt.Fprintf(w, `[
65 {
66 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
67 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
68 "bytes": 14,
69 "name": "goodbye",
70 "content_type": "application/octet-stream"
71 },
72 {
73 "hash": "451e372e48e0f6b1114fa0724aa79fa1",
74 "last_modified": "2009-11-10 23:00:00 +0000 UTC",
75 "bytes": 14,
76 "name": "hello",
77 "content_type": "application/octet-stream"
78 }
79 ]`)
80 case "hello":
81 fmt.Fprintf(w, `[]`)
82 default:
83 t.Fatalf("Unexpected marker: [%s]", marker)
84 }
85 })
86}
87
88// HandleListObjectNamesSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
89// responds with a `List` response when only object names are requested.
90func HandleListObjectNamesSuccessfully(t *testing.T) {
91 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
92 th.TestMethod(t, r, "GET")
93 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
94 th.TestHeader(t, r, "Accept", "text/plain")
95
96 w.Header().Set("Content-Type", "text/plain")
97 r.ParseForm()
98 marker := r.Form.Get("marker")
99 switch marker {
100 case "":
101 fmt.Fprintf(w, "hello\ngoodbye\n")
102 case "goodbye":
103 fmt.Fprintf(w, "")
104 default:
105 t.Fatalf("Unexpected marker: [%s]", marker)
106 }
107 })
108}
109
Ash Wilson93beae02015-01-23 14:14:48 -0500110// HandleCreateTextObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux
111// that responds with a `Create` response. A Content-Type of "text/plain" is expected.
Jamie Hannaford08096232015-07-13 12:47:28 +0200112func HandleCreateTextObjectSuccessfully(t *testing.T, content string) {
Ash Wilson93beae02015-01-23 14:14:48 -0500113 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
114 th.TestMethod(t, r, "PUT")
115 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
116 th.TestHeader(t, r, "Content-Type", "text/plain")
117 th.TestHeader(t, r, "Accept", "application/json")
Jamie Hannaford08096232015-07-13 12:47:28 +0200118
119 hash := md5.New()
120 io.WriteString(hash, content)
121 localChecksum := hash.Sum(nil)
122
123 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Ash Wilson93beae02015-01-23 14:14:48 -0500124 w.WriteHeader(http.StatusCreated)
125 })
126}
127
128// HandleCreateTypelessObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler
129// mux that responds with a `Create` response. No Content-Type header may be present in the request, so that server-
130// side content-type detection will be triggered properly.
Jamie Hannaford08096232015-07-13 12:47:28 +0200131func HandleCreateTypelessObjectSuccessfully(t *testing.T, content string) {
Jon Perritt457f8ca2014-10-15 00:28:23 -0500132 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, "Accept", "application/json")
Ash Wilson93beae02015-01-23 14:14:48 -0500136
137 if contentType, present := r.Header["Content-Type"]; present {
138 t.Errorf("Expected Content-Type header to be omitted, but was %#v", contentType)
139 }
140
Jamie Hannaford08096232015-07-13 12:47:28 +0200141 hash := md5.New()
142 io.WriteString(hash, content)
143 localChecksum := hash.Sum(nil)
144
145 w.Header().Set("ETag", fmt.Sprintf("%x", localChecksum))
Jon Perritt457f8ca2014-10-15 00:28:23 -0500146 w.WriteHeader(http.StatusCreated)
147 })
148}
149
150// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
151// responds with a `Copy` response.
152func HandleCopyObjectSuccessfully(t *testing.T) {
153 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
154 th.TestMethod(t, r, "COPY")
155 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
156 th.TestHeader(t, r, "Accept", "application/json")
157 th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
158 w.WriteHeader(http.StatusCreated)
159 })
160}
161
162// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
163// responds with a `Delete` response.
164func HandleDeleteObjectSuccessfully(t *testing.T) {
165 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
166 th.TestMethod(t, r, "DELETE")
167 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
168 th.TestHeader(t, r, "Accept", "application/json")
169 w.WriteHeader(http.StatusNoContent)
170 })
171}
172
173// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
174// responds with a `Update` response.
175func HandleUpdateObjectSuccessfully(t *testing.T) {
176 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
177 th.TestMethod(t, r, "POST")
178 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
179 th.TestHeader(t, r, "Accept", "application/json")
180 th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
181 w.WriteHeader(http.StatusAccepted)
182 })
183}
184
185// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
186// responds with a `Get` response.
187func HandleGetObjectSuccessfully(t *testing.T) {
188 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
189 th.TestMethod(t, r, "HEAD")
190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
191 th.TestHeader(t, r, "Accept", "application/json")
192 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
193 w.WriteHeader(http.StatusNoContent)
194 })
195}