convert FriendlyHost to use svchost for validation

Removed some of the test cases that we don't allow in the svchost
package. Will check back if those are needed in the registry and work
around them as necessary.
This commit is contained in:
James Bardin 2017-10-24 18:56:20 -04:00
parent 248a5e4523
commit 27e578e7fb
2 changed files with 21 additions and 18 deletions

View File

@ -4,7 +4,7 @@ import (
"regexp"
"strings"
"golang.org/x/net/idna"
"github.com/hashicorp/terraform/svchost"
)
var (
@ -110,27 +110,20 @@ func (h *FriendlyHost) Valid() bool {
// Display returns the host formatted for display to the user in CLI or web
// output.
func (h *FriendlyHost) Display() string {
parts := strings.SplitN(h.Raw, ":", 2)
var err error
parts[0], err = idna.Display.ToUnicode(parts[0])
hostname, err := svchost.ForComparison(h.Raw)
if err != nil {
return InvalidHostString
}
return strings.Join(parts, ":")
return hostname.ForDisplay()
}
// Normalized returns the host formatted for internal reference or comparison.
func (h *FriendlyHost) Normalized() string {
// For now IDNA does all the normalisation we need including case-folding
// pure ASCII to lower. But breaks if a custom port is included while we
// want to allow that and normalize comparison including it,
parts := strings.SplitN(h.Raw, ":", 2)
var err error
parts[0], err = idna.Lookup.ToASCII(parts[0])
hostname, err := svchost.ForComparison(h.Raw)
if err != nil {
return InvalidHostString
}
return strings.Join(parts, ":")
return hostname.String()
}
// String returns the host formatted as the user originally typed it assuming it

View File

@ -95,15 +95,21 @@ func TestFriendlyHost(t *testing.T) {
if v := gotHost.String(); v != tt.wantHost {
t.Fatalf("String() = %v, want %v", v, tt.wantHost)
}
if v := gotHost.Valid(); v != tt.wantValid {
t.Fatalf("Valid() = %v, want %v", v, tt.wantValid)
}
// FIXME: should we allow punycode as input
if !tt.wantValid {
return
}
if v := gotHost.Display(); v != tt.wantDisplay {
t.Fatalf("Display() = %v, want %v", v, tt.wantDisplay)
}
if v := gotHost.Normalized(); v != tt.wantNorm {
t.Fatalf("Normalized() = %v, want %v", v, tt.wantNorm)
}
if v := gotHost.Valid(); v != tt.wantValid {
t.Fatalf("Valid() = %v, want %v", v, tt.wantValid)
}
if gotRest != strings.TrimLeft(sfx, "/") {
t.Fatalf("ParseFriendlyHost() rest = %v, want %v", gotRest, strings.TrimLeft(sfx, "/"))
}
@ -112,9 +118,13 @@ func TestFriendlyHost(t *testing.T) {
if !gotHost.Equal(&FriendlyHost{Raw: tt.wantDisplay}) {
t.Fatalf("Equal() should be true for %s and %s", tt.wantHost, tt.wantValid)
}
if !gotHost.Equal(&FriendlyHost{Raw: tt.wantNorm}) {
t.Fatalf("Equal() should be true for %s and %s", tt.wantHost, tt.wantNorm)
}
// FIXME: Do we need to accept normalized input?
//if !gotHost.Equal(&FriendlyHost{Raw: tt.wantNorm}) {
// fmt.Println(gotHost.Normalized(), tt.wantNorm)
// fmt.Println(" ", (&FriendlyHost{Raw: tt.wantNorm}).Normalized())
// t.Fatalf("Equal() should be true for %s and %s", tt.wantHost, tt.wantNorm)
//}
})
}