@@ -75,14 +75,15 @@ func TestJWT(t *testing.T) {
7575 validAuth := DefaultJWTConfig .AuthScheme + " " + token
7676
7777 for _ , tc := range []struct {
78- expPanic bool
79- expErrCode int // 0 for Success
80- config JWTConfig
81- reqURL string // "/" if empty
82- hdrAuth string
83- hdrCookie string // test.Request doesn't provide SetCookie(); use name=val
84- formValues map [string ]string
85- info string
78+ expPanic bool
79+ expErrCode int // 0 for Success
80+ expEmptyCtx bool
81+ config JWTConfig
82+ reqURL string // "/" if empty
83+ hdrAuth string
84+ hdrCookie string // test.Request doesn't provide SetCookie(); use name=val
85+ formValues map [string ]string
86+ info string
8687 }{
8788 {
8889 expPanic : true ,
@@ -266,6 +267,17 @@ func TestJWT(t *testing.T) {
266267 config : JWTConfig {SigningKey : validKey },
267268 info : "Valid JWT with lower case AuthScheme" ,
268269 },
270+ {
271+ expEmptyCtx : true ,
272+ config : JWTConfig {SigningKey : validKey , CredentialsOptional : true },
273+ info : "Valid JWT" ,
274+ },
275+ {
276+ expEmptyCtx : false ,
277+ hdrAuth : validAuth ,
278+ config : JWTConfig {SigningKey : validKey , CredentialsOptional : true },
279+ info : "Valid JWT" ,
280+ },
269281 } {
270282 if tc .reqURL == "" {
271283 tc .reqURL = "/"
@@ -309,15 +321,19 @@ func TestJWT(t *testing.T) {
309321
310322 h := JWTWithConfig (tc .config )(handler )
311323 if assert .NoError (t , h (c ), tc .info ) {
312- user := c .Get ("user" ).(* jwt.Token )
313- switch claims := user .Claims .(type ) {
314- case jwt.MapClaims :
315- assert .Equal (t , claims ["name" ], "John Doe" , tc .info )
316- case * jwtCustomClaims :
317- assert .Equal (t , claims .Name , "John Doe" , tc .info )
318- assert .Equal (t , claims .Admin , true , tc .info )
319- default :
320- panic ("unexpected type of claims" )
324+ if tc .expEmptyCtx {
325+ assert .Nil (t , c .Get ("user" ), tc .info )
326+ } else {
327+ user := c .Get ("user" ).(* jwt.Token )
328+ switch claims := user .Claims .(type ) {
329+ case jwt.MapClaims :
330+ assert .Equal (t , claims ["name" ], "John Doe" , tc .info )
331+ case * jwtCustomClaims :
332+ assert .Equal (t , claims .Name , "John Doe" , tc .info )
333+ assert .Equal (t , claims .Admin , true , tc .info )
334+ default :
335+ panic ("unexpected type of claims" )
336+ }
321337 }
322338 }
323339 }
0 commit comments