-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains_test.go
178 lines (140 loc) · 4.79 KB
/
domains_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
package porkbun
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDomainsService_ListDomains(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/domains/listAll-success.http")
assert.NotNil(t, httpResponse)
testMethod(t, r, "POST")
testHeaders(t, r)
testCredentials(t, r)
for k, values := range httpResponse.Header {
for _, v := range values {
w.Header().Add(k, v)
}
}
w.WriteHeader(httpResponse.StatusCode)
_, err := io.Copy(w, httpResponse.Body)
assert.NoError(t, err)
})
resp, err := client.Domains.ListDomains(context.Background(), &DomainListOptions{})
assert.NoError(t, err)
assert.Equal(t, "SUCCESS", resp.Status)
assert.NotEmpty(t, resp.Domains)
testDomain := resp.Domains[0]
assert.Equal(t, "borseth.ink", testDomain.Domain)
assert.True(t, bool(testDomain.SecurityLock))
assert.True(t, bool(testDomain.WhoisPrivacy))
assert.False(t, bool(testDomain.AutoRenew))
assert.False(t, bool(testDomain.NotLocal))
}
func TestDomainsService_ListDomains_InvalidTime(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/domains/listAll-invalid-time.http")
assert.NotNil(t, httpResponse)
testMethod(t, r, "POST")
testHeaders(t, r)
testCredentials(t, r)
for k, values := range httpResponse.Header {
for _, v := range values {
w.Header().Add(k, v)
}
}
w.WriteHeader(httpResponse.StatusCode)
_, err := io.Copy(w, httpResponse.Body)
assert.NoError(t, err)
})
_, err := client.Domains.ListDomains(context.Background(), &DomainListOptions{})
assert.Error(t, err)
}
func TestDomainsService_ListDomains_EmptyResponse(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"status":"SUCCESS","domains":[]}`)
})
resp, err := client.Domains.ListDomains(context.Background(), &DomainListOptions{})
assert.NoError(t, err)
assert.Equal(t, "SUCCESS", resp.Status)
assert.Empty(t, resp.Domains)
}
func TestDomainsService_ListDomains_MalformedResponse(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"status":"SUCCESS","domains":[{"domain":"example.com","createDate":"2020-`)
})
_, err := client.Domains.ListDomains(context.Background(), &DomainListOptions{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
}
func TestDomainsService_ListDomains_Pagination(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{
"status":"SUCCESS",
"domains":[
{
"domain":"page1.com",
"status": "ACTIVE",
"tld": "com",
"createDate": "2023-01-01 12:00:00",
"expireDate": "2024-01-01 12:00:00",
"securityLock": "1",
"whoisPrivacy": "1",
"autoRenew": 0,
"notLocal": 0
}
]
}`)
})
options := &DomainListOptions{Start: String("1000")}
resp, err := client.Domains.ListDomains(context.Background(), options)
assert.NoError(t, err)
assert.Equal(t, "SUCCESS", resp.Status)
assert.Len(t, resp.Domains, 1)
assert.Equal(t, "page1.com", resp.Domains[0].Domain)
}
func TestDomain_UnmarshalJSON_InvalidExpireDate(t *testing.T) {
data := []byte(`{
"domain": "example.com",
"status": "active",
"tld": "com",
"createDate": "2024-01-01 12:00:00",
"expireDate": "invalid-date",
"securityLock": "1",
"whoisPrivacy": "1",
"autoRenew": 0,
"notLocal": 0
}`)
var domain Domain
err := json.Unmarshal(data, &domain)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error parsing ExpireDate")
}
func TestDomainsService_ListDomains_PostFailure(t *testing.T) {
setupMockServer(true)
defer teardownMockServer()
mux.HandleFunc("/domain/listAll", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"status":"ERROR","message":"Internal Server Error"}`)
})
_, err := client.Domains.ListDomains(context.Background(), &DomainListOptions{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "500 Internal Server Error")
}