Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 // Swift double testing service - HTTP API tests | |
2 | |
3 package swiftservice | |
4 | |
5 import ( | |
6 "bytes" | |
7 "io/ioutil" | |
8 . "launchpad.net/gocheck" | |
9 "launchpad.net/goose/testing/httpsuite" | |
10 "net/http" | |
11 ) | |
12 | |
13 type SwiftHTTPSuite struct { | |
14 httpsuite.HTTPSuite | |
15 service SwiftService | |
16 } | |
17 | |
18 var _ = Suite(&SwiftHTTPSuite{}) | |
19 | |
20 func (s *SwiftHTTPSuite) SetUpSuite(c *C) { | |
21 s.HTTPSuite.SetUpSuite(c) | |
22 s.service = New(s.Server.URL, baseURL, token) | |
23 } | |
24 | |
25 func (s *SwiftHTTPSuite) SetUpTest(c *C) { | |
26 s.HTTPSuite.SetUpTest(c) | |
27 s.Mux.Handle(baseURL, s.service) | |
28 } | |
29 | |
30 func (s *SwiftHTTPSuite) TearDownTest(c *C) { | |
31 s.HTTPSuite.TearDownTest(c) | |
32 } | |
33 | |
34 func (s *SwiftHTTPSuite) TearDownSuite(c *C) { | |
35 s.HTTPSuite.TearDownSuite(c) | |
36 } | |
37 | |
38 func (s *SwiftHTTPSuite) sendRequest(method, path string, body []byte) (*http.Re sponse, error) { | |
jameinel
2012/11/29 06:31:40
All callers of this follow up with:
c.Assert(err,
| |
39 var req *http.Request | |
40 var err error | |
41 url := s.Server.URL + baseURL + path | |
42 if body != nil { | |
43 req, err = http.NewRequest(method, url, bytes.NewBuffer(body)) | |
44 } else { | |
45 req, err = http.NewRequest(method, url, nil) | |
46 } | |
47 if err != nil { | |
48 return nil, err | |
49 } | |
50 if token != "" { | |
jameinel
2012/11/29 06:31:40
Where is 'token' being defined? I just realized I
| |
51 req.Header.Add("X-Auth-Token", token) | |
52 } | |
53 client := &http.Client{} | |
54 return client.Do(req) | |
55 } | |
56 | |
57 func (s *SwiftHTTPSuite) ensureNotContainer(name string, c *C) { | |
58 ok := s.service.HasContainer("test") | |
59 c.Assert(ok, Equals, false) | |
60 } | |
61 | |
62 func (s *SwiftHTTPSuite) ensureContainer(name string, c *C) { | |
63 s.ensureNotContainer(name, c) | |
64 err := s.service.AddContainer("test") | |
65 c.Assert(err, IsNil) | |
66 } | |
67 | |
68 func (s *SwiftHTTPSuite) removeContainer(name string, c *C) { | |
69 ok := s.service.HasContainer("test") | |
70 c.Assert(ok, Equals, true) | |
71 err := s.service.RemoveContainer("test") | |
72 c.Assert(err, IsNil) | |
73 } | |
74 | |
75 func (s *SwiftHTTPSuite) ensureNotObject(container, object string, c *C) { | |
76 _, err := s.service.GetObject(container, object) | |
77 c.Assert(err, Not(IsNil)) | |
78 } | |
79 | |
80 func (s *SwiftHTTPSuite) ensureObject(container, object string, data []byte, c * C) { | |
81 s.ensureNotObject(container, object, c) | |
82 err := s.service.AddObject(container, object, data) | |
83 c.Assert(err, IsNil) | |
84 } | |
85 | |
86 func (s *SwiftHTTPSuite) ensureObjectData(container, object string, data []byte, c *C) { | |
87 objdata, err := s.service.GetObject(container, object) | |
88 c.Assert(err, IsNil) | |
89 c.Assert(objdata, DeepEquals, data) | |
90 } | |
91 | |
92 func (s *SwiftHTTPSuite) removeObject(container, object string, c *C) { | |
93 err := s.service.RemoveObject(container, object) | |
94 c.Assert(err, IsNil) | |
95 s.ensureNotObject(container, object, c) | |
96 } | |
97 | |
98 func (s *SwiftHTTPSuite) TestPUTContainerMissingCreated(c *C) { | |
99 s.ensureNotContainer("test", c) | |
100 | |
101 resp, err := s.sendRequest("PUT", "test", nil) | |
102 c.Assert(err, IsNil) | |
103 c.Assert(resp.StatusCode, Equals, http.StatusCreated) | |
104 | |
105 s.removeContainer("test", c) | |
106 } | |
jameinel
2012/11/29 06:31:40
The 'removeContainer' here looks like cleanup. So
| |
107 | |
108 func (s *SwiftHTTPSuite) TestPUTContainerExistsAccepted(c *C) { | |
109 s.ensureContainer("test", c) | |
110 | |
111 resp, err := s.sendRequest("PUT", "test", nil) | |
112 c.Assert(err, IsNil) | |
113 c.Assert(resp.StatusCode, Equals, http.StatusAccepted) | |
114 | |
115 s.removeContainer("test", c) | |
116 } | |
117 | |
118 func (s *SwiftHTTPSuite) TestGETContainerMissingNotFound(c *C) { | |
119 s.ensureNotContainer("test", c) | |
120 | |
121 resp, err := s.sendRequest("GET", "test", nil) | |
122 c.Assert(err, IsNil) | |
123 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
124 | |
125 s.ensureNotContainer("test", c) | |
126 } | |
127 | |
128 func (s *SwiftHTTPSuite) TestGETContainerExistsOK(c *C) { | |
129 s.ensureContainer("test", c) | |
130 | |
131 resp, err := s.sendRequest("GET", "test", nil) | |
132 c.Assert(err, IsNil) | |
133 c.Assert(resp.StatusCode, Equals, http.StatusOK) | |
134 | |
135 s.removeContainer("test", c) | |
136 } | |
137 | |
138 func (s *SwiftHTTPSuite) TestDELETEContainerMissingNotFound(c *C) { | |
139 s.ensureNotContainer("test", c) | |
140 | |
141 resp, err := s.sendRequest("DELETE", "test", nil) | |
142 c.Assert(err, IsNil) | |
143 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
144 } | |
145 | |
146 func (s *SwiftHTTPSuite) TestDELETEContainerExistsNoContent(c *C) { | |
147 s.ensureContainer("test", c) | |
148 | |
149 resp, err := s.sendRequest("DELETE", "test", nil) | |
150 c.Assert(err, IsNil) | |
151 c.Assert(resp.StatusCode, Equals, http.StatusNoContent) | |
152 | |
153 s.ensureNotContainer("test", c) | |
154 } | |
155 | |
156 func (s *SwiftHTTPSuite) TestPUTObjectMissingCreated(c *C) { | |
157 s.ensureContainer("test", c) | |
158 s.ensureNotObject("test", "obj", c) | |
159 | |
160 data := []byte("test data") | |
161 resp, err := s.sendRequest("PUT", "test/obj", data) | |
162 c.Assert(err, IsNil) | |
163 c.Assert(resp.StatusCode, Equals, http.StatusCreated) | |
164 | |
165 s.ensureObjectData("test", "obj", data, c) | |
166 s.removeContainer("test", c) | |
167 } | |
168 | |
169 func (s *SwiftHTTPSuite) TestPUTObjectExistsCreated(c *C) { | |
170 data := []byte("test data") | |
171 s.ensureContainer("test", c) | |
172 s.ensureObject("test", "obj", data, c) | |
173 | |
174 newdata := []byte("new test data") | |
175 resp, err := s.sendRequest("PUT", "test/obj", newdata) | |
176 c.Assert(err, IsNil) | |
177 c.Assert(resp.StatusCode, Equals, http.StatusCreated) | |
178 | |
179 s.ensureObjectData("test", "obj", newdata, c) | |
180 s.removeContainer("test", c) | |
181 } | |
182 | |
183 func (s *SwiftHTTPSuite) TestPUTObjectContainerMissingNotFound(c *C) { | |
184 s.ensureNotContainer("test", c) | |
185 | |
186 data := []byte("test data") | |
187 resp, err := s.sendRequest("PUT", "test/obj", data) | |
188 c.Assert(err, IsNil) | |
189 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
190 | |
191 s.ensureNotContainer("test", c) | |
192 } | |
193 | |
194 func (s *SwiftHTTPSuite) TestGETObjectMissingNotFound(c *C) { | |
195 s.ensureContainer("test", c) | |
196 s.ensureNotObject("test", "obj", c) | |
197 | |
198 resp, err := s.sendRequest("GET", "test/obj", nil) | |
199 c.Assert(err, IsNil) | |
200 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
201 | |
202 s.removeContainer("test", c) | |
203 } | |
204 | |
205 func (s *SwiftHTTPSuite) TestGETObjectContainerMissingNotFound(c *C) { | |
206 s.ensureNotContainer("test", c) | |
207 | |
208 resp, err := s.sendRequest("GET", "test/obj", nil) | |
209 c.Assert(err, IsNil) | |
210 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
211 | |
212 s.ensureNotContainer("test", c) | |
213 } | |
214 | |
215 func (s *SwiftHTTPSuite) TestGETObjectExistsOK(c *C) { | |
216 data := []byte("test data") | |
217 s.ensureContainer("test", c) | |
218 s.ensureObject("test", "obj", data, c) | |
219 | |
220 resp, err := s.sendRequest("GET", "test/obj", nil) | |
221 c.Assert(err, IsNil) | |
222 c.Assert(resp.StatusCode, Equals, http.StatusOK) | |
223 | |
224 s.ensureObjectData("test", "obj", data, c) | |
225 | |
226 defer resp.Body.Close() | |
227 body, err := ioutil.ReadAll(resp.Body) | |
228 c.Assert(err, IsNil) | |
229 c.Assert(body, DeepEquals, data) | |
230 | |
231 s.removeContainer("test", c) | |
232 } | |
233 | |
234 func (s *SwiftHTTPSuite) TestDELETEObjectMissingNotFound(c *C) { | |
235 s.ensureContainer("test", c) | |
236 s.ensureNotObject("test", "obj", c) | |
237 | |
238 resp, err := s.sendRequest("DELETE", "test/obj", nil) | |
239 c.Assert(err, IsNil) | |
240 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
241 | |
242 s.removeContainer("test", c) | |
243 } | |
244 | |
245 func (s *SwiftHTTPSuite) TestDELETEObjectContainerMissingNotFound(c *C) { | |
246 s.ensureNotContainer("test", c) | |
247 | |
248 resp, err := s.sendRequest("DELETE", "test/obj", nil) | |
249 c.Assert(err, IsNil) | |
250 c.Assert(resp.StatusCode, Equals, http.StatusNotFound) | |
251 | |
252 s.ensureNotContainer("test", c) | |
253 } | |
254 | |
255 func (s *SwiftHTTPSuite) TestDELETEObjectExistsNoContent(c *C) { | |
256 data := []byte("test data") | |
257 s.ensureContainer("test", c) | |
258 s.ensureObject("test", "obj", data, c) | |
259 | |
260 resp, err := s.sendRequest("DELETE", "test/obj", nil) | |
261 c.Assert(err, IsNil) | |
262 c.Assert(resp.StatusCode, Equals, http.StatusNoContent) | |
263 | |
264 s.removeContainer("test", c) | |
265 } | |
266 | |
267 func (s *SwiftHTTPSuite) TestUnauthorizedFails(c *C) { | |
268 oldtoken := token | |
269 defer func() { | |
270 token = oldtoken | |
271 }() | |
272 token = "" | |
273 resp, err := s.sendRequest("GET", "test", nil) | |
274 c.Assert(err, IsNil) | |
275 c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized) | |
276 | |
277 token = "invalid" | |
278 resp, err = s.sendRequest("PUT", "test", nil) | |
279 c.Assert(err, IsNil) | |
280 c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized) | |
281 | |
282 resp, err = s.sendRequest("DELETE", "test", nil) | |
283 c.Assert(err, IsNil) | |
284 c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized) | |
285 } | |
OLD | NEW |