1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add EncodeUrl function

This commit is contained in:
dudaodong
2022-07-15 16:29:41 +08:00
parent 47ecfbfd5f
commit 506a8b4776
2 changed files with 26 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
)
@@ -157,3 +158,15 @@ func IsInternalIP(IP net.IP) bool {
}
return false
}
// EncodeUrl encode url
func EncodeUrl(urlStr string) (string, error) {
URL, err := url.Parse(urlStr)
if err != nil {
return "", err
}
URL.RawQuery = URL.Query().Encode()
return URL.String(), nil
}

View File

@@ -96,3 +96,16 @@ func TestGetMacAddrs(t *testing.T) {
macAddrs := GetMacAddrs()
t.Log(macAddrs)
}
func TestEncodeUrl(t *testing.T) {
assert := internal.NewAssert(t, "TestIsInternalIP")
urlAddr := "http://www.lancet.com?a=1&b=[2]"
encodedUrl, err := EncodeUrl(urlAddr)
if err != nil {
t.Log(err)
}
expected := "http://www.lancet.com?a=1&b=%5B2%5D"
assert.Equal(expected, encodedUrl)
}