fix:panic if ip is empty

pull/128/head
injet-zhou 3 years ago
parent e47604b249
commit 21e88a36e5

@ -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 "", ""
}
offset := searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
if offset <= 0 {
return "", ""

@ -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