...

Source file src/golang.org/x/oauth2/google/error_test.go

Documentation: golang.org/x/oauth2/google

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package google
     6  
     7  import (
     8  	"net/http"
     9  	"testing"
    10  
    11  	"golang.org/x/oauth2"
    12  )
    13  
    14  func TestAuthenticationError_Temporary(t *testing.T) {
    15  	tests := []struct {
    16  		name string
    17  		code int
    18  		want bool
    19  	}{
    20  		{
    21  			name: "temporary with 500",
    22  			code: 500,
    23  			want: true,
    24  		},
    25  		{
    26  			name: "temporary with 503",
    27  			code: 503,
    28  			want: true,
    29  		},
    30  		{
    31  			name: "temporary with 408",
    32  			code: 408,
    33  			want: true,
    34  		},
    35  		{
    36  			name: "temporary with 429",
    37  			code: 429,
    38  			want: true,
    39  		},
    40  		{
    41  			name: "temporary with 418",
    42  			code: 418,
    43  			want: false,
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			ae := &AuthenticationError{
    49  				err: &oauth2.RetrieveError{
    50  					Response: &http.Response{
    51  						StatusCode: tt.code,
    52  					},
    53  				},
    54  			}
    55  			if got := ae.Temporary(); got != tt.want {
    56  				t.Errorf("Temporary() = %v; want %v", got, tt.want)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestErrWrappingTokenSource_Token(t *testing.T) {
    63  	tok := oauth2.Token{AccessToken: "MyAccessToken"}
    64  	ts := errWrappingTokenSource{
    65  		src: oauth2.StaticTokenSource(&tok),
    66  	}
    67  	got, err := ts.Token()
    68  	if *got != tok {
    69  		t.Errorf("Token() = %v; want %v", got, tok)
    70  	}
    71  	if err != nil {
    72  		t.Error(err)
    73  	}
    74  }
    75  
    76  type errTokenSource struct {
    77  	err error
    78  }
    79  
    80  func (s *errTokenSource) Token() (*oauth2.Token, error) {
    81  	return nil, s.err
    82  }
    83  
    84  func TestErrWrappingTokenSource_TokenError(t *testing.T) {
    85  	re := &oauth2.RetrieveError{
    86  		Response: &http.Response{
    87  			StatusCode: 500,
    88  		},
    89  	}
    90  	ts := errWrappingTokenSource{
    91  		src: &errTokenSource{
    92  			err: re,
    93  		},
    94  	}
    95  	_, err := ts.Token()
    96  	if err == nil {
    97  		t.Fatalf("errWrappingTokenSource.Token() err = nil, want *AuthenticationError")
    98  	}
    99  	ae, ok := err.(*AuthenticationError)
   100  	if !ok {
   101  		t.Fatalf("errWrappingTokenSource.Token() err = %T, want *AuthenticationError", err)
   102  	}
   103  	wrappedErr := ae.Unwrap()
   104  	if wrappedErr == nil {
   105  		t.Fatalf("AuthenticationError.Unwrap() err = nil, want *oauth2.RetrieveError")
   106  	}
   107  	_, ok = wrappedErr.(*oauth2.RetrieveError)
   108  	if !ok {
   109  		t.Errorf("AuthenticationError.Unwrap() err = %T, want *oauth2.RetrieveError", err)
   110  	}
   111  }
   112  

View as plain text