-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwt_test.go
53 lines (43 loc) · 1.31 KB
/
jwt_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
package jwt
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestJwtTokenUtil(t *testing.T) {
tests := []struct {
name string
username string
}{
{
name: "test1",
username: "jack",
},
}
var jwtUtil = NewJwtTokenUtil("123456", 2, "Bearer ")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
token, err := jwtUtil.GenerateToken(tt.username)
assert.Equal(t, nil, err)
username := jwtUtil.GetUserNameFromToken(token)
assert.Equal(t, tt.username, username, "应该解析出用户名")
expiration := jwtUtil.getExpiredDateFromToken(token)
assert.NotEqual(t, time.Time{}, expiration, "应该解析出过期时间")
err = jwtUtil.ValidateToken(token, "nick")
assert.NotNil(t, err)
err = jwtUtil.ValidateToken(token, "jack")
assert.Nil(t, err)
newToken, err := jwtUtil.RefreshHeadToken(token, 1)
assert.Equal(t, nil, err)
assert.Equal(t, token, newToken, "无需刷新, 返回的应该是原始token")
time.Sleep(time.Second * 1)
newToken, err = jwtUtil.RefreshHeadToken(token, 1)
assert.Equal(t, nil, err)
assert.NotEqual(t, token, newToken, "token应该刷新")
// token过期失效
time.Sleep(time.Second * 2)
err = jwtUtil.ValidateToken(token, "jack")
assert.NotNil(t, err, "token应该过期")
})
}
}