fix gravatar url is invalid when email contains upper characters

This commit is contained in:
MaysWind
2023-08-13 15:37:48 +08:00
parent 4ac78fe4d1
commit 17b8ac6d0b
2 changed files with 20 additions and 1 deletions
+6 -1
View File
@@ -1,11 +1,16 @@
package utils package utils
import "fmt" import (
"fmt"
"strings"
)
const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s" const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s"
// GetGravatarUrl returns the Gravatar url according to the specified user email address // GetGravatarUrl returns the Gravatar url according to the specified user email address
func GetGravatarUrl(email string) string { func GetGravatarUrl(email string) string {
email = strings.TrimSpace(email)
email = strings.ToLower(email)
emailMd5 := MD5EncodeToString([]byte(email)) emailMd5 := MD5EncodeToString([]byte(email))
return fmt.Sprintf(gravatarUrlFormat, emailMd5) return fmt.Sprintf(gravatarUrlFormat, emailMd5)
} }
+14
View File
@@ -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)
}