MCPcopy
hub / github.com/grpc/grpc-go / TestTokenInfoFromResponse

Method TestTokenInfoFromResponse

credentials/sts/sts_test.go:706–766  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

704}
705
706func (s) TestTokenInfoFromResponse(t *testing.T) {
707 noAccessToken, _ := json.Marshal(responseParameters{
708 IssuedTokenType: "urn:ietf:params:oauth:token-type:access_token",
709 TokenType: "Bearer",
710 ExpiresIn: 3600,
711 })
712 goodResponse, _ := json.Marshal(responseParameters{
713 IssuedTokenType: requestedTokenType,
714 AccessToken: accessTokenContents,
715 TokenType: "Bearer",
716 ExpiresIn: 3600,
717 })
718
719 tests := []struct {
720 name string
721 respBody []byte
722 wantTokenInfo *tokenInfo
723 wantErr bool
724 }{
725 {
726 name: "bad JSON",
727 respBody: []byte("not JSON"),
728 wantErr: true,
729 },
730 {
731 name: "empty response",
732 respBody: []byte(""),
733 wantErr: true,
734 },
735 {
736 name: "non-empty response with no access token",
737 respBody: noAccessToken,
738 wantErr: true,
739 },
740 {
741 name: "good response",
742 respBody: goodResponse,
743 wantTokenInfo: &tokenInfo{
744 tokenType: "Bearer",
745 token: accessTokenContents,
746 },
747 },
748 }
749
750 for _, test := range tests {
751 t.Run(test.name, func(t *testing.T) {
752 gotTokenInfo, err := tokenInfoFromResponse(test.respBody)
753 if (err != nil) != test.wantErr {
754 t.Fatalf("tokenInfoFromResponse(%+v) = %v, wantErr: %v", test.respBody, err, test.wantErr)
755 }
756 if test.wantErr {
757 return
758 }
759 // Can't do a cmp.Equal on the whole struct since the expiryField
760 // is populated based on time.Now().
761 if gotTokenInfo.tokenType != test.wantTokenInfo.tokenType || gotTokenInfo.token != test.wantTokenInfo.token {
762 t.Errorf("tokenInfoFromResponse(%+v) = %+v, want: %+v", test.respBody, gotTokenInfo, test.wantTokenInfo)
763 }

Callers

nothing calls this directly

Calls 4

tokenInfoFromResponseFunction · 0.85
MarshalMethod · 0.65
FatalfMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected