blob: d9bc9f20b7785bfe9c7e38551695c44f7a51d3f7 [file] [log] [blame]
Jon Perritt9da13a12015-01-20 19:25:15 -07001package services
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12// HandleListCDNServiceSuccessfully creates an HTTP handler at `/services` on the test handler mux
13// that responds with a `List` response.
14func HandleListCDNServiceSuccessfully(t *testing.T) {
15 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
16 th.TestMethod(t, r, "GET")
17 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
18
19 w.Header().Set("Content-Type", "application/json")
20 w.WriteHeader(http.StatusOK)
21
22 r.ParseForm()
23 marker := r.Form.Get("marker")
24 switch marker {
25 case "":
26 fmt.Fprintf(w, `
27 {
28 "links": [
29 {
30 "rel": "next",
31 "href": "https://www.poppycdn.io/v1.0/services?marker=96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0&limit=20"
32 }
33 ],
34 "services": [
35 {
36 "id": "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
37 "name": "mywebsite.com",
38 "domains": [
39 {
40 "domain": "www.mywebsite.com"
41 }
42 ],
43 "origins": [
44 {
45 "origin": "mywebsite.com",
46 "port": 80,
47 "ssl": false
48 }
49 ],
50 "caching": [
51 {
52 "name": "default",
53 "ttl": 3600
54 },
55 {
56 "name": "home",
57 "ttl": 17200,
58 "rules": [
59 {
60 "name": "index",
61 "request_url": "/index.htm"
62 }
63 ]
64 },
65 {
66 "name": "images",
67 "ttl": 12800,
68 "rules": [
69 {
70 "name": "images",
71 "request_url": "*.png"
72 }
73 ]
74 }
75 ],
76 "restrictions": [
77 {
78 "name": "website only",
79 "rules": [
80 {
81 "name": "mywebsite.com",
82 "referrer": "www.mywebsite.com"
83 }
84 ]
85 }
86 ],
87 "flavor_id": "asia",
88 "status": "deployed",
89 "errors" : [],
90 "links": [
91 {
92 "href": "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
93 "rel": "self"
94 },
95 {
96 "href": "mywebsite.com.cdn123.poppycdn.net",
97 "rel": "access_url"
98 },
99 {
100 "href": "https://www.poppycdn.io/v1.0/flavors/asia",
101 "rel": "flavor"
102 }
103 ]
104 },
105 {
106 "id": "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
107 "name": "myothersite.com",
108 "domains": [
109 {
110 "domain": "www.myothersite.com"
111 }
112 ],
113 "origins": [
114 {
115 "origin": "44.33.22.11",
116 "port": 80,
117 "ssl": false
118 },
119 {
120 "origin": "77.66.55.44",
121 "port": 80,
122 "ssl": false,
123 "rules": [
124 {
125 "name": "videos",
126 "request_url": "^/videos/*.m3u"
127 }
128 ]
129 }
130 ],
131 "caching": [
132 {
133 "name": "default",
134 "ttl": 3600
135 }
136 ],
137 "restrictions": [
138 {}
139 ],
140 "flavor_id": "europe",
141 "status": "deployed",
142 "links": [
143 {
144 "href": "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
145 "rel": "self"
146 },
147 {
148 "href": "myothersite.com.poppycdn.net",
149 "rel": "access_url"
150 },
151 {
152 "href": "https://www.poppycdn.io/v1.0/flavors/europe",
153 "rel": "flavor"
154 }
155 ]
156 }
157 ]
158 }
159 `)
160 case "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1":
161 fmt.Fprintf(w, `{
162 "services": []
163 }`)
164 default:
165 t.Fatalf("Unexpected marker: [%s]", marker)
166 }
167 })
168}
169
170// HandleCreateCDNServiceSuccessfully creates an HTTP handler at `/services` on the test handler mux
171// that responds with a `Create` response.
172func HandleCreateCDNServiceSuccessfully(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500173 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
174 th.TestMethod(t, r, "POST")
175 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
176 th.TestJSONRequest(t, r, `
Jon Perritt9da13a12015-01-20 19:25:15 -0700177 {
178 "name": "mywebsite.com",
179 "domains": [
180 {
181 "domain": "www.mywebsite.com"
182 },
183 {
184 "domain": "blog.mywebsite.com"
185 }
186 ],
187 "origins": [
188 {
189 "origin": "mywebsite.com",
190 "port": 80,
191 "ssl": false
192 }
193 ],
194 "restrictions": [
195 {
196 "name": "website only",
197 "rules": [
198 {
199 "name": "mywebsite.com",
200 "referrer": "www.mywebsite.com"
201 }
202 ]
203 }
204 ],
205 "caching": [
206 {
207 "name": "default",
208 "ttl": 3600
209 }
210 ],
211
212 "flavor_id": "cdn"
213 }
214 `)
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500215 w.Header().Add("Location", "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0")
216 w.WriteHeader(http.StatusAccepted)
217 })
Jon Perritt9da13a12015-01-20 19:25:15 -0700218}
219
220// HandleGetCDNServiceSuccessfully creates an HTTP handler at `/services/{id}` on the test handler mux
221// that responds with a `Get` response.
222func HandleGetCDNServiceSuccessfully(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500223 th.Mux.HandleFunc("/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", func(w http.ResponseWriter, r *http.Request) {
224 th.TestMethod(t, r, "GET")
225 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jon Perritt9da13a12015-01-20 19:25:15 -0700226
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500227 w.Header().Set("Content-Type", "application/json")
228 w.WriteHeader(http.StatusOK)
229 fmt.Fprintf(w, `
Jon Perritt9da13a12015-01-20 19:25:15 -0700230 {
231 "id": "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
232 "name": "mywebsite.com",
233 "domains": [
234 {
235 "domain": "www.mywebsite.com",
236 "protocol": "http"
237 }
238 ],
239 "origins": [
240 {
241 "origin": "mywebsite.com",
242 "port": 80,
243 "ssl": false
244 }
245 ],
246 "caching": [
247 {
248 "name": "default",
249 "ttl": 3600
250 },
251 {
252 "name": "home",
253 "ttl": 17200,
254 "rules": [
255 {
256 "name": "index",
257 "request_url": "/index.htm"
258 }
259 ]
260 },
261 {
262 "name": "images",
263 "ttl": 12800,
264 "rules": [
265 {
266 "name": "images",
267 "request_url": "*.png"
268 }
269 ]
270 }
271 ],
272 "restrictions": [
273 {
274 "name": "website only",
275 "rules": [
276 {
277 "name": "mywebsite.com",
278 "referrer": "www.mywebsite.com"
279 }
280 ]
281 }
282 ],
283 "flavor_id": "cdn",
284 "status": "deployed",
285 "errors" : [],
286 "links": [
287 {
288 "href": "https://global.cdn.api.rackspacecloud.com/v1.0/110011/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
289 "rel": "self"
290 },
291 {
292 "href": "blog.mywebsite.com.cdn1.raxcdn.com",
293 "rel": "access_url"
294 },
295 {
296 "href": "https://global.cdn.api.rackspacecloud.com/v1.0/110011/flavors/cdn",
297 "rel": "flavor"
298 }
299 ]
300 }
301 `)
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500302 })
Jon Perritt9da13a12015-01-20 19:25:15 -0700303}
304
305// HandleUpdateCDNServiceSuccessfully creates an HTTP handler at `/services/{id}` on the test handler mux
306// that responds with a `Update` response.
307func HandleUpdateCDNServiceSuccessfully(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500308 th.Mux.HandleFunc("/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", func(w http.ResponseWriter, r *http.Request) {
309 th.TestMethod(t, r, "PATCH")
310 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
311 th.TestJSONRequest(t, r, `
Jon Perritt9da13a12015-01-20 19:25:15 -0700312 [
Ash Wilson163e4592015-01-29 12:03:28 -0500313 {
314 "op": "add",
315 "path": "/domains/-",
316 "value": {"domain": "appended.mocksite4.com"}
317 },
318 {
319 "op": "add",
320 "path": "/domains/4",
321 "value": {"domain": "inserted.mocksite4.com"}
322 },
323 {
324 "op": "add",
325 "path": "/domains",
326 "value": [
327 {"domain": "bulkadded1.mocksite4.com"},
328 {"domain": "bulkadded2.mocksite4.com"}
329 ]
330 },
331 {
332 "op": "replace",
333 "path": "/origins/2",
334 "value": {"origin": "44.33.22.11", "port": 80, "ssl": false}
335 },
336 {
337 "op": "replace",
338 "path": "/origins",
339 "value": [
340 {"origin": "44.33.22.11", "port": 80, "ssl": false},
341 {"origin": "55.44.33.22", "port": 443, "ssl": true}
342 ]
343 },
344 {
345 "op": "remove",
346 "path": "/caching/8"
Ash Wilsond842ae62015-01-29 13:11:50 -0500347 },
348 {
349 "op": "remove",
350 "path": "/caching"
351 },
352 {
353 "op": "replace",
354 "path": "/name",
355 "value": "differentServiceName"
Ash Wilson163e4592015-01-29 12:03:28 -0500356 }
Jon Perritt9da13a12015-01-20 19:25:15 -0700357 ]
358 `)
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500359 w.Header().Add("Location", "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0")
360 w.WriteHeader(http.StatusAccepted)
361 })
Jon Perritt9da13a12015-01-20 19:25:15 -0700362}
363
364// HandleDeleteCDNServiceSuccessfully creates an HTTP handler at `/services/{id}` on the test handler mux
365// that responds with a `Delete` response.
366func HandleDeleteCDNServiceSuccessfully(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500367 th.Mux.HandleFunc("/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", func(w http.ResponseWriter, r *http.Request) {
368 th.TestMethod(t, r, "DELETE")
369 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
370 w.WriteHeader(http.StatusAccepted)
371 })
Jon Perritt9da13a12015-01-20 19:25:15 -0700372}