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", |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 30 | "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 Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 40 | "id": 23, |
| 41 | "type": "DENY" |
| 42 | }, |
| 43 | { |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 44 | "address": "206.160.163.24", |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 45 | "id": 24, |
| 46 | "type": "DENY" |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 47 | } |
| 48 | ] |
| 49 | } |
| 50 | `) |
| 51 | }) |
| 52 | } |
| 53 | |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 54 | func mockCreateResponse(t *testing.T, lbID int) { |
| 55 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 56 | 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 | |
| 79 | func 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) |
Jamie Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 83 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 84 | }) |
| 85 | } |
| 86 | |
| 87 | func 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 | |
Jamie Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 99 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 100 | }) |
| 101 | } |
| 102 | |
| 103 | func 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) |
Jamie Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 107 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 108 | }) |
| 109 | } |