forked from gbrlsnchs/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
73 lines (69 loc) · 1.69 KB
/
time_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
73
package jwt_test
import (
"encoding/json"
"testing"
"time"
"github.com/gbrlsnchs/jwt/v3"
"github.com/gbrlsnchs/jwt/v3/internal"
"github.com/google/go-cmp/cmp"
)
func TestTimeMarshalJSON(t *testing.T) {
now := time.Now()
testCases := []struct {
tt jwt.Time
want int64
}{
{jwt.Time{}, 0},
{jwt.Time{now}, now.Unix()},
{jwt.Time{now.Add(24 * time.Hour)}, now.Add(24 * time.Hour).Unix()},
{jwt.Time{now.Add(24 * 30 * 12 * time.Hour)}, now.Add(24 * 30 * 12 * time.Hour).Unix()},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
b, err := tc.tt.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var n int64
if err = json.Unmarshal(b, &n); err != nil {
t.Fatal(err)
}
if want, got := tc.want, n; got != want {
t.Errorf("jwt.Time.Marshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}
func TestTimeUnmarshalJSON(t *testing.T) {
now := time.Now()
testCases := []struct {
n int64
want jwt.Time
isNil bool
}{
{now.Unix(), jwt.Time{now}, false},
{internal.Epoch.Unix() - 0xDEAD, jwt.Time{internal.Epoch}, false},
{internal.Epoch.Unix(), jwt.Time{internal.Epoch}, false},
{internal.Epoch.Unix() + 0xDEAD, jwt.Time{internal.Epoch.Add(0xDEAD * time.Second)}, false},
{0, jwt.Time{}, true},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
var n *int64
if !tc.isNil {
n = &tc.n
}
b, err := json.Marshal(n)
if err != nil {
t.Fatal(err)
}
var tt jwt.Time
if err = tt.UnmarshalJSON(b); err != nil {
t.Fatal(err)
}
if want, got := tc.want.Unix(), tt.Unix(); got != want {
t.Errorf("jwt.Time.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}