File: error_test.go

package info (click to toggle)
golang-golang-x-oauth2 0.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 748 kB
  • sloc: makefile: 15
file content (111 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package google

import (
	"net/http"
	"testing"

	"golang.org/x/oauth2"
)

func TestAuthenticationError_Temporary(t *testing.T) {
	tests := []struct {
		name string
		code int
		want bool
	}{
		{
			name: "temporary with 500",
			code: 500,
			want: true,
		},
		{
			name: "temporary with 503",
			code: 503,
			want: true,
		},
		{
			name: "temporary with 408",
			code: 408,
			want: true,
		},
		{
			name: "temporary with 429",
			code: 429,
			want: true,
		},
		{
			name: "temporary with 418",
			code: 418,
			want: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ae := &AuthenticationError{
				err: &oauth2.RetrieveError{
					Response: &http.Response{
						StatusCode: tt.code,
					},
				},
			}
			if got := ae.Temporary(); got != tt.want {
				t.Errorf("Temporary() = %v; want %v", got, tt.want)
			}
		})
	}
}

func TestErrWrappingTokenSource_Token(t *testing.T) {
	tok := oauth2.Token{AccessToken: "MyAccessToken"}
	ts := errWrappingTokenSource{
		src: oauth2.StaticTokenSource(&tok),
	}
	got, err := ts.Token()
	if *got != tok {
		t.Errorf("Token() = %v; want %v", got, tok)
	}
	if err != nil {
		t.Error(err)
	}
}

type errTokenSource struct {
	err error
}

func (s *errTokenSource) Token() (*oauth2.Token, error) {
	return nil, s.err
}

func TestErrWrappingTokenSource_TokenError(t *testing.T) {
	re := &oauth2.RetrieveError{
		Response: &http.Response{
			StatusCode: 500,
		},
	}
	ts := errWrappingTokenSource{
		src: &errTokenSource{
			err: re,
		},
	}
	_, err := ts.Token()
	if err == nil {
		t.Fatalf("errWrappingTokenSource.Token() err = nil, want *AuthenticationError")
	}
	ae, ok := err.(*AuthenticationError)
	if !ok {
		t.Fatalf("errWrappingTokenSource.Token() err = %T, want *AuthenticationError", err)
	}
	wrappedErr := ae.Unwrap()
	if wrappedErr == nil {
		t.Fatalf("AuthenticationError.Unwrap() err = nil, want *oauth2.RetrieveError")
	}
	_, ok = wrappedErr.(*oauth2.RetrieveError)
	if !ok {
		t.Errorf("AuthenticationError.Unwrap() err = %T, want *oauth2.RetrieveError", err)
	}
}