-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzonefile_test.go
161 lines (133 loc) · 3.1 KB
/
zonefile_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
package czds_test
import (
"compress/gzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"path"
"strings"
"testing"
. "github.com/t94j0/go-czds"
)
const ExampleZone string = "test.com 86400 in a 127.0.0.1\n"
func makeZoneServer(t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
TargetPath := "/czds/downloads/"
if !strings.HasPrefix(r.URL.Path, TargetPath) {
http.NotFound(w, r)
return
}
if r.Header.Get("Authorization") != "Bearer "+TargetAccessToken {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
requestedZone := strings.TrimPrefix(r.URL.Path, TargetPath)
switch requestedZone {
case "badrequest.zone":
http.Error(w, "malformed request", http.StatusBadRequest)
return
case "forbidden.zone":
w.Header().Set("Content-Type", "text/dns")
http.Error(w, "", http.StatusForbidden)
return
case "toc.zone":
http.Error(w, "", http.StatusConflict)
return
case "notexist.zone":
http.Error(w, "", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/dns")
gz := gzip.NewWriter(w)
gz.Write([]byte(ExampleZone))
gz.Close()
}))
}
func makeZoneCZDS(t *testing.T) (*CZDS, error) {
authStub := makeAuthServer(t)
zoneStub := makeZoneServer(t)
icann := New(TargetUsername, TargetPassword)
icann.BaseURL = zoneStub.URL
icann.AuthURL = authStub.URL
if err := icann.RefreshAccessToken(); err != nil {
return nil, err
}
return icann, nil
}
func TestZoneFile_Save(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
dir, err := ioutil.TempDir("", "example")
if err != nil {
t.Error(err)
}
zone := icann.Zone("testing")
if err := zone.Save(dir, "%s"); err != nil {
t.Error(err)
}
data, err := ioutil.ReadFile(path.Join(dir, zone.TLD()))
if err != nil {
t.Error(err)
}
if string(data) != ExampleZone {
t.Fail()
}
}
func TestCZDS_Zone_noaccess(t *testing.T) {
icann := New(TargetUsername, TargetPassword)
if _, err := icann.Zone("testing").Reader(); err != ErrMustRefresh {
t.Fail()
}
}
func TestCZDS_Zone(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
z, err := icann.Zone("testing").Reader()
if err != nil {
t.Error(err)
}
data, _ := ioutil.ReadAll(z)
if string(data) != ExampleZone {
t.Fail()
}
}
func TestCZDS_Zone_BadRequest(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
if _, err := icann.Zone("badrequest").Reader(); err == nil {
t.Fail()
}
}
func TestCZDS_Zone_Forbidden(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
if _, err := icann.Zone("forbidden").Reader(); err != ErrZoneUnavailable {
t.Fail()
}
}
func TestCZDS_Zone_NotExist(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
if _, err := icann.Zone("notexist").Reader(); err != ErrZoneNotExist {
t.Fail()
}
}
func TestCZDS_Zone_Conflict(t *testing.T) {
icann, err := makeZoneCZDS(t)
if err != nil {
t.Error(err)
}
if _, err := icann.Zone("toc").Reader(); err != ErrTOC {
t.Fail()
}
}