Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 6 | "strconv" |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 7 | "testing" |
| 8 | |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | const rootPath = "/os-security-group-default-rules" |
| 14 | |
| 15 | func mockListRulesResponse(t *testing.T) { |
| 16 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 17 | th.TestMethod(t, r, "GET") |
| 18 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 19 | |
| 20 | w.Header().Add("Content-Type", "application/json") |
| 21 | w.WriteHeader(http.StatusOK) |
| 22 | |
| 23 | fmt.Fprintf(w, ` |
| 24 | { |
| 25 | "security_group_default_rules": [ |
| 26 | { |
| 27 | "from_port": 80, |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 28 | "id": 1, |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 29 | "ip_protocol": "TCP", |
| 30 | "ip_range": { |
| 31 | "cidr": "10.10.10.0/24" |
| 32 | }, |
| 33 | "to_port": 80 |
| 34 | } |
| 35 | ] |
| 36 | } |
| 37 | `) |
| 38 | }) |
| 39 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 40 | |
| 41 | func mockCreateRuleResponse(t *testing.T) { |
| 42 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 43 | th.TestMethod(t, r, "POST") |
| 44 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 45 | |
| 46 | th.TestJSONRequest(t, r, ` |
| 47 | { |
| 48 | "security_group_default_rule": { |
| 49 | "ip_protocol": "TCP", |
| 50 | "from_port": 80, |
| 51 | "to_port": 80, |
| 52 | "cidr": "10.10.12.0/24" |
| 53 | } |
| 54 | } |
| 55 | `) |
| 56 | |
| 57 | w.Header().Add("Content-Type", "application/json") |
| 58 | w.WriteHeader(http.StatusOK) |
| 59 | |
| 60 | fmt.Fprintf(w, ` |
| 61 | { |
| 62 | "security_group_default_rule": { |
| 63 | "from_port": 80, |
| 64 | "id": 1, |
| 65 | "ip_protocol": "TCP", |
| 66 | "ip_range": { |
| 67 | "cidr": "10.10.12.0/24" |
| 68 | }, |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 69 | "to_port": 80 |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | `) |
| 73 | }) |
| 74 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 75 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 76 | func mockGetRuleResponse(t *testing.T, ruleID int) { |
| 77 | url := rootPath + "/" + strconv.Itoa(ruleID) |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 78 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 79 | th.TestMethod(t, r, "GET") |
| 80 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 81 | |
| 82 | w.Header().Add("Content-Type", "application/json") |
| 83 | w.WriteHeader(http.StatusOK) |
| 84 | |
| 85 | fmt.Fprintf(w, ` |
| 86 | { |
| 87 | "security_group_default_rule": { |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 88 | "id": 1, |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 89 | "from_port": 80, |
| 90 | "to_port": 80, |
| 91 | "ip_protocol": "TCP", |
| 92 | "ip_range": { |
| 93 | "cidr": "10.10.12.0/24" |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | `) |
| 98 | }) |
| 99 | } |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 100 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 101 | func mockDeleteRuleResponse(t *testing.T, ruleID int) { |
| 102 | url := rootPath + "/" + strconv.Itoa(ruleID) |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 103 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 104 | th.TestMethod(t, r, "DELETE") |
| 105 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 106 | w.Header().Add("Content-Type", "application/json") |
| 107 | w.WriteHeader(http.StatusAccepted) |
| 108 | }) |
| 109 | } |