forked from golang-jwt/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unnecessary for loop in token signing string for readability (g…
…olang-jwt#34) * remove unnecessary for loop in token signing string for readability - add testcase - add benchmark - improve performance slightly * Fix benchtests on token_test.go * Update token_test.go to v4 Co-authored-by: hyeonjae <[email protected]> Co-authored-by: Luis Gabriel Gomez <[email protected]>
- Loading branch information
1 parent
78a18c0
commit e01ed05
Showing
2 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
func TestToken_SigningString(t1 *testing.T) { | ||
type fields struct { | ||
Raw string | ||
Method jwt.SigningMethod | ||
Header map[string]interface{} | ||
Claims jwt.Claims | ||
Signature string | ||
Valid bool | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "", | ||
fields: fields{ | ||
Raw: "", | ||
Method: jwt.SigningMethodHS256, | ||
Header: map[string]interface{}{ | ||
"typ": "JWT", | ||
"alg": jwt.SigningMethodHS256.Alg(), | ||
}, | ||
Claims: jwt.StandardClaims{}, | ||
Signature: "", | ||
Valid: false, | ||
}, | ||
want: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t1.Run(tt.name, func(t1 *testing.T) { | ||
t := &jwt.Token{ | ||
Raw: tt.fields.Raw, | ||
Method: tt.fields.Method, | ||
Header: tt.fields.Header, | ||
Claims: tt.fields.Claims, | ||
Signature: tt.fields.Signature, | ||
Valid: tt.fields.Valid, | ||
} | ||
got, err := t.SigningString() | ||
if (err != nil) != tt.wantErr { | ||
t1.Errorf("SigningString() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t1.Errorf("SigningString() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkToken_SigningString(b *testing.B) { | ||
t := &jwt.Token{ | ||
Method: jwt.SigningMethodHS256, | ||
Header: map[string]interface{}{ | ||
"typ": "JWT", | ||
"alg": jwt.SigningMethodHS256.Alg(), | ||
}, | ||
Claims: jwt.StandardClaims{}, | ||
} | ||
b.Run("BenchmarkToken_SigningString", func(b *testing.B) { | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
for i := 0; i<b.N; i++ { | ||
t.SigningString() | ||
} | ||
}) | ||
} |