1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package transport
16
17 import (
18 "net/http"
19 "reflect"
20 "testing"
21
22 "cloud.google.com/go/auth"
23 "cloud.google.com/go/auth/credentials"
24 )
25
26
27
28
29
30
31 func TestCloneDetectOptions_FieldTest(t *testing.T) {
32 const WantNumberOfFields = 12
33 o := credentials.DetectOptions{}
34 got := reflect.TypeOf(o).NumField()
35 if got != WantNumberOfFields {
36 t.Errorf("if this fails please read comment above the test: got %v, want %v", got, WantNumberOfFields)
37 }
38 }
39
40 func TestCloneDetectOptions(t *testing.T) {
41 oldDo := &credentials.DetectOptions{
42 Audience: "aud",
43 Subject: "sub",
44 EarlyTokenRefresh: 42,
45 TokenURL: "TokenURL",
46 STSAudience: "STSAudience",
47 CredentialsFile: "CredentialsFile",
48 UseSelfSignedJWT: true,
49 CredentialsJSON: []byte{1, 2, 3, 4, 5},
50 Scopes: []string{"a", "b"},
51 Client: &http.Client{},
52 AuthHandlerOptions: &auth.AuthorizationHandlerOptions{
53 Handler: func(authCodeURL string) (code string, state string, err error) {
54 return "", "", nil
55 },
56 State: "state",
57 PKCEOpts: &auth.PKCEOptions{
58 Challenge: "Challenge",
59 ChallengeMethod: "ChallengeMethod",
60 Verifier: "Verifier",
61 },
62 },
63 }
64 newDo := CloneDetectOptions(oldDo)
65
66
67 if got, want := newDo.Audience, oldDo.Audience; got != want {
68 t.Fatalf("got %v, want %v", got, want)
69 }
70 if got, want := newDo.Subject, oldDo.Subject; got != want {
71 t.Fatalf("got %v, want %v", got, want)
72 }
73 if got, want := newDo.EarlyTokenRefresh, oldDo.EarlyTokenRefresh; got != want {
74 t.Fatalf("got %v, want %v", got, want)
75 }
76 if got, want := newDo.TokenURL, oldDo.TokenURL; got != want {
77 t.Fatalf("got %v, want %v", got, want)
78 }
79 if got, want := newDo.STSAudience, oldDo.STSAudience; got != want {
80 t.Fatalf("got %v, want %v", got, want)
81 }
82 if got, want := newDo.CredentialsFile, oldDo.CredentialsFile; got != want {
83 t.Fatalf("got %v, want %v", got, want)
84 }
85 if got, want := newDo.UseSelfSignedJWT, oldDo.UseSelfSignedJWT; got != want {
86 t.Fatalf("got %v, want %v", got, want)
87 }
88
89
90 if got, want := len(newDo.CredentialsJSON), len(oldDo.CredentialsJSON); got != want {
91 t.Fatalf("got %v, want %v", got, want)
92 }
93 if got, want := newDo.CredentialsJSON, oldDo.CredentialsJSON; reflect.ValueOf(got).Pointer() == reflect.ValueOf(want).Pointer() {
94 t.Fatalf("CredentialsJSON should not reference the same slice")
95 }
96 if got, want := len(newDo.Scopes), len(oldDo.Scopes); got != want {
97 t.Fatalf("got %v, want %v", got, want)
98 }
99 if got, want := newDo.Scopes, oldDo.Scopes; reflect.ValueOf(got).Pointer() == reflect.ValueOf(want).Pointer() {
100 t.Fatalf("Scopes should not reference the same slice")
101 }
102
103
104 if got, want := newDo.Client, oldDo.Client; reflect.ValueOf(got).Pointer() != reflect.ValueOf(want).Pointer() {
105 t.Fatalf("Scopes should not reference the same slice")
106 }
107 if got, want := newDo.AuthHandlerOptions, oldDo.AuthHandlerOptions; reflect.ValueOf(got).Pointer() != reflect.ValueOf(want).Pointer() {
108 t.Fatalf("Scopes should not reference the same slice")
109 }
110 }
111
View as plain text