mirror of
https://gitee.com/johng/gf
synced 2026-07-02 19:31:07 +08:00
fix IsSubDomain method error (#2283)
Co-authored-by: weiwei3 <weiwei3@37.com>
This commit is contained in:
@ -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
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user