File size: 2,057 Bytes
e089492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package stringext

import (
	"encoding/base64"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestNormalizeSpace(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{name: "empty string", input: "", expected: ""},
		{name: "converts CRLF", input: "a\r\nb", expected: "a\nb"},
		{name: "converts tabs to four spaces", input: "\ta", expected: "    a"},
		{name: "trims leading newlines", input: "\n\nfoo", expected: "foo"},
		{name: "trims trailing newlines", input: "foo\n\n", expected: "foo"},
		{
			name:     "preserves first line indentation",
			input:    "\t\t\tresp, _ := json.Marshal()\n\t\t\t\t_ = resp\n",
			expected: "            resp, _ := json.Marshal()\n                _ = resp",
		},
		{
			name:     "preserves first line spaces",
			input:    "   indented\n    more",
			expected: "   indented\n    more",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			require.Equal(t, tt.expected, NormalizeSpace(tt.input))
		})
	}
}

func TestIsValidBase64(t *testing.T) {
	t.Parallel()

	// Real PNG header encoded in standard base64.
	pngHeader := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
	pngBase64 := base64.StdEncoding.EncodeToString(pngHeader)

	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		{name: "empty string", input: "", expected: false},
		{name: "valid no padding", input: "SGVsbG8gV29ybGQh", expected: true},
		{name: "valid with padding", input: "YQ==", expected: true},
		{name: "non-ASCII bytes", input: "abc\x80def", expected: false},
		{name: "ASCII but not base64", input: "hello world!!!", expected: false},
		{name: "raw encoding no padding", input: "YQ", expected: false},
		{name: "trailing whitespace", input: "YQ==\n", expected: false},
		{name: "valid PNG header base64", input: pngBase64, expected: true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			require.Equal(t, tt.expected, IsValidBase64(tt.input))
		})
	}
}