blob: fde8815f5a4b1c88c1d0c7ccb7144bcb50775a35 [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
Jon Perritt27249f42016-02-18 10:35:59 -060010 th "github.com/gophercloud/gophercloud/testhelper"
11 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt457f8ca2014-10-15 00:28:23 -050012)
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 ]`)
Jon Perrittfe5e7352015-02-18 13:51:01 -070058 case "janeausten":
59 fmt.Fprintf(w, `[
60 {
61 "count": 1,
62 "bytes": 14,
63 "name": "marktwain"
64 }
65 ]`)
Jon Perritt457f8ca2014-10-15 00:28:23 -050066 case "marktwain":
67 fmt.Fprintf(w, `[]`)
68 default:
69 t.Fatalf("Unexpected marker: [%s]", marker)
70 }
71 })
72}
73
74// HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that
75// responds with a `ListNames` response when only container names are requested.
76func HandleListContainerNamesSuccessfully(t *testing.T) {
77 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
78 th.TestMethod(t, r, "GET")
79 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
80 th.TestHeader(t, r, "Accept", "text/plain")
81
82 w.Header().Set("Content-Type", "text/plain")
83 r.ParseForm()
84 marker := r.Form.Get("marker")
85 switch marker {
86 case "":
87 fmt.Fprintf(w, "janeausten\nmarktwain\n")
Jon Perrittfe5e7352015-02-18 13:51:01 -070088 case "janeausten":
89 fmt.Fprintf(w, "marktwain\n")
Jon Perritt457f8ca2014-10-15 00:28:23 -050090 case "marktwain":
91 fmt.Fprintf(w, ``)
92 default:
93 t.Fatalf("Unexpected marker: [%s]", marker)
94 }
95 })
96}
97
98// HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
99// responds with a `Create` response.
100func HandleCreateContainerSuccessfully(t *testing.T) {
101 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
102 th.TestMethod(t, r, "PUT")
103 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
104 th.TestHeader(t, r, "Accept", "application/json")
105
106 w.Header().Add("X-Container-Meta-Foo", "bar")
Jon Perritt59b0ea42015-01-31 20:23:33 -0700107 w.Header().Add("X-Trans-Id", "1234567")
Jon Perritt457f8ca2014-10-15 00:28:23 -0500108 w.WriteHeader(http.StatusNoContent)
109 })
110}
111
112// HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
113// responds with a `Delete` response.
114func HandleDeleteContainerSuccessfully(t *testing.T) {
115 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
116 th.TestMethod(t, r, "DELETE")
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// HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
124// responds with a `Update` response.
125func HandleUpdateContainerSuccessfully(t *testing.T) {
126 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
127 th.TestMethod(t, r, "POST")
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}
133
134// HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
135// responds with a `Get` response.
136func HandleGetContainerSuccessfully(t *testing.T) {
137 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
138 th.TestMethod(t, r, "HEAD")
139 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
140 th.TestHeader(t, r, "Accept", "application/json")
141 w.WriteHeader(http.StatusNoContent)
142 })
143}