136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
package encoding_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"apigo.cc/go/cast"
|
|
"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")
|
|
}
|
|
|
|
if !bytes.Equal(cast.As(encoding.UnHexFromString(encoded)), data) {
|
|
t.Error("UnHexFromString with cast.As failed")
|
|
}
|
|
|
|
if len(cast.As(encoding.UnHexFromString("!@#$"))) != 0 {
|
|
t.Error("UnHexFromString should return empty for invalid hex chars with cast.As")
|
|
}
|
|
}
|
|
|
|
// --- 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")
|
|
}
|
|
|
|
if !bytes.Equal(cast.As(encoding.UnBase64FromString(enc)), data) {
|
|
t.Error("UnBase64FromString with cast.As failed")
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
// URL
|
|
uData := []byte("hello/world+")
|
|
uEnc := encoding.UrlBase64ToString(uData)
|
|
uDec, _ := encoding.UnUrlBase64([]byte(uEnc))
|
|
if !bytes.Equal(uData, uDec) {
|
|
t.Error("UrlBase64 roundtrip failed")
|
|
}
|
|
|
|
if !bytes.Equal(cast.As(encoding.UnUrlBase64FromString(uEnc)), uData) {
|
|
t.Error("UnUrlBase64FromString with cast.As failed")
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|
|
|
|
// --- Web ---
|
|
func TestWebEncoding(t *testing.T) {
|
|
// URL
|
|
data := []byte("a b+c")
|
|
enc := encoding.UrlEncode(data)
|
|
if !bytes.Equal(cast.As(encoding.UnUrlEncode(enc)), data) {
|
|
t.Error("UrlEncode roundtrip failed")
|
|
}
|
|
if len(cast.As(encoding.UnUrlEncode("%ZZ"))) != 0 {
|
|
t.Error("UnUrlEncode should return empty for invalid input with cast.As")
|
|
}
|
|
|
|
// HTML
|
|
htmlData := []byte("<script>")
|
|
escaped := encoding.HtmlEscape(htmlData)
|
|
if encoding.HtmlUnescape(escaped) != string(htmlData) {
|
|
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")
|
|
}
|
|
}
|
|
|
|
// --- SortJoin ---
|
|
func TestSortJoin(t *testing.T) {
|
|
m := map[string]any{
|
|
"B": 2,
|
|
"A": 1,
|
|
"C": "hello world",
|
|
}
|
|
|
|
// Test Map
|
|
res := encoding.SortJoin(m, "&", "=", true)
|
|
expected := "A=1&B=2&C=hello+world"
|
|
if res != expected {
|
|
t.Errorf("SortJoin Map failed: expected %s, got %s", expected, res)
|
|
}
|
|
|
|
// Test Struct
|
|
type User struct {
|
|
Name string `json:"name"`
|
|
Age int `json:"age"`
|
|
ID string
|
|
}
|
|
u := User{Name: "Alice", Age: 30, ID: "123"}
|
|
resStruct := encoding.SortJoin(u, ";", ":", false)
|
|
// Keys: age, name, ID. Sorted: ID, age, name
|
|
expectedStruct := "ID:123;age:30;name:Alice"
|
|
if resStruct != expectedStruct {
|
|
t.Errorf("SortJoin Struct failed: expected %s, got %s", expectedStruct, resStruct)
|
|
}
|
|
}
|