-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
48 lines (39 loc) · 1.41 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
package jwt
import "testing"
func Test_NewHS256(t *testing.T) {
expected := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI"
key := []byte("your-256-bit-secret")
claims := &Claims{
Subject: "1234567890",
IssuedAt: 1516239022,
}
token, err := NewHS256(key, claims)
if err != nil {
t.Error(err)
t.Fail()
}
if string(token) != expected {
t.Fail()
}
}
func Test_ParseHS256(t *testing.T) {
token := []byte("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI")
malformedTokens := []string{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEw",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAif.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ.J3p7EuXSUS_3nmNgw9ZK0jJgdSEq5VOOJ52psItXEwI",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsInN1YiI6IjEyMzQ1Njc4OTAifQ",
}
key := []byte("your-256-bit-secret")
claims := new(Claims)
err := ParseHS256(key, token, claims)
if err != ErrExpiredToken {
t.Fail()
}
for _, v := range malformedTokens {
err = ParseHS256(key, []byte(v), claims)
if err == nil {
t.Fail()
}
}
}