-
Notifications
You must be signed in to change notification settings - Fork 11
/
jwt_benchmark_test.go
67 lines (54 loc) · 1.46 KB
/
jwt_benchmark_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 echojwt
import (
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
"net/http"
"net/http/httptest"
"testing"
)
func BenchmarkJWTSuccessPath(b *testing.B) {
e := echo.New()
e.GET("/", func(c echo.Context) error {
token := c.Get("user").(*jwt.Token)
return c.JSON(http.StatusTeapot, token.Claims)
})
b.ReportAllocs()
mw, err := Config{SigningKey: []byte("secret")}.ToMiddleware()
if err != nil {
b.Fatal(err)
}
e.Use(mw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderAuthorization, "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")
res := httptest.NewRecorder()
e.ServeHTTP(res, req)
if res.Code != http.StatusUnauthorized {
b.Failed()
}
}
}
func BenchmarkJWTErrorPath(b *testing.B) {
e := echo.New()
e.GET("/", func(c echo.Context) error {
token := c.Get("user").(*jwt.Token)
return c.JSON(http.StatusTeapot, token.Claims)
})
b.ReportAllocs()
mw, err := Config{SigningKey: []byte("secret")}.ToMiddleware()
if err != nil {
b.Fatal(err)
}
e.Use(mw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderAuthorization, "Bearer x.x.x")
res := httptest.NewRecorder()
e.ServeHTTP(res, req)
if res.Code != http.StatusUnauthorized {
b.Failed()
}
}
}