forked from cristalhq/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric_date_test.go
74 lines (61 loc) · 1.32 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
72
73
74
package jwt
import (
"strconv"
"testing"
"time"
)
func TestNumericDateMarshal(t *testing.T) {
f := func(got *NumericDate, want string) {
t.Helper()
raw, err := got.MarshalJSON()
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
if string(raw) != want {
t.Errorf("want %#v, got: %#v", want, string(raw))
}
}
now := time.Now()
nowTS := now.Unix()
f(NewNumericDate(time.Time{}), `null`)
f(NewNumericDate(now), strconv.Itoa(int(nowTS)))
}
func TestNumericDateUnmarshal(t *testing.T) {
f := func(s string, want NumericDate) {
t.Helper()
var got NumericDate
err := got.UnmarshalJSON([]byte(s))
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
if got.Unix() != want.Unix() {
t.Errorf("want %#v, got %#v", want.Unix(), got.Unix())
}
}
f(`1588707274`, asNumericDate(1588707274))
f(`1588707274.3769999`, asNumericDate(1588707274))
f(`"12345"`, asNumericDate(12345))
}
func TestNumericDateUnmarshalMalformed(t *testing.T) {
f := func(got string) {
t.Helper()
var nd NumericDate
err := nd.UnmarshalJSON([]byte(got))
if err == nil {
t.Error("want err")
}
}
f(``)
f(`{}`)
f(`[{}]`)
f(`abc12`)
f(`"abc"`)
f(`["admin",{}]`)
f(`["admin",123]`)
f(`{}`)
f(`[]`)
f(`1e+309`)
}
func asNumericDate(n int64) NumericDate {
return *NewNumericDate(time.Unix(n, 0))
}