blob: fe579d8358ccfd15ce9adb3fb041ea5ee996c82d [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jon Perritt457f8ca2014-10-15 00:28:23 -05002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
jrperritt3d966162016-06-06 14:08:54 -05008 "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
Jon Perritt27249f42016-02-18 10:35:59 -06009 th "github.com/gophercloud/gophercloud/testhelper"
10 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt457f8ca2014-10-15 00:28:23 -050011)
12
13// ExpectedListInfo is the result expected from a call to `List` when full
14// info is requested.
jrperritt3d966162016-06-06 14:08:54 -050015var ExpectedListInfo = []containers.Container{
16 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050017 Count: 0,
18 Bytes: 0,
19 Name: "janeausten",
20 },
jrperritt3d966162016-06-06 14:08:54 -050021 {
Jon Perritt457f8ca2014-10-15 00:28:23 -050022 Count: 1,
23 Bytes: 14,
24 Name: "marktwain",
25 },
26}
27
28// ExpectedListNames is the result expected from a call to `List` when just
29// container names are requested.
30var ExpectedListNames = []string{"janeausten", "marktwain"}
31
32// HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that
33// responds with a `List` response when full info is requested.
34func HandleListContainerInfoSuccessfully(t *testing.T) {
35 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
36 th.TestMethod(t, r, "GET")
37 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
38 th.TestHeader(t, r, "Accept", "application/json")
39
40 w.Header().Set("Content-Type", "application/json")
41 r.ParseForm()
42 marker := r.Form.Get("marker")
43 switch marker {
44 case "":
45 fmt.Fprintf(w, `[
46 {
47 "count": 0,
48 "bytes": 0,
49 "name": "janeausten"
50 },
51 {
52 "count": 1,
53 "bytes": 14,
54 "name": "marktwain"
55 }
56 ]`)
Jon Perrittfe5e7352015-02-18 13:51:01 -070057 case "janeausten":
58 fmt.Fprintf(w, `[
59 {
60 "count": 1,
61 "bytes": 14,
62 "name": "marktwain"
63 }
64 ]`)
Jon Perritt457f8ca2014-10-15 00:28:23 -050065 case "marktwain":
66 fmt.Fprintf(w, `[]`)
67 default:
68 t.Fatalf("Unexpected marker: [%s]", marker)
69 }
70 })
71}
72
73// HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that
74// responds with a `ListNames` response when only container names are requested.
75func HandleListContainerNamesSuccessfully(t *testing.T) {
76 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
77 th.TestMethod(t, r, "GET")
78 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
79 th.TestHeader(t, r, "Accept", "text/plain")
80
81 w.Header().Set("Content-Type", "text/plain")
82 r.ParseForm()
83 marker := r.Form.Get("marker")
84 switch marker {
85 case "":
86 fmt.Fprintf(w, "janeausten\nmarktwain\n")
Jon Perrittfe5e7352015-02-18 13:51:01 -070087 case "janeausten":
88 fmt.Fprintf(w, "marktwain\n")
Jon Perritt457f8ca2014-10-15 00:28:23 -050089 case "marktwain":
90 fmt.Fprintf(w, ``)
91 default:
92 t.Fatalf("Unexpected marker: [%s]", marker)
93 }
94 })
95}
96
97// HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
98// responds with a `Create` response.
99func HandleCreateContainerSuccessfully(t *testing.T) {
100 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
101 th.TestMethod(t, r, "PUT")
102 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
103 th.TestHeader(t, r, "Accept", "application/json")
104
105 w.Header().Add("X-Container-Meta-Foo", "bar")
Jon Perritt59b0ea42015-01-31 20:23:33 -0700106 w.Header().Add("X-Trans-Id", "1234567")
Jon Perritt457f8ca2014-10-15 00:28:23 -0500107 w.WriteHeader(http.StatusNoContent)
108 })
109}
110
111// HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
112// responds with a `Delete` response.
113func HandleDeleteContainerSuccessfully(t *testing.T) {
114 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "DELETE")
116 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
117 th.TestHeader(t, r, "Accept", "application/json")
118 w.WriteHeader(http.StatusNoContent)
119 })
120}
121
122// HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
123// responds with a `Update` response.
124func HandleUpdateContainerSuccessfully(t *testing.T) {
125 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
126 th.TestMethod(t, r, "POST")
127 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
128 th.TestHeader(t, r, "Accept", "application/json")
129 w.WriteHeader(http.StatusNoContent)
130 })
131}
132
133// HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that
134// responds with a `Get` response.
135func HandleGetContainerSuccessfully(t *testing.T) {
136 th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
137 th.TestMethod(t, r, "HEAD")
138 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
139 th.TestHeader(t, r, "Accept", "application/json")
140 w.WriteHeader(http.StatusNoContent)
141 })
142}