Skip to content

Fix #25: In Domain, strip trailing dot from domain name if present #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion domainutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// If quantity matches the number of subdomains in domain, this function returns true.
func HasSubdomainQuantity(domain string, quantity int) bool {
domainSplit := SplitDomain(domain)
if len(domainSplit) - 2 == quantity {
if len(domainSplit)-2 == quantity {
return true
}
return false
Expand Down Expand Up @@ -83,6 +83,11 @@ func Domain(url string) string {
currentTld := *tlds
foundTld := false

// Trailing periods are permitted
if parts[len(parts)-1] == "" {
parts = parts[:len(parts)-1]
}

// Cycle trough parts in reverse
if len(parts) > 1 {
for i := len(parts) - 1; i >= 0; i-- {
Expand Down
19 changes: 19 additions & 0 deletions domainutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,25 @@ func TestDomain(t *testing.T) {
}
}

// TestDomainrailingDot tests that Domain() accepts trailing dots in host names
func TestDomainTrailingDot(t *testing.T) {
//Test cases
cases := map[string]string{
"http://google.com": "google.com",
"http://google.com.": "google.com",
"http://google.com./": "google.com",
"http://google.com./page": "google.com",
}

//Test each domain, some should fail (expected)
for url, expected := range cases {
domain := Domain(url)
if domain != expected {
t.Errorf("Url (%q) returned (%s) as domain, but expected (%s)", url, domain, expected)
}
}
}

// BenchmarkDomain benchmarks Domain() function
func BenchmarkDomain(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down