From 17b8ac6d0be2933e531482eee03e5d9adc12dae9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 13 Aug 2023 15:37:48 +0800 Subject: [PATCH] fix gravatar url is invalid when email contains upper characters --- pkg/utils/avatar.go | 7 ++++++- pkg/utils/avatar_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/avatar_test.go diff --git a/pkg/utils/avatar.go b/pkg/utils/avatar.go index bb0bb313..620aca0f 100644 --- a/pkg/utils/avatar.go +++ b/pkg/utils/avatar.go @@ -1,11 +1,16 @@ package utils -import "fmt" +import ( + "fmt" + "strings" +) const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s" // GetGravatarUrl returns the Gravatar url according to the specified user email address func GetGravatarUrl(email string) string { + email = strings.TrimSpace(email) + email = strings.ToLower(email) emailMd5 := MD5EncodeToString([]byte(email)) return fmt.Sprintf(gravatarUrlFormat, emailMd5) } diff --git a/pkg/utils/avatar_test.go b/pkg/utils/avatar_test.go new file mode 100644 index 00000000..ef7b52a3 --- /dev/null +++ b/pkg/utils/avatar_test.go @@ -0,0 +1,14 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetGravatarUrl(t *testing.T) { + // Reference: https://en.gravatar.com/site/implement/hash/ + expectedValue := "https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346" + actualValue := GetGravatarUrl("MyEmailAddress@example.com") + assert.Equal(t, expectedValue, actualValue) +}