forked from billputer/go-namecheap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_test.go
123 lines (108 loc) · 3.35 KB
/
dns_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
package namecheap
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestDomainsDNSGetHosts(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.dns.getHosts</RequestedCommand>
<CommandResponse Type="namecheap.domains.dns.getHosts">
<DomainDNSGetHostsResult Domain="domain.com" IsUsingOurDNS="true">
<Host HostId="12" Name="@" Type="A" Address="1.2.3.4" MXPref="10" TTL="1800" />
<Host HostId="14" Name="www" Type="A" Address="122.23.3.7" MXPref="10" TTL="1800" />
</DomainDNSGetHostsResult>
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>32.76</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// verify that the URL exactly matches...brittle, I know.
correctURL := "/?ApiKey=anToken&ApiUser=anApiUser&ClientIp=127.0.0.1&Command=namecheap.domains.dns.getHosts&SLD=domain&TLD=com&UserName=anUser"
if r.URL.String() != correctURL {
t.Errorf("URL = %v, want %v", r.URL, correctURL)
}
testMethod(t, r, "GET")
fmt.Fprint(w, respXML)
})
hosts, err := client.DomainsDNSGetHosts("domain", "com")
if err != nil {
t.Errorf("DomainsDNSGetHosts returned error: %v", err)
}
want := &DomainDNSGetHostsResult{
Domain: "domain.com",
IsUsingOurDNS: true,
Hosts: []DomainDNSHost{
{
ID: 12,
Name: "@",
Type: "A",
Address: "1.2.3.4",
MXPref: 10,
TTL: 1800,
},
{
ID: 14,
Name: "www",
Type: "A",
Address: "122.23.3.7",
MXPref: 10,
TTL: 1800,
},
},
}
if !reflect.DeepEqual(hosts, want) {
t.Errorf("DomainsDNSGetHosts returned %+v, want %+v", hosts, want)
}
}
func TestDomainsDNSSetHosts(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.dns.setHosts</RequestedCommand>
<CommandResponse Type="namecheap.domains.dns.setHosts">
<DomainDNSSetHostsResult Domain="domain51.com" IsSuccess="true" />
</CommandResponse>
<Server>SERVER-NAME</Server>
<GMTTimeDifference>+5</GMTTimeDifference>
<ExecutionTime>32.76</ExecutionTime>
</ApiResponse>`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// verify that the URL exactly matches...brittle, I know.
correctURL := "/?Address1=http%3A%2F%2Fwww.namecheap.com&ApiKey=anToken&ApiUser=anApiUser&ClientIp=127.0.0.1&Command=namecheap.domains.dns.setHosts&HostName1=%40&RecordType1=URL&SLD=domain51&TLD=com&TTL1=100&UserName=anUser"
if r.URL.String() != correctURL {
t.Errorf("URL = %v, want %v", r.URL, correctURL)
}
testMethod(t, r, "GET")
fmt.Fprint(w, respXML)
})
hosts := []DomainDNSHost{
{
Name: "@",
Type: "URL",
Address: "http://www.namecheap.com",
TTL: 100,
},
}
result, err := client.DomainDNSSetHosts("domain51", "com", hosts)
if err != nil {
t.Errorf("DomainsDNSGetHosts returned error: %v", err)
}
want := &DomainDNSSetHostsResult{
Domain: "domain51.com",
IsSuccess: true,
}
if !reflect.DeepEqual(result, want) {
t.Errorf("DomainsDNSSetHosts returned %+v, want %+v", hosts, want)
}
}