| 1 |
package jwt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/golang-jwt/jwt/v4" |
| 7 |
) |
| 8 |
|
| 9 |
func TestToken_SigningString(t1 *testing.T) { |
| 10 |
type fields struct { |
| 11 |
Raw string |
| 12 |
Method jwt.SigningMethod |
| 13 |
Header map[string]interface{} |
| 14 |
Claims jwt.Claims |
| 15 |
Signature string |
| 16 |
Valid bool |
| 17 |
} |
| 18 |
tests := []struct { |
| 19 |
name string |
| 20 |
fields fields |
| 21 |
want string |
| 22 |
wantErr bool |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "", |
| 26 |
fields: fields{ |
| 27 |
Raw: "", |
| 28 |
Method: jwt.SigningMethodHS256, |
| 29 |
Header: map[string]interface{}{ |
| 30 |
"typ": "JWT", |
| 31 |
"alg": jwt.SigningMethodHS256.Alg(), |
| 32 |
}, |
| 33 |
Claims: jwt.StandardClaims{}, |
| 34 |
Signature: "", |
| 35 |
Valid: false, |
| 36 |
}, |
| 37 |
want: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30", |
| 38 |
wantErr: false, |
| 39 |
}, |
| 40 |
} |
| 41 |
for _, tt := range tests { |
| 42 |
t1.Run(tt.name, func(t1 *testing.T) { |
| 43 |
t := &jwt.Token{ |
| 44 |
Raw: tt.fields.Raw, |
| 45 |
Method: tt.fields.Method, |
| 46 |
Header: tt.fields.Header, |
| 47 |
Claims: tt.fields.Claims, |
| 48 |
Signature: tt.fields.Signature, |
| 49 |
Valid: tt.fields.Valid, |
| 50 |
} |
| 51 |
got, err := t.SigningString() |
| 52 |
if (err != nil) != tt.wantErr { |
| 53 |
t1.Errorf("SigningString() error = %v, wantErr %v", err, tt.wantErr) |
| 54 |
return |
| 55 |
} |
| 56 |
if got != tt.want { |
| 57 |
t1.Errorf("SigningString() got = %v, want %v", got, tt.want) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func BenchmarkToken_SigningString(b *testing.B) { |
| 64 |
t := &jwt.Token{ |
| 65 |
Method: jwt.SigningMethodHS256, |
| 66 |
Header: map[string]interface{}{ |
| 67 |
"typ": "JWT", |
| 68 |
"alg": jwt.SigningMethodHS256.Alg(), |
| 69 |
}, |
| 70 |
Claims: jwt.StandardClaims{}, |
| 71 |
} |
| 72 |
b.Run("BenchmarkToken_SigningString", func(b *testing.B) { |
| 73 |
b.ResetTimer() |
| 74 |
b.ReportAllocs() |
| 75 |
for i := 0; i < b.N; i++ { |
| 76 |
t.SigningString() |
| 77 |
} |
| 78 |
}) |
| 79 |
} |