Keith Byrne | bda4859 | 2016-03-23 11:37:08 +0000 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 3 | package acl |
| 4 | |
| 5 | import ( |
| 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 | |
| 15 | func _rootURL(lbID int) string { |
| 16 | return "/loadbalancers/" + strconv.Itoa(lbID) + "/accesslist" |
| 17 | } |
| 18 | |
| 19 | func 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 Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 32 | "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 Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 42 | "id": 23, |
| 43 | "type": "DENY" |
| 44 | }, |
| 45 | { |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 46 | "address": "206.160.163.24", |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 47 | "id": 24, |
| 48 | "type": "DENY" |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 49 | } |
| 50 | ] |
| 51 | } |
| 52 | `) |
| 53 | }) |
| 54 | } |
| 55 | |
Jamie Hannaford | f84f5fc | 2014-11-04 16:45:28 +0100 | [diff] [blame] | 56 | func mockCreateResponse(t *testing.T, lbID int) { |
| 57 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 58 | 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 | |
| 81 | func 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 Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 85 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 86 | }) |
| 87 | } |
| 88 | |
| 89 | func 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 Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 101 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 102 | }) |
| 103 | } |
| 104 | |
| 105 | func 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 Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 109 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 69f13f2 | 2014-11-04 15:55:18 +0100 | [diff] [blame] | 110 | }) |
| 111 | } |