blob: 4fe22110673a8eed19993d29f04de3bb3126fb19 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
4 "fmt"
5 "net/http"
Jamie Hannaford558572f2014-11-24 14:31:57 +01006 "strconv"
Jamie Hannaford17d2f872014-11-24 12:20:33 +01007 "testing"
8
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13const rootPath = "/os-security-group-default-rules"
14
15func mockListRulesResponse(t *testing.T) {
16 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
17 th.TestMethod(t, r, "GET")
18 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
19
20 w.Header().Add("Content-Type", "application/json")
21 w.WriteHeader(http.StatusOK)
22
23 fmt.Fprintf(w, `
24{
25 "security_group_default_rules": [
26 {
27 "from_port": 80,
Jamie Hannaford558572f2014-11-24 14:31:57 +010028 "id": 1,
Jamie Hannaford17d2f872014-11-24 12:20:33 +010029 "ip_protocol": "TCP",
30 "ip_range": {
31 "cidr": "10.10.10.0/24"
32 },
33 "to_port": 80
34 }
35 ]
36}
37 `)
38 })
39}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010040
41func mockCreateRuleResponse(t *testing.T) {
42 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
43 th.TestMethod(t, r, "POST")
44 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
45
46 th.TestJSONRequest(t, r, `
47{
48 "security_group_default_rule": {
49 "ip_protocol": "TCP",
50 "from_port": 80,
51 "to_port": 80,
52 "cidr": "10.10.12.0/24"
53 }
54}
55 `)
56
57 w.Header().Add("Content-Type", "application/json")
58 w.WriteHeader(http.StatusOK)
59
60 fmt.Fprintf(w, `
61{
62 "security_group_default_rule": {
63 "from_port": 80,
64 "id": 1,
65 "ip_protocol": "TCP",
66 "ip_range": {
67 "cidr": "10.10.12.0/24"
68 },
Jamie Hannaford558572f2014-11-24 14:31:57 +010069 "to_port": 80
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010070 }
71}
72`)
73 })
74}
Jamie Hannaford8031b732014-11-24 12:55:41 +010075
Jamie Hannaford558572f2014-11-24 14:31:57 +010076func mockGetRuleResponse(t *testing.T, ruleID int) {
77 url := rootPath + "/" + strconv.Itoa(ruleID)
Jamie Hannaford8031b732014-11-24 12:55:41 +010078 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": {
Jamie Hannaford558572f2014-11-24 14:31:57 +010088 "id": 1,
Jamie Hannaford8031b732014-11-24 12:55:41 +010089 "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 Hannaford20e92912014-11-24 13:01:45 +0100100
Jamie Hannaford558572f2014-11-24 14:31:57 +0100101func mockDeleteRuleResponse(t *testing.T, ruleID int) {
102 url := rootPath + "/" + strconv.Itoa(ruleID)
Jamie Hannaford20e92912014-11-24 13:01:45 +0100103 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}