Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 1 | package acl |
| 2 | |
| 3 | import ( |
| 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 | |
| 13 | func _rootURL(lbID int) string { |
| 14 | return "/loadbalancers/" + strconv.Itoa(lbID) + "/accesslist" |
| 15 | } |
| 16 | |
| 17 | func 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", |
| 30 | "id": 23, |
| 31 | "type": "DENY" |
| 32 | }, |
| 33 | { |
| 34 | "address": "206.160.165.11", |
| 35 | "id": 24, |
| 36 | "type": "DENY" |
| 37 | }, |
| 38 | { |
| 39 | "address": "206.160.163.21", |
| 40 | "id": 25, |
| 41 | "type": "DENY" |
| 42 | }, |
| 43 | { |
| 44 | "address": "206.160.165.11", |
| 45 | "id": 26, |
| 46 | "type": "DENY" |
| 47 | }, |
| 48 | { |
| 49 | "address": "206.160.123.11", |
| 50 | "id": 27, |
| 51 | "type": "DENY" |
| 52 | }, |
| 53 | { |
| 54 | "address": "206.160.122.21", |
| 55 | "id": 28, |
| 56 | "type": "DENY" |
| 57 | }, |
| 58 | { |
| 59 | "address": "206.140.123.11", |
| 60 | "id": 29, |
| 61 | "type": "DENY" |
| 62 | }, |
| 63 | { |
| 64 | "address": "206.140.122.21", |
| 65 | "id": 30, |
| 66 | "type": "DENY" |
| 67 | } |
| 68 | ] |
| 69 | } |
| 70 | `) |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | func mockCreateResponse(t *testing.T) { |
| 75 | th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) { |
| 76 | th.TestMethod(t, r, "POST") |
| 77 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 78 | |
| 79 | th.TestJSONRequest(t, r, ` |
| 80 | { |
| 81 | "accessList": [ |
| 82 | { |
| 83 | "address": "206.160.163.21", |
| 84 | "type": "DENY" |
| 85 | }, |
| 86 | { |
| 87 | "address": "206.160.165.11", |
| 88 | "type": "DENY" |
| 89 | } |
| 90 | ] |
| 91 | } |
| 92 | `) |
| 93 | |
| 94 | w.Header().Add("Content-Type", "application/json") |
| 95 | w.WriteHeader(http.StatusAccepted) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | func mockDeleteAllResponse(t *testing.T, lbID int) { |
| 100 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "DELETE") |
| 102 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 103 | w.WriteHeader(http.StatusOK) |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) { |
| 108 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
| 109 | th.TestMethod(t, r, "DELETE") |
| 110 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 111 | |
| 112 | r.ParseForm() |
| 113 | |
| 114 | for k, v := range ids { |
| 115 | fids := r.Form["id"] |
| 116 | th.AssertEquals(t, strconv.Itoa(v), fids[k]) |
| 117 | } |
| 118 | |
| 119 | w.WriteHeader(http.StatusOK) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | func mockDeleteResponse(t *testing.T, lbID, networkID int) { |
| 124 | th.Mux.HandleFunc(_rootURL(lbID)+"/"+strconv.Itoa(networkID), func(w http.ResponseWriter, r *http.Request) { |
| 125 | th.TestMethod(t, r, "DELETE") |
| 126 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 127 | w.WriteHeader(http.StatusOK) |
| 128 | }) |
| 129 | } |