diff --git a/text/gstr/gstr_domain.go b/text/gstr/gstr_domain.go index 45f31bb72..140c44d02 100644 --- a/text/gstr/gstr_domain.go +++ b/text/gstr/gstr_domain.go @@ -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 diff --git a/text/gstr/gstr_z_unit_domain_test.go b/text/gstr/gstr_z_unit_domain_test.go index 22591a017..f7176d6b8 100644 --- a/text/gstr/gstr_z_unit_domain_test.go +++ b/text/gstr/gstr_z_unit_domain_test.go @@ -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) + }) }