-
Notifications
You must be signed in to change notification settings - Fork 664
/
jwt_test.go
38 lines (34 loc) · 1.34 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
package helper
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/process"
)
func TestJwt(t *testing.T) {
data := map[string]interface{}{"hello": "world", "id": 1}
option := map[string]interface{}{"subject": "Unit Test", "audience": "Test", "issuer": "UnitTest", "timeout": 1, "sid": ""}
token := JwtMake(1, data, option)
tokenString := token.Token
res := JwtValidate(tokenString)
assert.NotNil(t, res)
assert.Equal(t, float64(1), res.Data["id"])
assert.Equal(t, "world", res.Data["hello"])
assert.Equal(t, "UnitTest", res.Issuer)
time.Sleep(2 * time.Second)
assert.Panics(t, func() { JwtValidate(tokenString) })
}
func TestProcessJwt(t *testing.T) {
data := map[string]interface{}{"hello": "world", "id": 1}
option := map[string]interface{}{"subject": "Unit Test", "audience": "Test", "issuer": "UnitTest", "timeout": 1, "sid": ""}
args := []interface{}{1, data, option}
p := process.New("xiang.helper.JwtMake", args...)
token := p.Run().(JwtToken)
tokenString := token.Token
res := process.New("xiang.helper.JwtValidate", tokenString).Run().(*JwtClaims)
assert.Equal(t, float64(1), res.Data["id"])
assert.Equal(t, "world", res.Data["hello"])
assert.Equal(t, "UnitTest", res.Issuer)
time.Sleep(2 * time.Second)
assert.Panics(t, func() { process.New("xiang.helper.JwtValidate", tokenString).Run() })
}