-
Notifications
You must be signed in to change notification settings - Fork 191
/
encode_test.go
58 lines (44 loc) · 1.66 KB
/
encode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package strutil_test
import (
"testing"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestEscape(t *testing.T) {
tests := struct{ give, want string }{
"<p>some text</p>",
"<p>some text</p>",
}
assert.Eq(t, tests.want, strutil.EscapeHTML(tests.give))
ret := strutil.EscapeJS("<script>var a = 23;</script>")
assert.NotContains(t, ret, "<script>")
assert.NotContains(t, ret, "</script>")
}
func TestAddSlashes(t *testing.T) {
assert.Eq(t, "", strutil.AddSlashes(""))
assert.Eq(t, "", strutil.StripSlashes(""))
assert.Eq(t, `{\"key\": 123}`, strutil.AddSlashes(`{"key": 123}`))
assert.Eq(t, `{"key": 123}`, strutil.StripSlashes(`{\"key\": 123}`))
assert.Eq(t, `path\to`, strutil.StripSlashes(`path\\to`))
}
func TestURLEnDecode(t *testing.T) {
is := assert.New(t)
is.Eq("a.com/?name%3D%E4%BD%A0%E5%A5%BD", strutil.URLEncode("a.com/?name=你好"))
is.Eq("a.com/?name=你好", strutil.URLDecode("a.com/?name%3D%E4%BD%A0%E5%A5%BD"))
is.Eq("a.com", strutil.URLEncode("a.com"))
is.Eq("a.com", strutil.URLDecode("a.com"))
}
func TestBaseDecode(t *testing.T) {
is := assert.New(t)
is.Eq("GEZGCYTD", strutil.B32Encode("12abc"))
is.Eq("12abc", strutil.B32Decode("GEZGCYTD"))
// b23 hex
is.Eq("64P62OJ3", strutil.B32Hex.EncodeToString([]byte("12abc")))
// fmt.Println(time.Now().Format("20060102150405"))
dateStr := "20230908101122"
is.Eq("68O34CPG74O3GC9G64OJ4CG", strutil.B32Hex.EncodeToString([]byte(dateStr)))
is.Eq("YWJj", strutil.B64Encode("abc"))
is.Eq("abc", strutil.B64Decode("YWJj"))
is.Eq([]byte("YWJj"), strutil.B64EncodeBytes([]byte("abc")))
is.Eq([]byte("abc"), strutil.B64DecodeBytes([]byte("YWJj")))
}