blob: ec1fd527e8bfad8f669e86e93b0e5d267c24f9b8 [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) {
173 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, `
177 {
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 `)
215 w.Header().Add("Location", "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0")
216 w.WriteHeader(http.StatusAccepted)
217 })
218}
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) {
223 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)
226
227 w.Header().Set("Content-Type", "application/json")
228 w.WriteHeader(http.StatusOK)
229 fmt.Fprintf(w, `
230 {
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 `)
302 })
303}
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) {
308 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, `
312 [
313 {
314 "op": "replace",
315 "path": "/origins/0",
316 "value": {
317 "origin": "44.33.22.11",
318 "port": 80,
319 "ssl": false
320 }
321 },
322 {
323 "op": "add",
324 "path": "/domains/0",
325 "value": {"domain": "added.mocksite4.com"}
326 }
327 ]
328 `)
329 w.Header().Add("Location", "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0")
330 w.WriteHeader(http.StatusAccepted)
331 })
332}
333
334// HandleDeleteCDNServiceSuccessfully creates an HTTP handler at `/services/{id}` on the test handler mux
335// that responds with a `Delete` response.
336func HandleDeleteCDNServiceSuccessfully(t *testing.T) {
337 th.Mux.HandleFunc("/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", func(w http.ResponseWriter, r *http.Request) {
338 th.TestMethod(t, r, "DELETE")
339 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
340 w.WriteHeader(http.StatusAccepted)
341 })
342}