github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zstring/encoding_test.go (about) 1 package zstring_test 2 3 import ( 4 "testing" 5 6 "github.com/sohaha/zlsgo" 7 "github.com/sohaha/zlsgo/zfile" 8 "github.com/sohaha/zlsgo/zhttp" 9 "github.com/sohaha/zlsgo/zstring" 10 ) 11 12 func TestBase64(t *testing.T) { 13 tt := zlsgo.NewTest(t) 14 str := "hi,是我" 15 strbyte := []byte(str) 16 s := zstring.Base64Encode(strbyte) 17 deByte, err := zstring.Base64Decode(s) 18 tt.EqualNil(err) 19 tt.Equal(strbyte, deByte) 20 21 s2 := zstring.Base64EncodeString(str) 22 tt.Equal(zstring.Bytes2String(s), s2) 23 24 de, err := zstring.Base64DecodeString(s2) 25 tt.EqualNil(err) 26 tt.Equal(str, de) 27 28 de, _ = zstring.Base64DecodeString(string(s)) 29 tt.Equal(str, de) 30 31 } 32 33 type testSt struct { 34 Name string 35 } 36 37 func TestSerialize(t *testing.T) { 38 tt := zlsgo.NewTest(t) 39 test := &testSt{"hi"} 40 41 s, err := zstring.Serialize(test) 42 tt.EqualNil(err) 43 44 v, err := zstring.UnSerialize(s, &testSt{}) 45 tt.EqualNil(err) 46 47 test2, ok := v.(*testSt) 48 tt.EqualTrue(ok) 49 tt.Equal(test.Name, test2.Name) 50 } 51 52 func TestImg2Base64(t *testing.T) { 53 tt := zlsgo.NewTest(t) 54 res, err := zhttp.Get("https://seekwe.73zls.com/signed/https:%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fa4bcc6b2-32ef-4a7d-ba1c-65a0330f632d%2Flogo.png") 55 if err == nil { 56 file := "tmp/logo.png" 57 err = res.ToFile(file) 58 if err == nil { 59 s, err := zstring.Img2Base64(file) 60 tt.EqualNil(err) 61 t.Log(s) 62 zfile.Rmdir("tmp") 63 } 64 } 65 }