forked from ken109/gin-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
67 lines (48 loc) · 1.28 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package jwt
import (
"github.com/gin-gonic/gin"
"github.com/go-playground/assert/v2"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestVerifyEmptyToken(t *testing.T) {
res := httptest.NewRecorder()
c, _ := gin.CreateTestContext(res)
c.Request, _ = http.NewRequest("GET", "/", nil)
Verify(c)
assert.Equal(t, res.Code, http.StatusUnauthorized)
}
func TestVerifyInvalidToken(t *testing.T) {
pemBytes, err := ioutil.ReadFile("private.key")
if err != nil {
panic(err)
}
if err := SetUp(pemBytes, Option{}); err != nil {
panic(err)
}
res := httptest.NewRecorder()
c, _ := gin.CreateTestContext(res)
c.Request, _ = http.NewRequest("GET", "/", nil)
c.Request.Header.Add("Authorization", "test")
Verify(c)
assert.Equal(t, res.Code, http.StatusUnauthorized)
}
func TestVerifyValidToken(t *testing.T) {
pemBytes, err := ioutil.ReadFile("private.key")
if err != nil {
panic(err)
}
if err := SetUp(pemBytes, Option{}); err != nil {
panic(err)
}
res := httptest.NewRecorder()
c, _ := gin.CreateTestContext(res)
c.Request, _ = http.NewRequest("GET", "/", nil)
token, err := GetToken(Claims{})
c.Request.Header.Add("Authorization", "bearer "+string(token))
Verify(c)
assert.Equal(t, res.Code, http.StatusOK)
assert.Equal(t, GetClaims(c), Claims{})
}