(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestDecodeBase64FileContent(t *testing.T) { |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | input func() string // build the raw API-style input |
| 18 | expected string |
| 19 | wantErr bool |
| 20 | }{ |
| 21 | { |
| 22 | name: "plain base64 without newlines", |
| 23 | input: func() string { |
| 24 | return base64.StdEncoding.EncodeToString([]byte("hello world")) |
| 25 | }, |
| 26 | expected: "hello world", |
| 27 | }, |
| 28 | { |
| 29 | name: "GitHub API style with embedded newlines every 60 chars", |
| 30 | input: func() string { |
| 31 | encoded := base64.StdEncoding.EncodeToString([]byte("hello world")) |
| 32 | // Simulate GitHub API line-wrapping at 60 characters |
| 33 | var sb strings.Builder |
| 34 | for i, c := range encoded { |
| 35 | if i > 0 && i%60 == 0 { |
| 36 | sb.WriteByte('\n') |
| 37 | } |
| 38 | sb.WriteRune(c) |
| 39 | } |
| 40 | return sb.String() |
| 41 | }, |
| 42 | expected: "hello world", |
| 43 | }, |
| 44 | { |
| 45 | name: "leading and trailing whitespace stripped", |
| 46 | input: func() string { |
| 47 | return " " + base64.StdEncoding.EncodeToString([]byte("trim me")) + "\n" |
| 48 | }, |
| 49 | expected: "trim me", |
| 50 | }, |
| 51 | { |
| 52 | name: "binary content round-trips correctly", |
| 53 | input: func() string { |
| 54 | data := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE} |
| 55 | return base64.StdEncoding.EncodeToString(data) |
| 56 | }, |
| 57 | expected: string([]byte{0x00, 0x01, 0x02, 0xFF, 0xFE}), |
| 58 | }, |
| 59 | { |
| 60 | name: "invalid base64 returns error", |
| 61 | input: func() string { return "!!!not-valid-base64!!!" }, |
| 62 | wantErr: true, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | for _, tt := range tests { |
| 67 | t.Run(tt.name, func(t *testing.T) { |
| 68 | got, err := decodeBase64FileContent(tt.input()) |
| 69 | if tt.wantErr { |
| 70 | assert.Error(t, err, "expected an error for invalid base64 input") |
| 71 | return |
nothing calls this directly
no test coverage detected