-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sjwt_test.go
93 lines (77 loc) · 1.89 KB
/
sjwt_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package sjwt
import (
"testing"
)
var secretKey = []byte("whats up yall")
func TestGenerate(t *testing.T) {
claims := New()
claims.Set("hello", "world")
jwt := claims.Generate(secretKey)
if jwt == "" {
t.Error("jwt is empty")
}
}
func BenchmarkGenerate(b *testing.B) {
for i := 0; i < b.N; i++ {
claims := New()
claims.Set("hello", "world")
claims.Generate(secretKey)
}
}
func TestParse(t *testing.T) {
claims := New()
claims.Set("hello", "world")
jwt := claims.Generate(secretKey)
newClaims, err := Parse(jwt)
if err != nil {
t.Error("error parsing claims")
}
if !newClaims.Has("hello") {
t.Error("error getting claims hello from parsed claims")
}
hello, _ := newClaims.GetStr("hello")
if hello != "world" {
t.Error("error hello does not equal world")
}
}
func TestParseEmpty(t *testing.T) {
_, err := Parse("")
if err != ErrTokenInvalid {
t.Error("error should have failed to parse empty jwt")
}
}
func TestParseDecodeError(t *testing.T) {
_, err := Parse("..")
if err == nil {
t.Error("error should have failed to parse empty jwt")
}
}
func TestVerify(t *testing.T) {
claims := New()
claims.Set("hello", "world")
jwt := claims.Generate(secretKey)
verified := Verify(jwt, secretKey)
if !verified {
t.Error("verification failed")
}
verified = Verify(jwt, []byte("Bad secret"))
if verified {
t.Error("verification should have failed")
}
}
func TestVerifyError(t *testing.T) {
jwt := "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." +
"uk1qJnGuGHHGFw6fXpVILrdo52JqyD3EzvW3_DxhgZPAqU-OKzzPy7xdRNeQRba5CI6VGmlo6DBYqRCteiiOTw"
verified := Verify(jwt, secretKey)
if verified {
t.Error("verification should have failed")
}
}
func TestVerifyInvalidJWTError(t *testing.T) {
jwt := "not_a_jwt"
verified := Verify(jwt, secretKey)
if verified {
t.Error("verification should have failed")
}
}