mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
feat: add EncodeUrl function
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -157,3 +158,15 @@ func IsInternalIP(IP net.IP) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,3 +96,16 @@ func TestGetMacAddrs(t *testing.T) {
|
|||||||
macAddrs := GetMacAddrs()
|
macAddrs := GetMacAddrs()
|
||||||
t.Log(macAddrs)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user