forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwx_test.go
69 lines (59 loc) · 1.57 KB
/
jwx_test.go
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
package jwx_test
import (
"fmt"
"strings"
"testing"
"github.com/lestrrat-go/jwx"
"github.com/lestrrat-go/jwx/internal/json"
"github.com/stretchr/testify/assert"
)
type jsonUnmarshalWrapper struct {
buf []byte
}
func (w jsonUnmarshalWrapper) Decode(v interface{}) error {
return json.Unmarshal(w.buf, v)
}
func TestDecoderSetting(t *testing.T) {
const src = `{"foo": 1}`
for _, useNumber := range []bool{true, false} {
useNumber := useNumber
t.Run(fmt.Sprintf("jwx.WithUseNumber(%t)", useNumber), func(t *testing.T) {
if useNumber {
jwx.DecoderSettings(jwx.WithUseNumber(useNumber))
t.Cleanup(func() {
jwx.DecoderSettings(jwx.WithUseNumber(false))
})
}
// json.NewDecoder must be called AFTER the above jwx.DecoderSettings call
decoders := []struct {
Name string
Decoder interface{ Decode(interface{}) error }
}{
{Name: "Decoder", Decoder: json.NewDecoder(strings.NewReader(src))},
{Name: "Unmarshal", Decoder: jsonUnmarshalWrapper{buf: []byte(src)}},
}
for _, tc := range decoders {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
var m map[string]interface{}
if !assert.NoError(t, tc.Decoder.Decode(&m), `Decode should succeed`) {
return
}
v, ok := m["foo"]
if !assert.True(t, ok, `m["foo"] should exist`) {
return
}
if useNumber {
if !assert.Equal(t, json.Number("1"), v, `v should be a json.Number object`) {
return
}
} else {
if !assert.Equal(t, float64(1), v, `v should be a float64`) {
return
}
}
})
}
})
}
}