Skip to content
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

[test] Add tests for hostname.go in common package #174

Merged
merged 2 commits into from
Apr 28, 2023
Merged
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
50 changes: 50 additions & 0 deletions pkg/common/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestNameSubsetOf(t *testing.T) {
{"hostname is not subset of wildcard subdomain", "foo.com", "*.foo.com", false},
{"global wildcard is not subset of wildcard hostname", "*", "*.com", false},
{"wildcard hostname is subset of global wildcard", "*.com", "*", true},
{"wildcards with different TLDs", "*.com", "*.org", false},
{"wildcards at different levels in domain hierarchy", "*.foo.com", "*.bar.foo.com", false},
{"wildcards with subdomains", "*.foo.com", "*.baz.foo.com", false},
{"empty hostnames", "", "", true},
{"one empty hostname", "", "foo.com", false},
{"multiple wildcards", "*.foo.*.com", "*.foo.*.com", true},
}

for _, tc := range testCases {
Expand All @@ -32,3 +38,47 @@ func TestNameSubsetOf(t *testing.T) {
})
}
}

func TestIsWildCarded(t *testing.T) {
testCases := []struct {
name string
hostname Name
expected bool
}{
{"when wildcard at beginning then return true", "*.example.com", true},
{"when empty string then return false", "", false},
{"when no wildcard then return false", "example.com", false},
{"when wildcard in middle then return false", "subdomain.*.example.com", false},
{"when wildcard at end then return false", "subdomain.example.*", false},
}

for _, tc := range testCases {
t.Run(tc.name, func(subT *testing.T) {
res := tc.hostname.IsWildCarded()
if res != tc.expected {
subT.Errorf("expected (%t) for hostname '%s', but got (%t)", tc.expected, tc.hostname, res)
}
})
}
}

func TestString(t *testing.T) {
testCases := []struct {
name string
actual Name
expected string
}{
{"empty name", "", ""},
{"non-empty name", "example.com", "example.com"},
{"wildcarded name", "*.com", "*.com"},
}

for _, tc := range testCases {
t.Run(tc.name, func(subT *testing.T) {
res := tc.actual.String()
if res != tc.expected {
subT.Errorf("expected (%s), got (%s)", tc.expected, res)
}
})
}
}