@@ -7,12 +7,12 @@ package http
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "strings"
10
11
"testing"
11
12
12
13
"go.opencensus.io/plugin/ochttp"
13
14
"golang.org/x/oauth2"
14
15
"golang.org/x/oauth2/google"
15
- "google.golang.org/api/internal"
16
16
"google.golang.org/api/option"
17
17
)
18
18
@@ -41,16 +41,88 @@ func TestNewClient(t *testing.T) {
41
41
}
42
42
}
43
43
44
- func TestNewClient_MismatchedUniverseDomainCreds (t * testing.T ) {
44
+ func TestNewClient_MismatchedUniverseChecks (t * testing.T ) {
45
+
45
46
rootTokenScope := "https://www.googleapis.com/auth/cloud-platform"
46
- universeDomain := "example.com"
47
- universeDomainDefault := "googleapis.com"
48
- creds := & google.Credentials {} // universeDomainDefault
49
- wantErr := internal .ErrUniverseNotMatch (universeDomain , universeDomainDefault )
50
- _ , _ , err := NewClient (context .Background (), option .WithUniverseDomain (universeDomain ),
51
- option .WithCredentials (creds ), option .WithScopes (rootTokenScope ))
52
-
53
- if err .Error () != wantErr .Error () {
54
- t .Fatalf ("got: %v, want: %v" , err , wantErr )
47
+ otherUniverse := "example.com"
48
+ defaultUniverse := "googleapis.com"
49
+ fakeCreds := `
50
+ {"type": "service_account",
51
+ "project_id": "some-project",
52
+ "universe_domain": "UNIVERSE"}`
53
+
54
+ // utility function to make a fake credential quickly
55
+ makeFakeCredF := func (universe string ) option.ClientOption {
56
+ data := []byte (strings .ReplaceAll (fakeCreds , "UNIVERSE" , universe ))
57
+ creds , _ := google .CredentialsFromJSON (context .Background (), data , rootTokenScope )
58
+ return option .WithCredentials (creds )
59
+ }
60
+
61
+ testCases := []struct {
62
+ description string
63
+ opts []option.ClientOption
64
+ wantErr bool
65
+ }{
66
+ {
67
+ description : "default creds and no universe" ,
68
+ opts : []option.ClientOption {
69
+ option .WithCredentials (& google.Credentials {}),
70
+ },
71
+ wantErr : false ,
72
+ },
73
+ {
74
+ description : "default creds and default universe" ,
75
+ opts : []option.ClientOption {
76
+ option .WithCredentials (& google.Credentials {}),
77
+ option .WithUniverseDomain (defaultUniverse ),
78
+ },
79
+ wantErr : false ,
80
+ },
81
+ {
82
+ description : "default creds and mismatched universe" ,
83
+ opts : []option.ClientOption {
84
+ option .WithCredentials (& google.Credentials {}),
85
+ option .WithUniverseDomain (otherUniverse ),
86
+ },
87
+ wantErr : true ,
88
+ },
89
+ {
90
+ description : "foreign universe creds and default universe" ,
91
+ opts : []option.ClientOption {
92
+ makeFakeCredF (otherUniverse ),
93
+ option .WithUniverseDomain (defaultUniverse ),
94
+ },
95
+ wantErr : true ,
96
+ },
97
+ {
98
+ description : "foreign universe creds and foreign universe" ,
99
+ opts : []option.ClientOption {
100
+ makeFakeCredF (otherUniverse ),
101
+ option .WithUniverseDomain (otherUniverse ),
102
+ },
103
+ wantErr : false ,
104
+ },
105
+ {
106
+ description : "tokensource + mismatched universe" ,
107
+ opts : []option.ClientOption {
108
+ option .WithTokenSource (oauth2 .StaticTokenSource (& oauth2.Token {})),
109
+ option .WithUniverseDomain (otherUniverse ),
110
+ },
111
+ wantErr : false ,
112
+ },
113
+ }
114
+
115
+ for _ , tc := range testCases {
116
+ opts := []option.ClientOption {
117
+ option .WithScopes (rootTokenScope ),
118
+ }
119
+ opts = append (opts , tc .opts ... )
120
+ _ , _ , gotErr := NewClient (context .Background (), opts ... )
121
+ if tc .wantErr && gotErr == nil {
122
+ t .Errorf ("%q: wanted error, got none" , tc .description )
123
+ }
124
+ if ! tc .wantErr && gotErr != nil {
125
+ t .Errorf ("%q: wanted success, got err: %v" , tc .description , gotErr )
126
+ }
55
127
}
56
128
}
0 commit comments