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