Repository navigation
Expand file tree
/
Copy pathoptions_test.go
More file actions
120 lines (100 loc) · 3.52 KB
/
Copy pathoptions_test.go
File metadata and controls
120 lines (100 loc) · 3.52 KB
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
112
113
114
115
116
117
118
119
120
package oauthctx
import (
"context"
"net/http"
"testing"
"golang.org/x/oauth2"
)
func TestOauth2ContextClient_ExtractsClient(t *testing.T) {
client := &http.Client{}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client)
got := Oauth2ContextClient(ctx)
if got != client {
t.Error("extracted client does not match injected client")
}
}
func TestOauth2ContextClient_NilContext(t *testing.T) {
if got := Oauth2ContextClient(nil); got != nil {
t.Errorf("expected nil for nil context, got %v", got)
}
}
func TestOauth2ContextClient_NoClientInContext(t *testing.T) {
if got := Oauth2ContextClient(context.Background()); got != nil {
t.Errorf("expected nil when no client is in context, got %v", got)
}
}
func TestTokenSourceConfig_WithOauth2HTTPClient_InjectsClient(t *testing.T) {
client := &http.Client{}
cfg := NewTokenSourceConfig(TokenSourceWithClient(client))
ctx := cfg.WithOauth2HTTPClient(context.Background())
if got := Oauth2ContextClient(ctx); got != client {
t.Error("client not recoverable from context after WithOauth2HTTPClient")
}
}
func TestTokenSourceConfig_WithOauth2HTTPClient_NilConfig_ReturnsCtx(t *testing.T) {
var cfg *TokenSourceConfig
ctx := cfg.WithOauth2HTTPClient(context.Background())
if ctx == nil {
t.Error("WithOauth2HTTPClient on nil config must return a non-nil context")
}
if got := Oauth2ContextClient(ctx); got != nil {
t.Error("nil config must not inject a client")
}
}
func TestTokenSourceConfig_GetOptionalClient_NilConfig(t *testing.T) {
var cfg *TokenSourceConfig
if got := cfg.GetOptionalClient(); got != nil {
t.Errorf("expected nil client for nil config, got %v", got)
}
}
func TestTokenSourceConfig_GetOptionalClient_ReturnsClient(t *testing.T) {
client := &http.Client{}
cfg := NewTokenSourceConfig(TokenSourceWithClient(client))
if got := cfg.GetOptionalClient(); got != client {
t.Error("GetOptionalClient did not return the configured client")
}
}
func TestNewRequestFlowConfig_NoOps_ReturnsNil(t *testing.T) {
if cfg := NewRequestFlowConfig(); cfg != nil {
t.Error("expected nil RequestFlowConfig when no ops are provided")
}
}
func TestRequestFlowWithClient_SetsBoth(t *testing.T) {
client := &http.Client{}
cfg := NewRequestFlowConfig(RequestFlowWithClient(client))
if ops := cfg.TokenSourceOps(); len(ops) == 0 {
t.Error("expected TokenSourceOps to be set")
}
if ops := cfg.ClientOps(); len(ops) == 0 {
t.Error("expected ClientOps to be set")
}
}
func TestRequestFlowWithTokenClient_OnlySetsTokenClient(t *testing.T) {
client := &http.Client{}
cfg := NewRequestFlowConfig(RequestFlowWithTokenClient(client))
if ops := cfg.TokenSourceOps(); len(ops) == 0 {
t.Error("expected TokenSourceOps to be set")
}
if ops := cfg.ClientOps(); len(ops) != 0 {
t.Error("ClientOps should be empty when only token client is configured")
}
}
func TestRequestFlowWithRequestClient_OnlySetsRequestClient(t *testing.T) {
client := &http.Client{}
cfg := NewRequestFlowConfig(RequestFlowWithRequestClient(client))
if ops := cfg.TokenSourceOps(); len(ops) != 0 {
t.Error("TokenSourceOps should be empty when only request client is configured")
}
if ops := cfg.ClientOps(); len(ops) == 0 {
t.Error("expected ClientOps to be set")
}
}
func TestRequestFlowConfig_NilConfig_ReturnsNilOps(t *testing.T) {
var cfg *RequestFlowConfig
if ops := cfg.TokenSourceOps(); ops != nil {
t.Errorf("expected nil TokenSourceOps for nil config, got %v", ops)
}
if ops := cfg.ClientOps(); ops != nil {
t.Errorf("expected nil ClientOps for nil config, got %v", ops)
}
}