-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_test.go
323 lines (290 loc) · 9.87 KB
/
domain_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package namecheap
import (
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
)
func TestDomainsGetList(t *testing.T) {
setup()
defer teardown()
respXML := `
<?xml version="1.0" encoding="utf-8"?>
<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.getList</RequestedCommand>
<CommandResponse Type="namecheap.domains.getList">
<DomainGetListResult>
<Domain ID="57579" Name="example.com" User="anUser" Created="11/04/2014" Expires="11/04/2015" IsExpired="false" IsLocked="false" AutoRenew="false" WhoisGuard="ENABLED" />
</DomainGetListResult>
<Paging>
<TotalItems>12</TotalItems>
<CurrentPage>1</CurrentPage>
<PageSize>20</PageSize>
</Paging>
</CommandResponse>
<Server>WEB1-SANDBOX1</Server>
<GMTTimeDifference>--5:00</GMTTimeDifference>
<ExecutionTime>0.009</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.getList")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})
domains, err := client.DomainsGetList()
if err != nil {
t.Errorf("DomainsGetList returned error: %v", err)
}
// DomainGetListResult we expect, given the respXML above
want := []DomainGetListResult{{
ID: 57579,
Name: "example.com",
User: "anUser",
Created: "11/04/2014",
Expires: "11/04/2015",
IsExpired: false,
IsLocked: false,
AutoRenew: false,
WhoisGuard: "ENABLED",
}}
if !reflect.DeepEqual(domains, want) {
t.Errorf("DomainsGetList returned %+v, want %+v", domains, want)
}
}
func TestDomainGetInfo(t *testing.T) {
setup()
defer teardown()
respXML := `<?xml version="1.0" encoding="utf-8"?>
<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.getInfo</RequestedCommand>
<CommandResponse Type="namecheap.domains.getInfo">
<DomainGetInfoResult Status="Ok" ID="57582" DomainName="example.com" OwnerName="anUser" IsOwner="true">
<DomainDetails>
<CreatedDate>11/04/2014</CreatedDate>
<ExpiredDate>11/04/2015</ExpiredDate>
<NumYears>0</NumYears>
</DomainDetails>
<LockDetails />
<Whoisguard Enabled="True">
<ID>53536</ID>
<ExpiredDate>11/04/2015</ExpiredDate>
<EmailDetails WhoisGuardEmail="[email protected]" ForwardedTo="[email protected]" LastAutoEmailChangeDate="" AutoEmailChangeFrequencyDays="0" />
</Whoisguard>
<DnsDetails ProviderType="FREE" IsUsingOurDNS="true">
<Nameserver>dns1.registrar-servers.com</Nameserver>
<Nameserver>dns2.registrar-servers.com</Nameserver>
<Nameserver>dns3.registrar-servers.com</Nameserver>
<Nameserver>dns4.registrar-servers.com</Nameserver>
<Nameserver>dns5.registrar-servers.com</Nameserver>
</DnsDetails>
<Modificationrights All="true" />
</DomainGetInfoResult>
</CommandResponse>
<Server>WEB1-SANDBOX1</Server>
<GMTTimeDifference>--5:00</GMTTimeDifference>
<ExecutionTime>0.008</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.getInfo")
correctParams.Set("DomainName", "example.com")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})
domain, err := client.DomainGetInfo("example.com")
if err != nil {
t.Errorf("DomainGetInfo returned error: %v", err)
}
// DomainInfo we expect, given the respXML above
want := &DomainInfo{
ID: 57582,
Name: "example.com",
Owner: "anUser",
Created: "11/04/2014",
Expires: "11/04/2015",
IsExpired: false,
IsLocked: false,
AutoRenew: false,
DNSDetails: DNSDetails{
ProviderType: "FREE",
IsUsingOurDNS: true,
Nameservers: []string{
"dns1.registrar-servers.com",
"dns2.registrar-servers.com",
"dns3.registrar-servers.com",
"dns4.registrar-servers.com",
"dns5.registrar-servers.com",
},
},
Whoisguard: Whoisguard{
Enabled: true,
ID: 53536,
ExpiredDate: "11/04/2015",
},
}
if !reflect.DeepEqual(domain, want) {
t.Errorf("DomainGetInfo returned %+v, want %+v", domain, want)
}
}
func TestDomainsCheck(t *testing.T) {
setup()
defer teardown()
respXML := `<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="domain1.com" Available="true" />
<DomainCheckResult Domain="availabledomain.com" Available="false" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>32.76</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.check")
correctParams.Set("DomainList", "domain1.com,availabledomain.com")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})
domains, err := client.DomainsCheck("domain1.com", "availabledomain.com")
if err != nil {
t.Errorf("DomainsCheck returned error: %v", err)
}
// DomainCheckResult we expect, given the respXML above
want := []DomainCheckResult{
DomainCheckResult{
Domain: "domain1.com",
Available: true,
},
DomainCheckResult{
Domain: "availabledomain.com",
Available: false,
},
}
if !reflect.DeepEqual(domains, want) {
t.Errorf("DomainsCheck returned %+v, want %+v", domains, want)
}
}
func TestDomainCreate(t *testing.T) {
setup()
defer teardown()
respXML := `<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<RequestedCommand>namecheap.domains.create</RequestedCommand>
<CommandResponse Type="namecheap.domains.create">
<DomainCreateResult Domain="domain1.com" Registered="true" ChargedAmount="20.3600" DomainID="9007" OrderID="196074" TransactionID="380716" WhoisguardEnable="true" NonRealTimeDomain="false" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>0.078</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
fillInfo := func(prefix string) {
correctParams.Set(prefix+"FirstName", "John")
correctParams.Set(prefix+"LastName", "Smith")
correctParams.Set(prefix+"Address1", "8939 S.cross Blvd")
correctParams.Set(prefix+"StateProvince", "CA")
correctParams.Set(prefix+"PostalCode", "90045")
correctParams.Set(prefix+"Country", "US")
correctParams.Set(prefix+"Phone", "+1.6613102107")
correctParams.Set(prefix+"EmailAddress", "[email protected]")
correctParams.Set(prefix+"City", "CA")
}
correctParams.Set("Command", "namecheap.domains.create")
correctParams.Set("DomainName", "domain1.com")
correctParams.Set("Years", "2")
correctParams.Set("AddFreeWhoisguard", "yes")
correctParams.Set("WGEnabled", "yes")
correctParams.Set("Nameservers", "ns1.test.com,ns2.test.com")
fillInfo("AuxBilling")
fillInfo("Tech")
fillInfo("Admin")
fillInfo("Registrant")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})
client.NewRegistrant(
"John", "Smith",
"8939 S.cross Blvd", "",
"CA", "CA", "90045", "US",
"+1.6613102107", "[email protected]",
)
result, err := client.DomainCreate("domain1.com", 2, DomainCreateOption{
AddFreeWhoisguard: true,
WGEnabled: true,
Nameservers: []string{
"ns1.test.com",
"ns2.test.com",
},
})
if err != nil {
t.Fatalf("DomainCreate returned error: %v", nil)
}
// DomainCreateResult we expect, given the respXML above
want := &DomainCreateResult{
"domain1.com", true, 20.36, 9007, 196074, 380716, true, false,
}
if !reflect.DeepEqual(result, want) {
t.Fatalf("DomainCreate returned\n%+v,\nwant\n%+v", result, want)
}
}
func TestDomainsRenew(t *testing.T) {
setup()
defer teardown()
respXML := `<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors />
<RequestedCommand>namecheap.domains.renew</RequestedCommand>
<CommandResponse Type="namecheap.domains.renew">
<DomainRenewResult DomainName="domain1.com" DomainID="151378" Renew="true" OrderID="109116" TransactionID="119569" ChargedAmount="650.0000">
<DomainDetails>
<ExpiredDate>4/30/2021 11:31:13 AM</ExpiredDate>
<NumYears>0</NumYears>
</DomainDetails>
</DomainRenewResult>
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>32.76</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
correctParams := fillDefaultParams(url.Values{})
correctParams.Set("Command", "namecheap.domains.renew")
correctParams.Set("DomainName", "domain1.com")
correctParams.Set("Years", "1")
testBody(t, r, correctParams)
testMethod(t, r, "POST")
fmt.Fprint(w, respXML)
})
result, err := client.DomainRenew("domain1.com", 1)
if err != nil {
t.Errorf("DomainRenew returned error: %v", err)
}
// DomainCheckResult we expect, given the respXML above
want := &DomainRenewResult{
DomainID: 151378,
Name: "domain1.com",
Renewed: true,
ChargedAmount: 650,
TransactionID: 119569,
OrderID: 109116,
ExpireDate: "4/30/2021 11:31:13 AM",
}
if !reflect.DeepEqual(result, want) {
t.Errorf("DomainRenew returned %+v, want %+v", result, want)
}
}