-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: added namecheap.domains.getInfo API support * fix: fixed work with FreeDNS domains for namecheap.domains.dns.getList * chore: added unit tests for new functionality
- Loading branch information
Showing
4 changed files
with
309 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package namecheap | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
) | ||
|
||
type DomainsGetInfoResponse struct { | ||
XMLName *xml.Name `xml:"ApiResponse"` | ||
Errors *[]struct { | ||
Message *string `xml:",chardata"` | ||
Number *string `xml:"Number,attr"` | ||
} `xml:"Errors>Error"` | ||
CommandResponse *DomainsGetInfoCommandResponse `xml:"CommandResponse"` | ||
} | ||
|
||
type DomainsGetInfoCommandResponse struct { | ||
DomainDNSGetListResult *DomainsGetInfoResult `xml:"DomainGetInfoResult"` | ||
} | ||
|
||
type DomainsGetInfoResult struct { | ||
DomainName *string `xml:"DomainName,attr"` | ||
IsPremium *bool `xml:"IsPremium,attr"` | ||
PremiumDnsSubscription *PremiumDnsSubscription `xml:"PremiumDnsSubscription"` | ||
DnsDetails *DnsDetails `xml:"DnsDetails"` | ||
} | ||
|
||
type PremiumDnsSubscription struct { | ||
IsActive *bool `xml:"IsActive"` | ||
} | ||
|
||
type DnsDetails struct { | ||
ProviderType *string `xml:"ProviderType,attr"` | ||
IsUsingOurDNS *bool `xml:"IsUsingOurDNS,attr"` | ||
Nameservers *[]string `xml:"Nameserver"` | ||
} | ||
|
||
func (ds *DomainsService) GetInfo(domain string) (*DomainsGetInfoCommandResponse, error) { | ||
var response DomainsGetInfoResponse | ||
|
||
params := map[string]string{ | ||
"Command": "namecheap.domains.getInfo", | ||
"DomainName": domain, | ||
"HostName": domain, | ||
} | ||
|
||
_, err := ds.client.DoXML(params, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if response.Errors != nil && len(*response.Errors) > 0 { | ||
apiErr := (*response.Errors)[0] | ||
|
||
return nil, fmt.Errorf("%s (%s)", *apiErr.Message, *apiErr.Number) | ||
} | ||
|
||
return response.CommandResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package namecheap | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestDomainsGetInfo(t *testing.T) { | ||
fakeResponse := ` | ||
<?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 ID="1706717" DomainName="horse-family.com.ua" OwnerName="NCStaffvladlenf" IsOwner="false" IsPremium="false"> | ||
<DomainDetails> | ||
<CreatedDate>11/26/2021</CreatedDate> | ||
<NumYears>0</NumYears> | ||
</DomainDetails> | ||
<LockDetails /> | ||
<Whoisguard Enabled="NotAlloted"> | ||
<ID>0</ID> | ||
</Whoisguard> | ||
<PremiumDnsSubscription> | ||
<UseAutoRenew>false</UseAutoRenew> | ||
<SubscriptionId>-1</SubscriptionId> | ||
<CreatedDate>0001-01-01T00:00:00</CreatedDate> | ||
<ExpirationDate>0001-01-01T00:00:00</ExpirationDate> | ||
<IsActive>false</IsActive> | ||
</PremiumDnsSubscription> | ||
<DnsDetails ProviderType="FreeDNS" IsUsingOurDNS="true" HostCount="0" EmailType="No Email Service" DynamicDNSStatus="false" IsFailover="false"> | ||
<Nameserver>freedns1.registrar-servers.com</Nameserver> | ||
<Nameserver>freedns2.registrar-servers.com</Nameserver> | ||
<Nameserver>freedns3.registrar-servers.com</Nameserver> | ||
<Nameserver>freedns4.registrar-servers.com</Nameserver> | ||
<Nameserver>freedns5.registrar-servers.com</Nameserver> | ||
</DnsDetails> | ||
<Modificationrights /> | ||
</DomainGetInfoResult> | ||
</CommandResponse> | ||
<Server>PHX01APIEXT12</Server> | ||
<GMTTimeDifference>--5:00</GMTTimeDifference> | ||
<ExecutionTime>0.013</ExecutionTime> | ||
</ApiResponse> | ||
` | ||
|
||
t.Run("request_command", func(t *testing.T) { | ||
var sentBody url.Values | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
body, _ := ioutil.ReadAll(request.Body) | ||
query, _ := url.ParseQuery(string(body)) | ||
sentBody = query | ||
_, _ = writer.Write([]byte(fakeResponse)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
client := setupClient(nil) | ||
client.BaseURL = mockServer.URL | ||
|
||
_, err := client.Domains.GetInfo("horse-family.com.ua") | ||
if err != nil { | ||
t.Fatal("Unable to get domains", err) | ||
} | ||
|
||
assert.Equal(t, "namecheap.domains.getInfo", sentBody.Get("Command")) | ||
}) | ||
|
||
t.Run("server_empty_response", func(t *testing.T) { | ||
fakeLocalResponse := "" | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
_, _ = writer.Write([]byte(fakeLocalResponse)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
client := setupClient(nil) | ||
client.BaseURL = mockServer.URL | ||
|
||
_, err := client.DomainsDNS.GetHosts("horse-family.com.ua") | ||
|
||
assert.EqualError(t, err, "unable to parse server response: EOF") | ||
}) | ||
|
||
t.Run("server_non_xml_response", func(t *testing.T) { | ||
fakeLocalResponse := "non-xml response" | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
_, _ = writer.Write([]byte(fakeLocalResponse)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
client := setupClient(nil) | ||
client.BaseURL = mockServer.URL | ||
|
||
_, err := client.DomainsDNS.GetHosts("domain.net") | ||
|
||
assert.EqualError(t, err, "unable to parse server response: EOF") | ||
}) | ||
|
||
t.Run("server_broken_xml_response", func(t *testing.T) { | ||
fakeLocalResponse := "<broken></xml><response>" | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
_, _ = writer.Write([]byte(fakeLocalResponse)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
client := setupClient(nil) | ||
client.BaseURL = mockServer.URL | ||
|
||
_, err := client.DomainsDNS.GetHosts("domain.net") | ||
|
||
assert.EqualError(t, err, "unable to parse server response: expected element type <ApiResponse> but have <broken>") | ||
}) | ||
|
||
t.Run("server_respond_with_error", func(t *testing.T) { | ||
fakeLocalResponse := ` | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response"> | ||
<Errors> | ||
<Error Number="2050900">Invalid Address</Error> | ||
</Errors> | ||
<Warnings /> | ||
<RequestedCommand>namecheap.domains.dns.getlist</RequestedCommand> | ||
<Server>PHX01SBAPIEXT05</Server> | ||
<GMTTimeDifference>--4:00</GMTTimeDifference> | ||
<ExecutionTime>0.011</ExecutionTime> | ||
</ApiResponse> | ||
` | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
_, _ = writer.Write([]byte(fakeLocalResponse)) | ||
})) | ||
defer mockServer.Close() | ||
|
||
client := setupClient(nil) | ||
client.BaseURL = mockServer.URL | ||
|
||
_, err := client.DomainsDNS.GetHosts("domain.net") | ||
|
||
assert.EqualError(t, err, "Invalid Address (2050900)") | ||
}) | ||
} |