Skip to content

Commit

Permalink
add MustNormalize
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisohaver committed Oct 18, 2019
1 parent 5f114d3 commit b51abd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 15 additions & 2 deletions plugin/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,26 @@ type (

// Normalize will return the host portion of host, stripping
// of any port or transport. The host will also be fully qualified and lowercased.
// An empty string is returned on failure
func (h Host) Normalize() string {
// The error can be ignored here, because this function should only be called after the corefile has already been vetted.
host, _ := h.MustNormalize()
return host
}

// MustNormalize will return the host portion of host, stripping
// of any port or transport. The host will also be fully qualified and lowercased.
// An error is returned on error
func (h Host) MustNormalize() (string, error) {
s := string(h)
_, s = parse.Transport(s)

// The error can be ignored here, because this function is called after the corefile has already been vetted.
host, _, _, _ := SplitHostPort(s)
return Name(host).Normalize()
host, _, _, err := SplitHostPort(s)
if err != nil {
return "", err
}
return Name(host).Normalize(), nil
}

// SplitHostPort splits s up in a host and port portion, taking reverse address notation into account.
Expand Down
11 changes: 11 additions & 0 deletions plugin/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ func TestHostNormalize(t *testing.T) {
}
}

func TestHostMustNormalizeFail(t *testing.T) {
hosts := []string{"..:53", "::", ""}
for i := 0; i < len(hosts); i++ {
ts := hosts[i]
h, err := Host(ts).MustNormalize()
if err == nil {
t.Errorf("Expected error, got %v", h)
}
}
}

func TestSplitHostPortReverse(t *testing.T) {
tests := map[string]int{
"example.org.": 0,
Expand Down

0 comments on commit b51abd3

Please sign in to comment.