File: base64_test.go

package info (click to toggle)
golang-github-lestrrat-go-jwx 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: sh: 222; makefile: 86; perl: 62
file content (49 lines) | stat: -rw-r--r-- 950 bytes parent folder | download
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
package base64

import (
	"encoding/base64"
	"testing"

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

func TestDecode(t *testing.T) {
	testcases := []struct {
		Name     string
		Encoding *base64.Encoding
	}{
		{
			Name:     "base64.RawURLEncoding",
			Encoding: base64.RawURLEncoding,
		},
		{
			Name:     "base64.URLEncoding",
			Encoding: base64.URLEncoding,
		},
		{
			Name:     "base64.RawStdEncoding",
			Encoding: base64.RawStdEncoding,
		},
		{
			Name:     "base64.StdEncoding",
			Encoding: base64.StdEncoding,
		},
	}

	var payload = []byte("Hello, World")
	for _, tc := range testcases {
		tc := tc
		t.Run(tc.Name, func(t *testing.T) {
			dst := make([]byte, tc.Encoding.EncodedLen(len(payload)))
			tc.Encoding.Encode(dst, payload)

			decoded, err := Decode(dst)
			if !assert.NoError(t, err, `Decode should succeed`) {
				return
			}
			if !assert.Equal(t, payload, decoded, `decoded content should match`) {
				return
			}
		})
	}
}