blob: 1c0a915cb7dd4bd2ef1066028c90c040d7fc03b3 [file] [log] [blame]
Jon Perritt457f8ca2014-10-15 00:28:23 -05001// +build fixtures
2
3package containers
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// ExpectedListInfo is the result expected from a call to `List` when full
15// info is requested.
16var ExpectedListInfo = []Container{
17 Container{
18 Count: 0,
19 Bytes: 0,
20 Name: "janeausten",
21 },
22 Container{
23 Count: 1,
24 Bytes: 14,
25 Name: "marktwain",
26 },
27}
28
29// ExpectedListNames is the result expected from a call to `List` when just
30// container names are requested.
31var ExpectedListNames = []string{"janeausten", "marktwain"}
32
33// HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that
34// responds with a `List` response when full info is requested.
35func HandleListContainerInfoSuccessfully(t *testing.T) {
36 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
37 th.TestMethod(t, r, "GET")
38 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
39 th.TestHeader(t, r, "Accept", "application/json")
40
41 w.Header().Set("Content-Type", "application/json")
42 r.ParseForm()
43 marker := r.Form.Get("marker")
44 switch marker {
45 case "":
46 fmt.Fprintf(w, `[
47 {
48 "count": 0,
49 "bytes": 0,
50 "name": "janeausten"
51 },
52 {
53 "count": 1,
54 "bytes": 14,
55 "name": "marktwain"
56 }
57 ]`)
58 case "marktwain":
59 fmt.Fprintf(w, `[]`)
60 default:
61 t.Fatalf("Unexpected marker: [%s]", marker)
62 }
63 })
64}
65
66// HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that
67// responds with a `ListNames` response when only container names are requested.
68func HandleListContainerNamesSuccessfully(t *testing.T) {
69 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
70 th.TestMethod(t, r, "GET")
71 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
72 th.TestHeader(t, r, "Accept", "text/plain")
73
74 w.Header().Set("Content-Type", "text/plain")
75 r.ParseForm()
76 marker := r.Form.Get("marker")
77 switch marker {
78 case "":
79 fmt.Fprintf(w, "janeausten\nmarktwain\n")
80 case "marktwain":
81 fmt.Fprintf(w, ``)
82 default:
83 t.Fatalf("Unexpected marker: [%s]", marker)
84 }
85 })
86}
87
88// HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
89// responds with a `Create` response.
90func HandleCreateContainerSuccessfully(t *testing.T) {
91 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
92 th.TestMethod(t, r, "PUT")
93 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
94 th.TestHeader(t, r, "Accept", "application/json")
95
96 w.Header().Add("X-Container-Meta-Foo", "bar")
97 w.WriteHeader(http.StatusNoContent)
98 })
99}
100
101// HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
102// responds with a `Delete` response.
103func HandleDeleteContainerSuccessfully(t *testing.T) {
104 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
105 th.TestMethod(t, r, "DELETE")
106 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
107 th.TestHeader(t, r, "Accept", "application/json")
108 w.WriteHeader(http.StatusNoContent)
109 })
110}
111
112// HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
113// responds with a `Update` response.
114func HandleUpdateContainerSuccessfully(t *testing.T) {
115 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
116 th.TestMethod(t, r, "POST")
117 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
118 th.TestHeader(t, r, "Accept", "application/json")
119 w.WriteHeader(http.StatusNoContent)
120 })
121}
122
123// HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
124// responds with a `Get` response.
125func HandleGetContainerSuccessfully(t *testing.T) {
126 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
127 th.TestMethod(t, r, "HEAD")
128 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
129 th.TestHeader(t, r, "Accept", "application/json")
130 w.WriteHeader(http.StatusNoContent)
131 })
132}