/
powerdns_test.go
244 lines (218 loc) · 6.76 KB
/
powerdns_test.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package powerdns
import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"github.com/jarcoal/httpmock"
)
const (
testBaseURL string = "http://localhost:8080"
testVHost string = "localhost"
testAPIKey string = "apipw"
)
func generateTestAPIURL() string {
return fmt.Sprintf("%s/api/v1", testBaseURL)
}
func generateTestAPIVHostURL() string {
return fmt.Sprintf("%s/servers/%s", generateTestAPIURL(), testVHost)
}
func verifyAPIKey(req *http.Request) *http.Response {
if req.Header.Get("X-Api-Key") != testAPIKey {
return httpmock.NewStringResponse(http.StatusUnauthorized, "Unauthorized")
}
return nil
}
func initialisePowerDNSTestClient() *Client {
return NewClient(testBaseURL, testVHost, map[string]string{"X-API-Key": testAPIKey}, nil)
}
func registerDoMockResponder() {
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/servers/doesntExist", generateTestAPIURL()),
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
return httpmock.NewStringResponse(http.StatusNotFound, "Not Found"), nil
},
)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/servers/localhost", generateTestAPIURL()),
func(req *http.Request) (*http.Response, error) {
return verifyAPIKey(req), nil
},
)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/server", generateTestAPIURL()),
func(req *http.Request) (*http.Response, error) {
mock := Error{
Status: "Not Found",
StatusCode: http.StatusNotFound,
Message: "Not Found",
}
return httpmock.NewJsonResponse(http.StatusNotImplemented, mock)
},
)
}
func TestNewClient(t *testing.T) {
t.Run("TestValidURL", func(t *testing.T) {
tmpl := &Client{"http", "localhost", "8080", "localhost", map[string]string{"X-API-Key": "apipw"}, http.DefaultClient, service{}, nil, nil, nil, nil, nil, nil, nil}
p := NewClient("http://localhost:8080", "localhost", map[string]string{"X-API-Key": "apipw"}, http.DefaultClient)
if p.Hostname != tmpl.Hostname {
t.Error("NewClient returns invalid Client object")
}
})
t.Run("TestInvalidURL", func(t *testing.T) {
originalLogFatalf := logFatalf
defer func() {
logFatalf = originalLogFatalf
}()
var errors []string
logFatalf = func(format string, args ...interface{}) {
if len(args) > 0 {
errors = append(errors, fmt.Sprintf(format, args))
} else {
errors = append(errors, format)
}
}
_ = NewClient("http://1.2:foo", "localhost", map[string]string{"X-API-Key": "apipw"}, http.DefaultClient)
if len(errors) < 1 {
t.Error("NewClient does not exit with fatal error")
}
})
}
func TestNewRequest(t *testing.T) {
p := initialisePowerDNSTestClient()
t.Run("TestValidRequest", func(t *testing.T) {
if _, err := p.newRequest(context.Background(), "GET", "servers", nil, nil); err != nil {
t.Error("error is not nil")
}
})
}
func TestDo(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerDoMockResponder()
t.Run("TestStringErrorResponse", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "servers/doesntExist", nil, nil)
if _, err := p.do(req, nil); err == nil {
t.Error("err is nil")
}
})
t.Run("Test401Handling", func(t *testing.T) {
p := initialisePowerDNSTestClient()
p.Headers = nil
req, _ := p.newRequest(context.Background(), "GET", "servers/localhost", nil, nil)
if _, err := p.do(req, nil); err.Error() != "Unauthorized" {
t.Error("401 response does not result into an error with correct message.")
}
})
t.Run("Test404Handling", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "servers/doesntExist", nil, nil)
if _, err := p.do(req, nil); err.Error() != "Not Found" {
t.Error("404 response does not result into an error with correct message.")
}
})
t.Run("TestJSONResponseHandling", func(t *testing.T) {
p := initialisePowerDNSTestClient()
req, _ := p.newRequest(context.Background(), "GET", "server", nil, &Server{})
if _, err := p.do(req, nil); err.Error() != "Not Found" {
t.Error("501 JSON response does not result into an error with correct message.")
}
})
}
func TestParseBaseURL(t *testing.T) {
testCases := []struct {
baseURL string
wantScheme string
wantHostname string
wantPort string
wantError bool
}{
{"https://example.com", "https", "example.com", "443", false},
{"http://example.com", "http", "example.com", "80", false},
{"https://example.com:8080", "https", "example.com", "8080", false},
{"http://example.com:8080", "http", "example.com", "8080", false},
{"http%%%foo", "http", "", "", true},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TestCase%d", i), func(t *testing.T) {
scheme, hostname, port, err := parseBaseURL(tc.baseURL)
if err != nil && tc.wantError == true {
return
}
if err != nil && tc.wantError == false {
t.Error("Error was returned unexpectedly")
}
if err == nil && tc.wantError == true {
t.Error("No error was returned")
}
if scheme != tc.wantScheme {
t.Errorf("Scheme parsing failed: %s != %s", scheme, tc.wantScheme)
}
if hostname != tc.wantHostname {
t.Errorf("Hostname parsing failed: %s != %s", hostname, tc.wantHostname)
}
if port != tc.wantPort {
t.Errorf("Port parsing failed: %s != %s", port, tc.wantPort)
}
})
}
}
func TestParseVHost(t *testing.T) {
testCases := []struct {
vHost string
wantVHost string
}{
{"example.com", "example.com"},
{"", "localhost"},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TestCase%d", i), func(t *testing.T) {
if parseVHost(tc.vHost) != tc.wantVHost {
t.Error("parseVHost returned an invalid value")
}
})
}
}
func TestGenerateAPIURL(t *testing.T) {
tmpl := "https://localhost:8080/api/v1/foo?a=b"
query := url.Values{}
query.Add("a", "b")
g := generateAPIURL("https", "localhost", "8080", "foo", &query)
if tmpl != g.String() {
t.Errorf("Template does not match generated API URL: %s", g.String())
}
}
func TestTrimDomain(t *testing.T) {
testCases := []struct {
domain string
wantDomain string
}{
{"example.com.", "example.com"},
{"example.com", "example.com"},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TestCase%d", i), func(t *testing.T) {
if trimDomain(tc.domain) != tc.wantDomain {
t.Error("trimDomain returned an invalid value")
}
})
}
}
func TestMakeDomainCanonical(t *testing.T) {
testCases := []struct {
domain string
wantDomain string
}{
{"example.com.", "example.com."},
{"example.com", "example.com."},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TestCase%d", i), func(t *testing.T) {
if makeDomainCanonical(tc.domain) != tc.wantDomain {
t.Error("makeDomainCanonical returned an invalid value")
}
})
}
}