From f5827651b3381b8ec82a78ed8b02c40dde79ff1e Mon Sep 17 00:00:00 2001 From: minhlq Date: Wed, 5 Apr 2023 09:57:45 +0700 Subject: [PATCH] fix: correct hasdomain function and increase df update letter --- pkg/service/notion/template.go | 2 +- pkg/utils/strings.go | 2 +- pkg/utils/strings_test.go | 32 +++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/service/notion/template.go b/pkg/service/notion/template.go index 550889915..346189a45 100644 --- a/pkg/service/notion/template.go +++ b/pkg/service/notion/template.go @@ -91,7 +91,7 @@ const MJMLChangelogTemplate = ` Changelog Email Changelog Email - + diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 149057a6e..cee0c3a05 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -49,5 +49,5 @@ func RemoveAllSpace(str string) string { func HasDomain(str string) bool { u, err := url.Parse(str) - return err == nil && u.Scheme != "" && u.Host != "" && regexp.MustCompile(`^[^.]+\.?[^.]+$`).MatchString(u.Host) + return err == nil && u.Scheme != "" && u.Host != "" && regexp.MustCompile(`^[^.]+\.[^.]+(\.[^.]+)*$`).MatchString(u.Host) } diff --git a/pkg/utils/strings_test.go b/pkg/utils/strings_test.go index de08b7be6..b433a3df7 100644 --- a/pkg/utils/strings_test.go +++ b/pkg/utils/strings_test.go @@ -1,6 +1,8 @@ package utils -import "testing" +import ( + "testing" +) func TestIsNumber(t *testing.T) { type args struct { @@ -34,3 +36,31 @@ func TestIsNumber(t *testing.T) { }) } } + +func TestHasDomain(t *testing.T) { + testCases := []struct { + url string + expected bool + }{ + {"https://www.example.com", true}, + {"https://www.facebook.com", true}, + {"http://google.com", true}, + {"https://docs.google.co.uk", true}, + {"https://example.com", true}, + {"https://example.co.uk", true}, + {"https://sub.example.co.uk", true}, + {"https://sub.sub.example.co.uk", true}, + {"https://example..com", false}, + {"ftp://example.com", true}, + {"not a url", false}, + } + + for _, tc := range testCases { + t.Run(tc.url, func(t *testing.T) { + result := HasDomain(tc.url) + if result != tc.expected { + t.Errorf("HasDomain() = %v, want %v", result, tc.expected) + } + }) + } +}