Skip to content

Commit 524e633

Browse files
committed
add test for TestConvertByteArraysToBase64Recursive
1 parent 7f3f460 commit 524e633

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

core/utils/utils_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,55 @@ func TestConvertByteArraysToBase64(t *testing.T) {
151151
})
152152
}
153153
}
154+
155+
func TestConvertByteArraysToBase64Recursive(t *testing.T) {
156+
tests := []struct {
157+
name string
158+
input interface{}
159+
expected string // check if []byte was converted to base64 string
160+
}{
161+
{"nil", nil, ""},
162+
{"map with []byte", map[string]interface{}{"data": []byte("hello")}, "aGVsbG8="},
163+
{"slice with []byte", []interface{}{[]byte("test")}, "dGVzdA=="},
164+
{"nested map", map[string]interface{}{"level": map[string]interface{}{"data": []byte("nested")}}, "bmVzdGVk"},
165+
}
166+
167+
for _, tt := range tests {
168+
t.Run(tt.name, func(t *testing.T) {
169+
convertByteArraysToBase64Recursive(tt.input)
170+
171+
if tt.expected == "" {
172+
return
173+
}
174+
175+
// check if base64 string in the result
176+
found := findBase64String(tt.input)
177+
if found != tt.expected {
178+
t.Fatalf("Expected %s, got %s", tt.expected, found)
179+
}
180+
})
181+
}
182+
}
183+
184+
// helper to find base64 string in interface{}
185+
func findBase64String(data interface{}) string {
186+
switch v := data.(type) {
187+
case map[string]interface{}:
188+
for _, val := range v {
189+
if str := findBase64String(val); str != "" {
190+
return str
191+
}
192+
}
193+
case []interface{}:
194+
for _, val := range v {
195+
if str := findBase64String(val); str != "" {
196+
return str
197+
}
198+
}
199+
case string:
200+
if len(v) > 0 && v != "hello" && v != "test" && v != "nested" {
201+
return v
202+
}
203+
}
204+
return ""
205+
}

0 commit comments

Comments
 (0)