blob: 4eaef0ec18920dc71c45c398347c820c188d3a3a [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jamie Hannaford17d2f872014-11-24 12:20:33 +01002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02008 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
9 fake "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
Jamie Hannaford17d2f872014-11-24 12:20:33 +010010)
11
12const rootPath = "/os-security-group-default-rules"
13
14func 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 Hannaford2f226172014-11-25 11:52:25 +010027 "id": "{ruleID}",
Jamie Hannaford17d2f872014-11-24 12:20:33 +010028 "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 Hannaford43fa4a22014-11-24 12:49:17 +010039
40func 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 Hannaford2f226172014-11-25 11:52:25 +010063 "id": "{ruleID}",
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010064 "ip_protocol": "TCP",
65 "ip_range": {
66 "cidr": "10.10.12.0/24"
67 },
Jamie Hannaford558572f2014-11-24 14:31:57 +010068 "to_port": 80
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010069 }
70}
71`)
72 })
73}
Jamie Hannaford8031b732014-11-24 12:55:41 +010074
Joe Topjian4f9dce22016-02-28 00:03:37 +000075func 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",
jrperritte0ba1052016-04-13 17:19:54 -050084 "from_port": 0,
85 "to_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +000086 "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": {
jrperritte0ba1052016-04-13 17:19:54 -050097 "from_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +000098 "id": "{ruleID}",
99 "ip_protocol": "ICMP",
100 "ip_range": {
101 "cidr": "10.10.12.0/24"
102 },
jrperritte0ba1052016-04-13 17:19:54 -0500103 "to_port": 0
Joe Topjian4f9dce22016-02-28 00:03:37 +0000104 }
105}
106`)
107 })
108}
109
Jamie Hannaford2f226172014-11-25 11:52:25 +0100110func mockGetRuleResponse(t *testing.T, ruleID string) {
111 url := rootPath + "/" + ruleID
Jamie Hannaford8031b732014-11-24 12:55:41 +0100112 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 Hannaford2f226172014-11-25 11:52:25 +0100122 "id": "{ruleID}",
Jamie Hannaford8031b732014-11-24 12:55:41 +0100123 "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 Hannaford20e92912014-11-24 13:01:45 +0100134
Jamie Hannaford2f226172014-11-25 11:52:25 +0100135func mockDeleteRuleResponse(t *testing.T, ruleID string) {
136 url := rootPath + "/" + ruleID
Jamie Hannaford20e92912014-11-24 13:01:45 +0100137 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 Hannafordddd4c082014-11-24 15:21:07 +0100141 w.WriteHeader(http.StatusNoContent)
Jamie Hannaford20e92912014-11-24 13:01:45 +0100142 })
143}