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