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