fix IsSubDomain method error (#2283)

Co-authored-by: weiwei3 <weiwei3@37.com>
This commit is contained in:
wei.wei
2022-11-10 19:47:43 +08:00
committed by GitHub
parent 91b94878d3
commit 2e8d8f63ff
2 changed files with 27 additions and 0 deletions

View File

@ -21,6 +21,17 @@ func IsSubDomain(subDomain string, mainDomain string) bool {
mainArray := strings.Split(mainDomain, ".")
subLength := len(subArray)
mainLength := len(mainArray)
// Eg:
// goframe.org is not sub-dome of 's.goframe.org'
if mainLength > subLength {
for i := range mainArray[0 : mainLength-subLength] {
if mainArray[i] != "*" {
return false
}
}
}
// Eg:
// "s.s.goframe.org" is not sub-domain of "*.goframe.org"
// but

View File

@ -58,4 +58,20 @@ func Test_IsSubDomain(t *testing.T) {
t.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
t.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
gtest.C(t, func(t *gtest.T) {
main := "*.*.goframe.org:8080"
t.Assert(gstr.IsSubDomain("goframe.org", main), true)
t.Assert(gstr.IsSubDomain("s.goframe.org", main), true)
t.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true)
t.Assert(gstr.IsSubDomain("s.s.goframe.org:8000", main), true)
t.Assert(gstr.IsSubDomain("s.s.s.goframe.org", main), false)
t.Assert(gstr.IsSubDomain("johng.cn", main), false)
t.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
t.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
gtest.C(t, func(t *gtest.T) {
main := "s.goframe.org"
t.Assert(gstr.IsSubDomain("goframe.org", main), false)
})
}