blob: d951160e3a1685db12971c4b075f3d230448abc2 [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
108// HandleCreateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
109// responds with a `Create` response.
110func HandleCreateObjectSuccessfully(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, "Accept", "application/json")
115 w.WriteHeader(http.StatusCreated)
116 })
117}
118
119// HandleCopyObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
120// responds with a `Copy` response.
121func HandleCopyObjectSuccessfully(t *testing.T) {
122 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
123 th.TestMethod(t, r, "COPY")
124 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
125 th.TestHeader(t, r, "Accept", "application/json")
126 th.TestHeader(t, r, "Destination", "/newTestContainer/newTestObject")
127 w.WriteHeader(http.StatusCreated)
128 })
129}
130
131// HandleDeleteObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
132// responds with a `Delete` response.
133func HandleDeleteObjectSuccessfully(t *testing.T) {
134 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
135 th.TestMethod(t, r, "DELETE")
136 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
137 th.TestHeader(t, r, "Accept", "application/json")
138 w.WriteHeader(http.StatusNoContent)
139 })
140}
141
142// HandleUpdateObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
143// responds with a `Update` response.
144func HandleUpdateObjectSuccessfully(t *testing.T) {
145 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
146 th.TestMethod(t, r, "POST")
147 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
148 th.TestHeader(t, r, "Accept", "application/json")
149 th.TestHeader(t, r, "X-Object-Meta-Gophercloud-Test", "objects")
150 w.WriteHeader(http.StatusAccepted)
151 })
152}
153
154// HandleGetObjectSuccessfully creates an HTTP handler at `/testContainer/testObject` on the test handler mux that
155// responds with a `Get` response.
156func HandleGetObjectSuccessfully(t *testing.T) {
157 th.Mux.HandleFunc("/testContainer/testObject", func(w http.ResponseWriter, r *http.Request) {
158 th.TestMethod(t, r, "HEAD")
159 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
160 th.TestHeader(t, r, "Accept", "application/json")
161 w.Header().Add("X-Object-Meta-Gophercloud-Test", "objects")
162 w.WriteHeader(http.StatusNoContent)
163 })
164}