blob: 9b3c2b6d54d84f82c1eacb26c7ae7d7143b99ed8 [file] [log] [blame]
Keith Byrnebda48592016-03-23 11:37:08 +00001// +build fixtures
2
Jamie Hannaford17d2f872014-11-24 12:20:33 +01003package defsecrules
4
5import (
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
14const rootPath = "/os-security-group-default-rules"
15
16func 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 Hannaford2f226172014-11-25 11:52:25 +010029 "id": "{ruleID}",
Jamie Hannaford17d2f872014-11-24 12:20:33 +010030 "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 Hannaford43fa4a22014-11-24 12:49:17 +010041
42func 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 Hannaford2f226172014-11-25 11:52:25 +010065 "id": "{ruleID}",
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010066 "ip_protocol": "TCP",
67 "ip_range": {
68 "cidr": "10.10.12.0/24"
69 },
Jamie Hannaford558572f2014-11-24 14:31:57 +010070 "to_port": 80
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010071 }
72}
73`)
74 })
75}
Jamie Hannaford8031b732014-11-24 12:55:41 +010076
Joe Topjian4f9dce22016-02-28 00:03:37 +000077func 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 Hannaford2f226172014-11-25 11:52:25 +0100112func mockGetRuleResponse(t *testing.T, ruleID string) {
113 url := rootPath + "/" + ruleID
Jamie Hannaford8031b732014-11-24 12:55:41 +0100114 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 Hannaford2f226172014-11-25 11:52:25 +0100124 "id": "{ruleID}",
Jamie Hannaford8031b732014-11-24 12:55:41 +0100125 "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 Hannaford20e92912014-11-24 13:01:45 +0100136
Jamie Hannaford2f226172014-11-25 11:52:25 +0100137func mockDeleteRuleResponse(t *testing.T, ruleID string) {
138 url := rootPath + "/" + ruleID
Jamie Hannaford20e92912014-11-24 13:01:45 +0100139 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 Hannafordddd4c082014-11-24 15:21:07 +0100143 w.WriteHeader(http.StatusNoContent)
Jamie Hannaford20e92912014-11-24 13:01:45 +0100144 })
145}