Skip to content

Commit

Permalink
return error for all domain exts without grace period (#6)
Browse files Browse the repository at this point in the history
* return error for all domain exts without grace period

* Update whois.go

* requested changes

---------

Co-authored-by: julian.pieles <[email protected]>
Co-authored-by: TwiN <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2023
1 parent c0a275d commit ea4c6db
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
18 changes: 17 additions & 1 deletion whois.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package whois

import (
"errors"
"io"
"net"
"strings"
Expand All @@ -11,6 +12,8 @@ const (
ianaWHOISServerAddress = "whois.iana.org:43"
)

var tldWithoutExpirationDate = []string{"at","be","ch","co.at","com.br","or.at","de","fr","me","mx","nl"}

type Client struct {
whoisServerAddress string

Expand Down Expand Up @@ -49,14 +52,27 @@ func (c *Client) WithReferralCache(enabled bool) *Client {
return c
}

func doesTLDHaveExpirationDate(e string) bool {
for _, a := range tldWithoutExpirationDate {
if a == e {
return true
}
}
return false
}

func (c *Client) Query(domain string) (string, error) {
parts := strings.Split(domain, ".")
domainExtension := parts[len(parts)-1]
if doesTLDHaveExpirationDate(domainExtension) {
return "", errors.New("Domain extension " + domainExtension + " does not have a grace period.")
}
if c.isCachingReferralWHOISServers {
if cachedWHOISServer, ok := c.referralWHOISServersCache[domain]; ok {
return c.query(cachedWHOISServer, domain)
}
}
output, err := c.query(c.whoisServerAddress, parts[len(parts)-1])
output, err := c.query(c.whoisServerAddress, domainExtension)
if err != nil {
return "", err
}
Expand Down
40 changes: 24 additions & 16 deletions whois_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func TestClient(t *testing.T) {
domain: "name.black",
wantErr: false,
},
{
domain: "name.de",
wantErr: true,
},
}
client := NewClient().WithReferralCache(true)
for _, scenario := range scenarios {
Expand All @@ -56,11 +60,13 @@ func TestClient(t *testing.T) {
t.Error("expected error, got none")
t.FailNow()
}
if !scenario.wantErr && err != nil {
t.Error("expected no error, got", err.Error())
}
if !strings.Contains(strings.ToLower(output), scenario.domain) {
t.Errorf("expected %s in output, got %s", scenario.domain, output)
if !scenario.wantErr {
if err != nil {
t.Error("expected no error, got", err.Error())
}
if !strings.Contains(strings.ToLower(output), scenario.domain) {
t.Errorf("expected %s in output, got %s", scenario.domain, output)
}
}
})
time.Sleep(50 * time.Millisecond) // Give the WHOIS servers some breathing room
Expand All @@ -70,17 +76,19 @@ func TestClient(t *testing.T) {
t.Error("expected error, got none")
t.FailNow()
}
if !scenario.wantErr && err != nil {
t.Error("expected no error, got", err.Error())
}
if response.ExpirationDate.Unix() == 0 {
t.Errorf("expected to have an expiry date")
}
if len(response.NameServers) == 0 {
t.Errorf("expected to have at least one name server")
}
if len(response.DomainStatuses) == 0 {
t.Errorf("expected to have at least one domain status")
if !scenario.wantErr {
if err != nil {
t.Error("expected no error, got", err.Error())
}
if response.ExpirationDate.Unix() == 0 {
t.Errorf("expected to have an expiry date")
}
if len(response.NameServers) == 0 {
t.Errorf("expected to have at least one name server")
}
if len(response.DomainStatuses) == 0 {
t.Errorf("expected to have at least one domain status")
}
}
})
time.Sleep(50 * time.Millisecond) // Give the WHOIS servers some breathing room
Expand Down

0 comments on commit ea4c6db

Please sign in to comment.