-
Notifications
You must be signed in to change notification settings - Fork 0
/
godorjwt_test.go
81 lines (72 loc) · 1.9 KB
/
godorjwt_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
package godorjwt
import (
"go.uber.org/goleak"
"testing"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestNew(t *testing.T) {
config := New("secret")
if config.Secret != "secret" {
t.Errorf("Expected secret to be secret, got %s", config.Secret)
}
if config.Expiry != 0 {
t.Errorf("Expected expiry to be 0, got %d", config.Expiry)
}
if config.Algorithm != "" {
t.Errorf("Expected algorithm to be empty, got %s", config.Algorithm)
}
}
func TestEncode(t *testing.T) {
config := New("secret")
payload := map[string]any{"name": "John Doe"}
token, _, _, err := Encode(payload, *config)
if err != nil {
t.Errorf("Expected error to be nil, got %s", err)
}
if token == "" {
t.Errorf("Expected token to be not empty, got %s", token)
}
}
func TestDecode(t *testing.T) {
config := New("secret")
payload := map[string]any{"name": "John Doe"}
token, _, _, _ := Encode(payload, *config)
decoded, err := Decode(token, *config)
if err != nil {
t.Errorf("Expected error to be nil, got %s", err)
}
if decoded["name"] != "John Doe" {
t.Errorf("Expected name to be John Doe, got %s", decoded["name"])
}
}
func TestDecodeWithEmptySecret(t *testing.T) {
config := New("")
payload := map[string]any{"name": "John Doe"}
token, _, _, _ := Encode(payload, *config)
_, err := Decode(token, *config)
if err == nil {
t.Errorf("Expected error to be not nil, got %s", err)
}
}
func TestDecodeWithInvalidToken(t *testing.T) {
config := New("secret")
_, err := Decode("invalid", *config)
if err == nil {
t.Errorf("Expected error to be not nil, got %s", err)
}
}
func TestDecodeWithEmptyToken(t *testing.T) {
config := New("secret")
_, err := Decode("", *config)
if err == nil {
t.Errorf("Expected error to be not nil, got %s", err)
}
}
func TestDecodeWithEmptyConfig(t *testing.T) {
_, err := Decode("", Config{})
if err == nil {
t.Errorf("Expected error to be not nil, got %s", err)
}
}