blob: c28e492d357a4d1cafb461ad48dc53e8307c50b0 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
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
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
Jamie Hannaford2f226172014-11-25 11:52:25 +010075func mockGetRuleResponse(t *testing.T, ruleID string) {
76 url := rootPath + "/" + ruleID
Jamie Hannaford8031b732014-11-24 12:55:41 +010077 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
78 th.TestMethod(t, r, "GET")
79 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
80
81 w.Header().Add("Content-Type", "application/json")
82 w.WriteHeader(http.StatusOK)
83
84 fmt.Fprintf(w, `
85{
86 "security_group_default_rule": {
Jamie Hannaford2f226172014-11-25 11:52:25 +010087 "id": "{ruleID}",
Jamie Hannaford8031b732014-11-24 12:55:41 +010088 "from_port": 80,
89 "to_port": 80,
90 "ip_protocol": "TCP",
91 "ip_range": {
92 "cidr": "10.10.12.0/24"
93 }
94 }
95}
96 `)
97 })
98}
Jamie Hannaford20e92912014-11-24 13:01:45 +010099
Jamie Hannaford2f226172014-11-25 11:52:25 +0100100func mockDeleteRuleResponse(t *testing.T, ruleID string) {
101 url := rootPath + "/" + ruleID
Jamie Hannaford20e92912014-11-24 13:01:45 +0100102 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
103 th.TestMethod(t, r, "DELETE")
104 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
105 w.Header().Add("Content-Type", "application/json")
Jamie Hannafordddd4c082014-11-24 15:21:07 +0100106 w.WriteHeader(http.StatusNoContent)
Jamie Hannaford20e92912014-11-24 13:01:45 +0100107 })
108}