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