fix gravatar url is invalid when email contains upper characters
This commit is contained in:
+6
-1
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user