-
Notifications
You must be signed in to change notification settings - Fork 390
Added support for type parameters in the ParseXXX
functions
#271
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
Changes from 5 commits
c8567b4
e11c17d
ca0f5b0
a73d8a1
a9fd512
003fb0f
2d08912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/golang-jwt/jwt/v5 | ||
|
||
go 1.16 | ||
go 1.18 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,13 +56,13 @@ func TestVerifyAud(t *testing.T) { | |
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
var opts []ParserOption | ||
var opts []ParserOption[MapClaims] | ||
|
||
if test.Required { | ||
opts = append(opts, WithAudience(test.Comparison)) | ||
opts = append(opts, WithAudience[MapClaims](test.Comparison)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
validator := newValidator(opts...) | ||
validator := newValidator[MapClaims](opts...) | ||
got := validator.Validate(test.MapClaims) | ||
|
||
if (got == nil) != test.Expected { | ||
|
@@ -77,7 +77,7 @@ func TestMapclaimsVerifyIssuedAtInvalidTypeString(t *testing.T) { | |
"iat": "foo", | ||
} | ||
want := false | ||
got := newValidator(WithIssuedAt()).Validate(mapClaims) | ||
got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
@@ -88,7 +88,7 @@ func TestMapclaimsVerifyNotBeforeInvalidTypeString(t *testing.T) { | |
"nbf": "foo", | ||
} | ||
want := false | ||
got := newValidator().Validate(mapClaims) | ||
got := newValidator[MapClaims]().Validate(mapClaims) | ||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
@@ -99,7 +99,7 @@ func TestMapclaimsVerifyExpiresAtInvalidTypeString(t *testing.T) { | |
"exp": "foo", | ||
} | ||
want := false | ||
got := newValidator().Validate(mapClaims) | ||
got := newValidator[MapClaims]().Validate(mapClaims) | ||
|
||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
|
@@ -112,25 +112,78 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) { | |
"exp": float64(exp.Unix()), | ||
} | ||
want := false | ||
got := newValidator(WithTimeFunc(func() time.Time { | ||
got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return exp | ||
})).Validate(mapClaims) | ||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return exp.Add(1 * time.Second) | ||
})).Validate(mapClaims) | ||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
||
want = true | ||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return exp.Add(-1 * time.Second) | ||
})).Validate(mapClaims) | ||
if want != (got == nil) { | ||
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) | ||
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) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: ParserOption[MapClaims] (ParserOption is not a generic type)