Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 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 | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 27 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
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, |
| 63 | "id": 1, |
| 64 | "ip_protocol": "TCP", |
| 65 | "ip_range": { |
| 66 | "cidr": "10.10.12.0/24" |
| 67 | }, |
| 68 | "to_port": 80, |
| 69 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5" |
| 70 | } |
| 71 | } |
| 72 | `) |
| 73 | }) |
| 74 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 75 | |
| 76 | func mockGetRuleResponse(t *testing.T, ruleID string) { |
| 77 | url := rootPath + "/" + ruleID |
| 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": { |
| 88 | "id": "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 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 | |
| 101 | func mockDeleteRuleResponse(t *testing.T, ruleID string) { |
| 102 | url := rootPath + "/" + ruleID |
| 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 | } |