-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric_date_test.go
71 lines (62 loc) · 1.25 KB
/
numeric_date_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
package paseto
import (
"strconv"
"testing"
"time"
)
func TestNumericDateMarshal(t *testing.T) {
now := time.Now()
nowTS := now.Unix()
testCases := []struct {
value *NumericDate
want string
}{
{NewNumericDate(time.Time{}), `null`},
{NewNumericDate(now), strconv.Itoa(int(nowTS))},
}
for _, tc := range testCases {
raw, err := tc.value.MarshalJSON()
mustOk(t, err)
mustEqual(t, string(raw), tc.want)
}
}
func TestNumericDateUnmarshal(t *testing.T) {
testCases := []struct {
s string
want NumericDate
}{
{`1588707274`, asNumericDate(1588707274)},
{`1588707274.3769999`, asNumericDate(1588707274)},
{`"12345"`, asNumericDate(12345)},
}
for _, tc := range testCases {
var have NumericDate
err := have.UnmarshalJSON([]byte(tc.s))
mustOk(t, err)
mustEqual(t, have.Unix(), tc.want.Unix())
}
}
func TestNumericDateUnmarshalMalformed(t *testing.T) {
testCases := []struct {
value string
}{
{``},
{`{}`},
{`[{}]`},
{`abc12`},
{`"abc"`},
{`["admin",{}]`},
{`["admin",123]`},
{`{}`},
{`[]`},
{`1e+309`},
}
for _, tc := range testCases {
var nd NumericDate
err := nd.UnmarshalJSON([]byte(tc.value))
mustFail(t, err)
}
}
func asNumericDate(n int64) NumericDate {
return *NewNumericDate(time.Unix(n, 0))
}