Skip to content

Commit

Permalink
[test] Add tests for hostname.go in common package (#174)
Browse files Browse the repository at this point in the history
* test: Add six test-cases for TestNameSubsetOf (#167)

- wildcards with different TLDs
- wildcards at different levels in domain hierarchy
- wildcards with subdomains
- empty hostnames
- one empty hostname
- multiple wildcards

* test: Add unit-tests for IsWildCarded & String (#167)
  • Loading branch information
art-tapin authored Apr 28, 2023
1 parent ba154bd commit 6c37169
Showing 1 changed file with 50 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit 6c37169

Please sign in to comment.