github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/defsecrules/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 th "github.com/huaweicloud/golangsdk/testhelper" 9 fake "github.com/huaweicloud/golangsdk/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, 27 "id": "{ruleID}", 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 } 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": "{ruleID}", 64 "ip_protocol": "TCP", 65 "ip_range": { 66 "cidr": "10.10.12.0/24" 67 }, 68 "to_port": 80 69 } 70 } 71 `) 72 }) 73 } 74 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", 84 "from_port": 0, 85 "to_port": 0, 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": { 97 "from_port": 0, 98 "id": "{ruleID}", 99 "ip_protocol": "ICMP", 100 "ip_range": { 101 "cidr": "10.10.12.0/24" 102 }, 103 "to_port": 0 104 } 105 } 106 `) 107 }) 108 } 109 110 func mockGetRuleResponse(t *testing.T, ruleID string) { 111 url := rootPath + "/" + ruleID 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": { 122 "id": "{ruleID}", 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 } 134 135 func mockDeleteRuleResponse(t *testing.T, ruleID string) { 136 url := rootPath + "/" + ruleID 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") 141 w.WriteHeader(http.StatusNoContent) 142 }) 143 }