Merge pull request #128 from injet-zhou/dev

fix:panic if ip is empty
pull/136/head
Michael Li 2 years ago committed by GitHub
commit 869f269d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,36 @@
package util
import "testing"
func TestGetIPLoc(t *testing.T) {
type args struct {
ip string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test1",
args: args{
ip: "",
},
want: "",
},
{
name: "test2",
args: args{
ip: "103.197.70.244",
},
want: "香港",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetIPLoc(tt.args.ip); got != tt.want {
t.Errorf("GetIPLoc() = %v, want %v", got, tt.want)
}
})
}
}

@ -4,6 +4,7 @@ import (
_ "embed"
"encoding/binary"
"net"
"strings"
"github.com/yinheli/mahonia"
)
@ -21,6 +22,9 @@ const (
// Find get country and city base ip
func Find(ip string) (string, string) {
if strings.Trim(ip, " ") == "" {
return "", ""
}
// If ip is "::1", To4 returns nil.
to4 := net.ParseIP(ip).To4()
if to4 == nil {

@ -0,0 +1,43 @@
package util
import "testing"
func TestEncodeMD5(t *testing.T) {
type args struct {
value string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test1",
args: args{
value: "123456",
},
want: "e10adc3949ba59abbe56e057f20f883e",
},
{
name: "test2",
args: args{
value: "",
},
want: "d41d8cd98f00b204e9800998ecf8427e", // really odd, why?
},
{
name: "test3",
args: args{
value: "paopaocestr",
},
want: "8a5033dda1a8919224c66e68d846a289",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EncodeMD5(tt.args.value); got != tt.want {
t.Errorf("EncodeMD5() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save