Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5 Pre-Release #234

Merged
merged 12 commits into from
Feb 21, 2023
Prev Previous commit
Next Next commit
Adding more coverage (#268)
  • Loading branch information
oxisto authored Feb 20, 2023
commit fa7a12b45fb49d6d2f66730e1eb3d071caee078e
53 changes: 53 additions & 0 deletions map_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,56 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
}
}

func TestMapClaims_ParseString(t *testing.T) {
type args struct {
key string
}
tests := []struct {
name string
m MapClaims
args args
want string
wantErr bool
}{
{
name: "missing key",
m: MapClaims{},
args: args{
key: "mykey",
},
want: "",
wantErr: false,
},
{
name: "wrong key type",
m: MapClaims{"mykey": 4},
args: args{
key: "mykey",
},
want: "",
wantErr: true,
},
{
name: "correct key type",
m: MapClaims{"mykey": "mystring"},
args: args{
key: "mykey",
},
want: "mystring",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.m.ParseString(tt.args.key)
oxisto marked this conversation as resolved.
Show resolved Hide resolved
if (err != nil) != tt.wantErr {
t.Errorf("MapClaims.ParseString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("MapClaims.ParseString() = %v, want %v", got, tt.want)
}
})
}
}
31 changes: 29 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ var jwtTestData = []struct {
parser *jwt.Parser
signingMethod jwt.SigningMethod // The method to sign the JWT token for test purpose
}{
{
"invalid JWT",
"thisisnotreallyajwt",
defaultKeyFunc,
nil,
false,
jwt.ValidationErrorMalformed,
[]error{jwt.ErrTokenMalformed},
nil,
jwt.SigningMethodRS256,
},
{
"bearer in JWT",
"bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
defaultKeyFunc,
nil,
false,
jwt.ValidationErrorMalformed,
[]error{jwt.ErrTokenMalformed},
nil,
jwt.SigningMethodRS256,
},
{
"basic",
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
Expand Down Expand Up @@ -371,10 +393,12 @@ func TestParser_Parse(t *testing.T) {
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)
case *jwt.RegisteredClaims:
token, err = parser.ParseWithClaims(data.tokenString, &jwt.RegisteredClaims{}, data.keyfunc)
case nil:
token, err = parser.ParseWithClaims(data.tokenString, nil, data.keyfunc)
}

// Verify result matches expectation
if !reflect.DeepEqual(data.claims, token.Claims) {
if data.claims != nil && !reflect.DeepEqual(data.claims, token.Claims) {
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
}

Expand All @@ -386,7 +410,10 @@ func TestParser_Parse(t *testing.T) {
t.Errorf("[%v] Invalid token passed validation", data.name)
}

if (err == nil && !token.Valid) || (err != nil && token.Valid) {
// Since the returned token is nil in the ErrTokenMalformed, we
// cannot make the comparison here
if !errors.Is(err, jwt.ErrTokenMalformed) &&
((err == nil && !token.Valid) || (err != nil && token.Valid)) {
t.Errorf("[%v] Inconsistent behavior between returned error and token.Valid", data.name)
}

Expand Down