Keith Byrne | bda4859 | 2016-03-23 11:37:08 +0000 | [diff] [blame^] | 1 | // +build fixtures |
| 2 | |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 3 | package defsecrules |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | const rootPath = "/os-security-group-default-rules" |
| 15 | |
| 16 | func mockListRulesResponse(t *testing.T) { |
| 17 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "GET") |
| 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | w.WriteHeader(http.StatusOK) |
| 23 | |
| 24 | fmt.Fprintf(w, ` |
| 25 | { |
| 26 | "security_group_default_rules": [ |
| 27 | { |
| 28 | "from_port": 80, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 29 | "id": "{ruleID}", |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 30 | "ip_protocol": "TCP", |
| 31 | "ip_range": { |
| 32 | "cidr": "10.10.10.0/24" |
| 33 | }, |
| 34 | "to_port": 80 |
| 35 | } |
| 36 | ] |
| 37 | } |
| 38 | `) |
| 39 | }) |
| 40 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 41 | |
| 42 | func mockCreateRuleResponse(t *testing.T) { |
| 43 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 44 | th.TestMethod(t, r, "POST") |
| 45 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 46 | |
| 47 | th.TestJSONRequest(t, r, ` |
| 48 | { |
| 49 | "security_group_default_rule": { |
| 50 | "ip_protocol": "TCP", |
| 51 | "from_port": 80, |
| 52 | "to_port": 80, |
| 53 | "cidr": "10.10.12.0/24" |
| 54 | } |
| 55 | } |
| 56 | `) |
| 57 | |
| 58 | w.Header().Add("Content-Type", "application/json") |
| 59 | w.WriteHeader(http.StatusOK) |
| 60 | |
| 61 | fmt.Fprintf(w, ` |
| 62 | { |
| 63 | "security_group_default_rule": { |
| 64 | "from_port": 80, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 65 | "id": "{ruleID}", |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 66 | "ip_protocol": "TCP", |
| 67 | "ip_range": { |
| 68 | "cidr": "10.10.12.0/24" |
| 69 | }, |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 70 | "to_port": 80 |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | `) |
| 74 | }) |
| 75 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 76 | |
Joe Topjian | 4f9dce2 | 2016-02-28 00:03:37 +0000 | [diff] [blame] | 77 | func mockCreateRuleResponseICMPZero(t *testing.T) { |
| 78 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 79 | th.TestMethod(t, r, "POST") |
| 80 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 81 | |
| 82 | th.TestJSONRequest(t, r, ` |
| 83 | { |
| 84 | "security_group_default_rule": { |
| 85 | "ip_protocol": "ICMP", |
| 86 | "from_port": 0, |
| 87 | "to_port": 0, |
| 88 | "cidr": "10.10.12.0/24" |
| 89 | } |
| 90 | } |
| 91 | `) |
| 92 | |
| 93 | w.Header().Add("Content-Type", "application/json") |
| 94 | w.WriteHeader(http.StatusOK) |
| 95 | |
| 96 | fmt.Fprintf(w, ` |
| 97 | { |
| 98 | "security_group_default_rule": { |
| 99 | "from_port": 0, |
| 100 | "id": "{ruleID}", |
| 101 | "ip_protocol": "ICMP", |
| 102 | "ip_range": { |
| 103 | "cidr": "10.10.12.0/24" |
| 104 | }, |
| 105 | "to_port": 0 |
| 106 | } |
| 107 | } |
| 108 | `) |
| 109 | }) |
| 110 | } |
| 111 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 112 | func mockGetRuleResponse(t *testing.T, ruleID string) { |
| 113 | url := rootPath + "/" + ruleID |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 114 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 115 | th.TestMethod(t, r, "GET") |
| 116 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 117 | |
| 118 | w.Header().Add("Content-Type", "application/json") |
| 119 | w.WriteHeader(http.StatusOK) |
| 120 | |
| 121 | fmt.Fprintf(w, ` |
| 122 | { |
| 123 | "security_group_default_rule": { |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 124 | "id": "{ruleID}", |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 125 | "from_port": 80, |
| 126 | "to_port": 80, |
| 127 | "ip_protocol": "TCP", |
| 128 | "ip_range": { |
| 129 | "cidr": "10.10.12.0/24" |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | `) |
| 134 | }) |
| 135 | } |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 136 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 137 | func mockDeleteRuleResponse(t *testing.T, ruleID string) { |
| 138 | url := rootPath + "/" + ruleID |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 139 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 140 | th.TestMethod(t, r, "DELETE") |
| 141 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 142 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | ddd4c08 | 2014-11-24 15:21:07 +0100 | [diff] [blame] | 143 | w.WriteHeader(http.StatusNoContent) |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 144 | }) |
| 145 | } |