-
Notifications
You must be signed in to change notification settings - Fork 19
/
sign_test.go
61 lines (50 loc) · 1.31 KB
/
sign_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
package jwt
import (
"reflect"
"testing"
"time"
)
// The actual implementation tests live inside token_test.go and each algorithm's test file.
func TestSignOption(t *testing.T) {
now := time.Date(2020, 10, 26, 1, 1, 1, 1, time.Local)
exp := now.Add(time.Second).Unix()
iat := now.Unix()
type claims struct {
Foo string `json:"foo"`
Issuer string `json:"iss"`
Expiry int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
}
expectedCustomClaims := claims{
"bar",
"issuer",
exp,
iat,
}
expectedStdClaims := Claims{Issuer: "issuer", Expiry: exp, IssuedAt: iat}
prevClock := Clock
t.Cleanup(func() {
Clock = prevClock
})
Clock = func() time.Time {
return now
}
token, err := Sign(testAlg, testSecret, Map{"foo": "bar"}, expectedStdClaims, MaxAge(time.Second))
if err != nil {
t.Fatal(err)
}
verifiedToken, err := Verify(testAlg, testSecret, token)
if err != nil {
t.Fatal(err)
}
if got := verifiedToken.StandardClaims; !reflect.DeepEqual(got, expectedStdClaims) {
t.Fatalf("expected standard claims:\n%#+v\n\nbut got:\n%#+v", expectedStdClaims, got)
}
var got claims
if err = verifiedToken.Claims(&got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expectedCustomClaims, got) {
t.Fatalf("expected custom claims:\n%#+v\n\nbut got:\n%#+v", expectedCustomClaims, got)
}
}