forked from cristalhq/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudience_test.go
72 lines (60 loc) · 1.28 KB
/
audience_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
70
71
72
package jwt
import (
"encoding/json"
"testing"
)
func TestAudienceMarshal(t *testing.T) {
f := func(got Audience, want string) {
t.Helper()
raw, err := json.Marshal(got)
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
if string(raw) != want {
t.Errorf("want %#v, got: %#v", want, string(raw))
}
}
f(nil, `""`)
f(Audience{}, `""`)
f(Audience{"admin"}, `"admin"`)
f(Audience{"admin", "co-admin"}, `["admin","co-admin"]`)
}
func TestAudienceUnmarshal(t *testing.T) {
f := func(got string, want Audience) {
t.Helper()
var a Audience
err := json.Unmarshal([]byte(got), &a)
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
if len(want) != len(a) {
t.Errorf("want %#v, got: %#v", len(want), len(a))
}
for i := range a {
if a[i] != want[i] {
t.Errorf("want %#v, got: %#v", want[i], a[i])
}
}
}
f(`[]`, Audience{})
f(`"admin"`, Audience{"admin"})
f(`["admin"]`, Audience{"admin"})
f(`["admin","co-admin"]`, Audience{"admin", "co-admin"})
}
func TestAudienceUnmarshalMalformed(t *testing.T) {
f := func(got string) {
t.Helper()
var a Audience
err := json.Unmarshal([]byte(got), &a)
if err == nil {
t.Error("want err")
}
}
f(``)
f(`abc12`)
f(`123`)
f(`{}`)
f(`[{}]`)
f(`["admin",{}]`)
f(`["admin",123]`)
}