Skip to content

Commit 3aed806

Browse files
committed
[feat gw-api]add unit test
1 parent cfca713 commit 3aed806

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

pkg/gateway/routeutils/utils_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,81 @@ func Test_isHostnameCompatible(t *testing.T) {
608608
})
609609
}
610610
}
611+
612+
func Test_getCompatibleHostname(t *testing.T) {
613+
tests := []struct {
614+
name string
615+
hostnameOne string
616+
hostnameTwo string
617+
wantHostname string
618+
wantOk bool
619+
}{
620+
{
621+
name: "exact match",
622+
hostnameOne: "example.com",
623+
hostnameTwo: "example.com",
624+
wantHostname: "example.com",
625+
wantOk: true,
626+
},
627+
{
628+
name: "wildcard in first, specific in second",
629+
hostnameOne: "*.example.com",
630+
hostnameTwo: "api.example.com",
631+
wantHostname: "api.example.com",
632+
wantOk: true,
633+
},
634+
{
635+
name: "wildcard in second, specific in first",
636+
hostnameOne: "api.example.com",
637+
hostnameTwo: "*.example.com",
638+
wantHostname: "api.example.com",
639+
wantOk: true,
640+
},
641+
{
642+
name: "incompatible hostnames",
643+
hostnameOne: "example.com",
644+
hostnameTwo: "different.com",
645+
wantHostname: "",
646+
wantOk: false,
647+
},
648+
{
649+
name: "wildcard does not match",
650+
hostnameOne: "*.example.com",
651+
hostnameTwo: "api.different.com",
652+
wantHostname: "",
653+
wantOk: false,
654+
},
655+
{
656+
name: "both wildcards same domain",
657+
hostnameOne: "*.example.com",
658+
hostnameTwo: "*.example.com",
659+
wantHostname: "*.example.com",
660+
wantOk: true,
661+
},
662+
{
663+
name: "both wildcards different domains",
664+
hostnameOne: "*.example.com",
665+
hostnameTwo: "*.different.com",
666+
wantHostname: "",
667+
wantOk: false,
668+
},
669+
{
670+
name: "nested subdomain with wildcard",
671+
hostnameOne: "*.example.com",
672+
hostnameTwo: "sub.api.example.com",
673+
wantHostname: "sub.api.example.com",
674+
wantOk: true,
675+
},
676+
}
677+
for _, tt := range tests {
678+
t.Run(tt.name, func(t *testing.T) {
679+
gotHostname, gotOk := getCompatibleHostname(tt.hostnameOne, tt.hostnameTwo)
680+
if gotHostname != tt.wantHostname {
681+
t.Errorf("getCompatibleHostname() hostname = %v, want %v", gotHostname, tt.wantHostname)
682+
}
683+
if gotOk != tt.wantOk {
684+
t.Errorf("getCompatibleHostname() ok = %v, want %v", gotOk, tt.wantOk)
685+
}
686+
})
687+
}
688+
}

0 commit comments

Comments
 (0)