Skip to content

Commit fa7a12b

Browse files
authored
Adding more coverage (#268)
1 parent 8144967 commit fa7a12b

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

map_claims_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,56 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
134134
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
135135
}
136136
}
137+
138+
func TestMapClaims_ParseString(t *testing.T) {
139+
type args struct {
140+
key string
141+
}
142+
tests := []struct {
143+
name string
144+
m MapClaims
145+
args args
146+
want string
147+
wantErr bool
148+
}{
149+
{
150+
name: "missing key",
151+
m: MapClaims{},
152+
args: args{
153+
key: "mykey",
154+
},
155+
want: "",
156+
wantErr: false,
157+
},
158+
{
159+
name: "wrong key type",
160+
m: MapClaims{"mykey": 4},
161+
args: args{
162+
key: "mykey",
163+
},
164+
want: "",
165+
wantErr: true,
166+
},
167+
{
168+
name: "correct key type",
169+
m: MapClaims{"mykey": "mystring"},
170+
args: args{
171+
key: "mykey",
172+
},
173+
want: "mystring",
174+
wantErr: false,
175+
},
176+
}
177+
for _, tt := range tests {
178+
t.Run(tt.name, func(t *testing.T) {
179+
got, err := tt.m.ParseString(tt.args.key)
180+
if (err != nil) != tt.wantErr {
181+
t.Errorf("MapClaims.ParseString() error = %v, wantErr %v", err, tt.wantErr)
182+
return
183+
}
184+
if got != tt.want {
185+
t.Errorf("MapClaims.ParseString() = %v, want %v", got, tt.want)
186+
}
187+
})
188+
}
189+
}

parser_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ var jwtTestData = []struct {
5656
parser *jwt.Parser
5757
signingMethod jwt.SigningMethod // The method to sign the JWT token for test purpose
5858
}{
59+
{
60+
"invalid JWT",
61+
"thisisnotreallyajwt",
62+
defaultKeyFunc,
63+
nil,
64+
false,
65+
jwt.ValidationErrorMalformed,
66+
[]error{jwt.ErrTokenMalformed},
67+
nil,
68+
jwt.SigningMethodRS256,
69+
},
70+
{
71+
"bearer in JWT",
72+
"bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
73+
defaultKeyFunc,
74+
nil,
75+
false,
76+
jwt.ValidationErrorMalformed,
77+
[]error{jwt.ErrTokenMalformed},
78+
nil,
79+
jwt.SigningMethodRS256,
80+
},
5981
{
6082
"basic",
6183
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
@@ -371,10 +393,12 @@ func TestParser_Parse(t *testing.T) {
371393
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)
372394
case *jwt.RegisteredClaims:
373395
token, err = parser.ParseWithClaims(data.tokenString, &jwt.RegisteredClaims{}, data.keyfunc)
396+
case nil:
397+
token, err = parser.ParseWithClaims(data.tokenString, nil, data.keyfunc)
374398
}
375399

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

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

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

0 commit comments

Comments
 (0)