(t *testing.T)
| 586 | } |
| 587 | |
| 588 | func TestParser_ParseUnverified(t *testing.T) { |
| 589 | // Iterate over test data set and run tests |
| 590 | for _, data := range jwtTestData { |
| 591 | // Skip test data, that intentionally contains malformed tokens, as they would lead to an error |
| 592 | if len(data.err) == 1 && errors.Is(data.err[0], jwt.ErrTokenMalformed) { |
| 593 | continue |
| 594 | } |
| 595 | |
| 596 | t.Run(data.name, func(t *testing.T) { |
| 597 | // If the token string is blank, use helper function to generate string |
| 598 | if data.tokenString == "" { |
| 599 | data.tokenString = signToken(data.claims, data.signingMethod) |
| 600 | } |
| 601 | |
| 602 | // Parse the token |
| 603 | var token *jwt.Token |
| 604 | var err error |
| 605 | var parser = data.parser |
| 606 | if parser == nil { |
| 607 | parser = new(jwt.Parser) |
| 608 | } |
| 609 | // Figure out correct claims type |
| 610 | switch data.claims.(type) { |
| 611 | case jwt.MapClaims: |
| 612 | token, _, err = parser.ParseUnverified(data.tokenString, jwt.MapClaims{}) |
| 613 | case *jwt.RegisteredClaims: |
| 614 | token, _, err = parser.ParseUnverified(data.tokenString, &jwt.RegisteredClaims{}) |
| 615 | case *customClaimsWithDifferentTypes: |
| 616 | token, _, err = parser.ParseUnverified(data.tokenString, &customClaimsWithDifferentTypes{}) |
| 617 | } |
| 618 | |
| 619 | if err != nil { |
| 620 | t.Errorf("[%v] Invalid token", data.name) |
| 621 | } |
| 622 | |
| 623 | // Verify result matches expectation |
| 624 | if !reflect.DeepEqual(data.claims, token.Claims) { |
| 625 | t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims) |
| 626 | } |
| 627 | |
| 628 | if data.valid && err != nil { |
| 629 | t.Errorf("[%v] Error while verifying token: %T:%v", data.name, err, err) |
| 630 | } |
| 631 | if token.Valid { |
| 632 | // The 'Valid' field should not be set to true when invoking ParseUnverified() |
| 633 | t.Errorf("[%v] Token.Valid field mismatch. Expecting false, got %v", data.name, token.Valid) |
| 634 | } |
| 635 | if len(token.Signature) == 0 { |
| 636 | // The 'Signature' should always be populated. |
| 637 | t.Errorf("[%v] Token.Signature field mismatch. Expecting non-nil, got %v", data.name, token.Signature) |
| 638 | } |
| 639 | }) |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | var setPaddingTestData = []struct { |
| 644 | name string |
nothing calls this directly
no test coverage detected