1
2
3 package jwa_test
4
5 import (
6 "testing"
7
8 "github.com/lestrrat-go/jwx/jwa"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestCompressionAlgorithm(t *testing.T) {
13 t.Parallel()
14 t.Run(`accept jwa constant Deflate`, func(t *testing.T) {
15 t.Parallel()
16 var dst jwa.CompressionAlgorithm
17 if !assert.NoError(t, dst.Accept(jwa.Deflate), `accept is successful`) {
18 return
19 }
20 if !assert.Equal(t, jwa.Deflate, dst, `accepted value should be equal to constant`) {
21 return
22 }
23 })
24 t.Run(`accept the string DEF`, func(t *testing.T) {
25 t.Parallel()
26 var dst jwa.CompressionAlgorithm
27 if !assert.NoError(t, dst.Accept("DEF"), `accept is successful`) {
28 return
29 }
30 if !assert.Equal(t, jwa.Deflate, dst, `accepted value should be equal to constant`) {
31 return
32 }
33 })
34 t.Run(`accept fmt.Stringer for DEF`, func(t *testing.T) {
35 t.Parallel()
36 var dst jwa.CompressionAlgorithm
37 if !assert.NoError(t, dst.Accept(stringer{src: "DEF"}), `accept is successful`) {
38 return
39 }
40 if !assert.Equal(t, jwa.Deflate, dst, `accepted value should be equal to constant`) {
41 return
42 }
43 })
44 t.Run(`stringification for DEF`, func(t *testing.T) {
45 t.Parallel()
46 if !assert.Equal(t, "DEF", jwa.Deflate.String(), `stringified value matches`) {
47 return
48 }
49 })
50 t.Run(`accept jwa constant NoCompress`, func(t *testing.T) {
51 t.Parallel()
52 var dst jwa.CompressionAlgorithm
53 if !assert.NoError(t, dst.Accept(jwa.NoCompress), `accept is successful`) {
54 return
55 }
56 if !assert.Equal(t, jwa.NoCompress, dst, `accepted value should be equal to constant`) {
57 return
58 }
59 })
60 t.Run(`accept the string `, func(t *testing.T) {
61 t.Parallel()
62 var dst jwa.CompressionAlgorithm
63 if !assert.NoError(t, dst.Accept(""), `accept is successful`) {
64 return
65 }
66 if !assert.Equal(t, jwa.NoCompress, dst, `accepted value should be equal to constant`) {
67 return
68 }
69 })
70 t.Run(`accept fmt.Stringer for `, func(t *testing.T) {
71 t.Parallel()
72 var dst jwa.CompressionAlgorithm
73 if !assert.NoError(t, dst.Accept(stringer{src: ""}), `accept is successful`) {
74 return
75 }
76 if !assert.Equal(t, jwa.NoCompress, dst, `accepted value should be equal to constant`) {
77 return
78 }
79 })
80 t.Run(`stringification for `, func(t *testing.T) {
81 t.Parallel()
82 if !assert.Equal(t, "", jwa.NoCompress.String(), `stringified value matches`) {
83 return
84 }
85 })
86 t.Run(`bail out on random integer value`, func(t *testing.T) {
87 t.Parallel()
88 var dst jwa.CompressionAlgorithm
89 if !assert.Error(t, dst.Accept(1), `accept should fail`) {
90 return
91 }
92 })
93 t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
94 t.Parallel()
95 var dst jwa.CompressionAlgorithm
96 if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
97 return
98 }
99 })
100 t.Run(`check list of elements`, func(t *testing.T) {
101 t.Parallel()
102 var expected = map[jwa.CompressionAlgorithm]struct{}{
103 jwa.Deflate: {},
104 jwa.NoCompress: {},
105 }
106 for _, v := range jwa.CompressionAlgorithms() {
107 if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) {
108 return
109 }
110 delete(expected, v)
111 }
112 if !assert.Len(t, expected, 0) {
113 return
114 }
115 })
116 }
117
View as plain text