-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdns_test.go
More file actions
202 lines (163 loc) · 5.69 KB
/
dns_test.go
File metadata and controls
202 lines (163 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package loopia
import (
"io/ioutil"
"net/http"
"testing"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
)
func zoneRecordsHandler(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unexpected error when reading response Body")
}
strBody := string(body[:])
if strings.Contains(strBody, "getZoneRecords") {
byteArray, _ := ioutil.ReadFile("fixtures/zone_records.xml")
fmt.Fprintf(w, string(byteArray[:]))
} else if strings.Contains(strBody, "addZoneRecord") {
byteArray, _ := ioutil.ReadFile("fixtures/ok.xml")
fmt.Fprintf(w, string(byteArray[:]))
}
}
}
func TestClient_GetZoneRecord(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", zoneRecordsHandler(t))
record, _ := client.GetZoneRecord("example.com", "@", 14096733)
assert.Equal(t, int64(14096733), record.ID)
}
func TestClient_AddSubdomain_OK(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
byteArray, _ := ioutil.ReadFile("fixtures/ok.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, _ := client.AddSubdomain("example.com", "test")
assert.Equal(t, "success", result.Status)
}
func TestClient_AddSubdomain_AUTH_ERROR(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
byteArray, _ := ioutil.ReadFile("fixtures/auth_error.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, _ := client.AddSubdomain("example.com", "test")
assert.Equal(t, "failed", result.Status)
assert.Equal(t, "AUTH_ERROR", result.Cause)
}
func TestClient_GetSubdomains(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
byteArray, _ := ioutil.ReadFile("fixtures/subdomains.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, _ := client.GetSubdomains("example.com")
assert.Equal(t, 8, len(result), "Expected result to have 8 items")
}
func TestClient_GetSubdomain(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
byteArray, _ := ioutil.ReadFile("fixtures/subdomains.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, _ := client.GetSubdomain("example.com", "www")
assert.Equal(t, "www", result.Name, "Expected result equal www")
}
func TestClient_AddZoneRecord(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", zoneRecordsHandler(t))
record := Record{
TTL: 300,
Type: "A",
Value: "1.1.1.1",
Priority: 0,
}
err := client.AddZoneRecord("example.com", "api", &record)
if err != nil {
t.Error(err)
}
assert.Equal(t, int64(14096733), record.ID, "AddZoneRecord expects to find ID")
}
func TestClient_RemoveZoneRecord(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unexpected error when reading response Body")
}
search := []string{
"<param><value><string>example.com</string></value></param>",
"<param><value><string>api</string></value></param>",
"<param><value><int>14096733</int></value></param>",
}
assert.Contains(t, string(body[:]), strings.Join(search[:], ""), "Parameters should be in exact order")
byteArray, _ := ioutil.ReadFile("fixtures/ok.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, err := client.RemoveZoneRecord("example.com", "api", 14096733)
assert.Equal(t, nil, err)
assert.Equal(t, "success", result.Status)
}
func TestClient_UpdateZoneRecord(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unexpected error when reading response Body")
}
assert.Contains(t,
string(body[:]),
"<member><name>record_id</name><value><int>14096733</int></value></member>",
"ID should be converted to record_id")
byteArray, _ := ioutil.ReadFile("fixtures/ok.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, err := client.UpdateZoneRecord("example.com", "api", Record{
ID: 14096733,
TTL: 300,
Type: "A",
Value: "1.1.1.1",
Priority: 0,
})
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "success", result.Status)
}
func TestClient_RemoveSubDomain(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error("Unexpected error when reading response Body")
}
search := []string{
"<param><value><string>example.com</string></value></param>",
"<param><value><string>api</string></value></param>",
}
assert.Contains(t, string(body[:]), strings.Join(search[:], ""), "Parameters should be in exact order")
byteArray, _ := ioutil.ReadFile("fixtures/ok.xml")
fmt.Fprintf(w, string(byteArray[:]))
})
result, err := client.RemoveSubDomain("example.com", "api")
assert.Equal(t, nil, err)
assert.Equal(t, "success", result.Status)
}