2026-04-22 13:48:27 +08:00
|
|
|
package encoding_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-05-06 00:11:50 +08:00
|
|
|
"apigo.cc/go/cast"
|
2026-04-22 13:48:27 +08:00
|
|
|
"apigo.cc/go/encoding"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// --- Hex ---
|
|
|
|
|
func TestHex(t *testing.T) {
|
|
|
|
|
data := []byte("hello go")
|
|
|
|
|
encoded := encoding.HexToString(data)
|
|
|
|
|
decoded, err := encoding.UnHex([]byte(encoded))
|
|
|
|
|
if err != nil || !bytes.Equal(data, decoded) {
|
|
|
|
|
t.Fatal("Hex roundtrip failed")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 00:11:50 +08:00
|
|
|
if !bytes.Equal(cast.As(encoding.UnHexFromString(encoded)), data) {
|
|
|
|
|
t.Error("UnHexFromString with cast.As failed")
|
2026-04-22 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-06 00:11:50 +08:00
|
|
|
if len(cast.As(encoding.UnHexFromString("!@#$"))) != 0 {
|
|
|
|
|
t.Error("UnHexFromString should return empty for invalid hex chars with cast.As")
|
2026-04-22 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Base64 ---
|
|
|
|
|
func TestBase64(t *testing.T) {
|
|
|
|
|
data := []byte("hello world")
|
|
|
|
|
|
|
|
|
|
// Standard
|
|
|
|
|
enc := encoding.Base64ToString(data)
|
|
|
|
|
dec, err := encoding.UnBase64([]byte(enc))
|
|
|
|
|
if err != nil || !bytes.Equal(data, dec) {
|
|
|
|
|
t.Error("Base64 roundtrip failed")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 00:11:50 +08:00
|
|
|
if !bytes.Equal(cast.As(encoding.UnBase64FromString(enc)), data) {
|
|
|
|
|
t.Error("UnBase64FromString with cast.As failed")
|
2026-04-22 13:48:27 +08:00
|
|
|
}
|
2026-05-06 01:11:39 +08:00
|
|
|
|
|
|
|
|
// Raw (Unpadded)
|
|
|
|
|
rawEnc := encoding.Base64RawToString(data)
|
|
|
|
|
if bytes.HasSuffix([]byte(rawEnc), []byte("=")) {
|
|
|
|
|
t.Error("Base64Raw should not have padding")
|
|
|
|
|
}
|
|
|
|
|
rawDec, err := encoding.UnBase64FromString(rawEnc)
|
|
|
|
|
if err != nil || !bytes.Equal(data, rawDec) {
|
|
|
|
|
t.Error("Base64Raw smart decoding failed")
|
|
|
|
|
}
|
2026-04-22 13:48:27 +08:00
|
|
|
|
|
|
|
|
// URL
|
2026-05-06 01:11:39 +08:00
|
|
|
uData := []byte("hello/world+")
|
|
|
|
|
uEnc := encoding.UrlBase64ToString(uData)
|
2026-04-22 13:48:27 +08:00
|
|
|
uDec, _ := encoding.UnUrlBase64([]byte(uEnc))
|
2026-05-06 01:11:39 +08:00
|
|
|
if !bytes.Equal(uData, uDec) {
|
2026-04-22 13:48:27 +08:00
|
|
|
t.Error("UrlBase64 roundtrip failed")
|
|
|
|
|
}
|
2026-05-06 00:11:50 +08:00
|
|
|
|
2026-05-06 01:11:39 +08:00
|
|
|
if !bytes.Equal(cast.As(encoding.UnUrlBase64FromString(uEnc)), uData) {
|
2026-05-06 00:11:50 +08:00
|
|
|
t.Error("UnUrlBase64FromString with cast.As failed")
|
|
|
|
|
}
|
2026-05-06 01:11:39 +08:00
|
|
|
|
|
|
|
|
// URL Raw (Unpadded)
|
|
|
|
|
uRawEnc := encoding.UrlBase64RawToString(uData)
|
|
|
|
|
if bytes.HasSuffix([]byte(uRawEnc), []byte("=")) {
|
|
|
|
|
t.Error("UrlBase64Raw should not have padding")
|
|
|
|
|
}
|
|
|
|
|
uRawDec, err := encoding.UnUrlBase64FromString(uRawEnc)
|
|
|
|
|
if err != nil || !bytes.Equal(uData, uRawDec) {
|
|
|
|
|
t.Error("UrlBase64Raw smart decoding failed")
|
|
|
|
|
}
|
2026-04-22 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Web ---
|
|
|
|
|
func TestWebEncoding(t *testing.T) {
|
|
|
|
|
// URL
|
|
|
|
|
data := []byte("a b+c")
|
|
|
|
|
enc := encoding.UrlEncode(data)
|
2026-05-06 00:11:50 +08:00
|
|
|
if !bytes.Equal(cast.As(encoding.UnUrlEncode(enc)), data) {
|
2026-04-22 13:48:27 +08:00
|
|
|
t.Error("UrlEncode roundtrip failed")
|
|
|
|
|
}
|
2026-05-06 00:11:50 +08:00
|
|
|
if len(cast.As(encoding.UnUrlEncode("%ZZ"))) != 0 {
|
|
|
|
|
t.Error("UnUrlEncode should return empty for invalid input with cast.As")
|
2026-04-22 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTML
|
|
|
|
|
htmlData := []byte("<script>")
|
|
|
|
|
escaped := encoding.HtmlEscape(htmlData)
|
2026-05-06 00:11:50 +08:00
|
|
|
if encoding.HtmlUnescape(escaped) != string(htmlData) {
|
2026-04-22 13:48:27 +08:00
|
|
|
t.Error("HtmlEscape roundtrip failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Utf8 ---
|
|
|
|
|
func TestUtf8(t *testing.T) {
|
|
|
|
|
if !encoding.Utf8Valid([]byte("你好")) {
|
|
|
|
|
t.Error("Valid UTF-8 should pass")
|
|
|
|
|
}
|
|
|
|
|
if encoding.Utf8Valid([]byte{0xff, 0xff}) {
|
|
|
|
|
t.Error("Invalid UTF-8 should fail")
|
|
|
|
|
}
|
|
|
|
|
}
|