blob: a1b30c90115e09584a02d1ac9815ac40e3079ddb [file] [log] [blame]
Jamie Hannaford69f13f22014-11-04 15:55:18 +01001package acl
2
3import (
4 "fmt"
5 "net/http"
6 "strconv"
7 "testing"
8
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func _rootURL(lbID int) string {
14 return "/loadbalancers/" + strconv.Itoa(lbID) + "/accesslist"
15}
16
17func mockListResponse(t *testing.T, id int) {
18 th.Mux.HandleFunc(_rootURL(id), func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21
22 w.Header().Add("Content-Type", "application/json")
23 w.WriteHeader(http.StatusOK)
24
25 fmt.Fprintf(w, `
26{
27 "accessList": [
28 {
29 "address": "206.160.163.21",
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010030 "id": 21,
31 "type": "DENY"
32 },
33 {
34 "address": "206.160.163.22",
35 "id": 22,
36 "type": "DENY"
37 },
38 {
39 "address": "206.160.163.23",
Jamie Hannaford69f13f22014-11-04 15:55:18 +010040 "id": 23,
41 "type": "DENY"
42 },
43 {
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010044 "address": "206.160.163.24",
Jamie Hannaford69f13f22014-11-04 15:55:18 +010045 "id": 24,
46 "type": "DENY"
Jamie Hannaford69f13f22014-11-04 15:55:18 +010047 }
48 ]
49}
50 `)
51 })
52}
53
Jamie Hannafordf84f5fc2014-11-04 16:45:28 +010054func mockCreateResponse(t *testing.T, lbID int) {
55 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford69f13f22014-11-04 15:55:18 +010056 th.TestMethod(t, r, "POST")
57 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
58
59 th.TestJSONRequest(t, r, `
60{
61 "accessList": [
62 {
63 "address": "206.160.163.21",
64 "type": "DENY"
65 },
66 {
67 "address": "206.160.165.11",
68 "type": "DENY"
69 }
70 ]
71}
72 `)
73
74 w.Header().Add("Content-Type", "application/json")
75 w.WriteHeader(http.StatusAccepted)
76 })
77}
78
79func mockDeleteAllResponse(t *testing.T, lbID int) {
80 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
81 th.TestMethod(t, r, "DELETE")
82 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
83 w.WriteHeader(http.StatusOK)
84 })
85}
86
87func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
88 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
89 th.TestMethod(t, r, "DELETE")
90 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
91
92 r.ParseForm()
93
94 for k, v := range ids {
95 fids := r.Form["id"]
96 th.AssertEquals(t, strconv.Itoa(v), fids[k])
97 }
98
99 w.WriteHeader(http.StatusOK)
100 })
101}
102
103func mockDeleteResponse(t *testing.T, lbID, networkID int) {
104 th.Mux.HandleFunc(_rootURL(lbID)+"/"+strconv.Itoa(networkID), func(w http.ResponseWriter, r *http.Request) {
105 th.TestMethod(t, r, "DELETE")
106 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
107 w.WriteHeader(http.StatusOK)
108 })
109}