@@ -6,6 +6,9 @@ package google
6
6
7
7
import (
8
8
"context"
9
+ "net/http"
10
+ "net/http/httptest"
11
+ "strings"
9
12
"testing"
10
13
)
11
14
@@ -74,6 +77,9 @@ func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
74
77
if want := "googleapis.com" ; creds .UniverseDomain () != want {
75
78
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
76
79
}
80
+ if want := "googleapis.com" ; creds .UniverseDomain () != want {
81
+ t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
82
+ }
77
83
}
78
84
79
85
func TestCredentialsFromJSONWithParams_SA_Params_UniverseDomain (t * testing.T ) {
@@ -94,6 +100,9 @@ func TestCredentialsFromJSONWithParams_SA_Params_UniverseDomain(t *testing.T) {
94
100
if creds .UniverseDomain () != universeDomain2 {
95
101
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), universeDomain2 )
96
102
}
103
+ if creds .UniverseDomain () != universeDomain2 {
104
+ t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), universeDomain2 )
105
+ }
97
106
}
98
107
99
108
func TestCredentialsFromJSONWithParams_SA_UniverseDomain (t * testing.T ) {
@@ -113,6 +122,13 @@ func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
113
122
if creds .UniverseDomain () != universeDomain {
114
123
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), universeDomain )
115
124
}
125
+ got , err := creds .GetUniverseDomain ()
126
+ if err != nil {
127
+ t .Fatal (err )
128
+ }
129
+ if got != universeDomain {
130
+ t .Fatalf ("got %q, want %q" , got , universeDomain )
131
+ }
116
132
}
117
133
118
134
func TestCredentialsFromJSONWithParams_SA_UniverseDomain_Params_UniverseDomain (t * testing.T ) {
@@ -133,6 +149,13 @@ func TestCredentialsFromJSONWithParams_SA_UniverseDomain_Params_UniverseDomain(t
133
149
if creds .UniverseDomain () != universeDomain2 {
134
150
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), universeDomain2 )
135
151
}
152
+ got , err := creds .GetUniverseDomain ()
153
+ if err != nil {
154
+ t .Fatal (err )
155
+ }
156
+ if got != universeDomain2 {
157
+ t .Fatalf ("got %q, want %q" , got , universeDomain2 )
158
+ }
136
159
}
137
160
138
161
func TestCredentialsFromJSONWithParams_User (t * testing.T ) {
@@ -149,6 +172,13 @@ func TestCredentialsFromJSONWithParams_User(t *testing.T) {
149
172
if want := "googleapis.com" ; creds .UniverseDomain () != want {
150
173
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
151
174
}
175
+ got , err := creds .GetUniverseDomain ()
176
+ if err != nil {
177
+ t .Fatal (err )
178
+ }
179
+ if want := "googleapis.com" ; got != want {
180
+ t .Fatalf ("got %q, want %q" , got , want )
181
+ }
152
182
}
153
183
154
184
func TestCredentialsFromJSONWithParams_User_Params_UniverseDomain (t * testing.T ) {
@@ -166,6 +196,13 @@ func TestCredentialsFromJSONWithParams_User_Params_UniverseDomain(t *testing.T)
166
196
if want := "googleapis.com" ; creds .UniverseDomain () != want {
167
197
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
168
198
}
199
+ got , err := creds .GetUniverseDomain ()
200
+ if err != nil {
201
+ t .Fatal (err )
202
+ }
203
+ if want := "googleapis.com" ; got != want {
204
+ t .Fatalf ("got %q, want %q" , got , want )
205
+ }
169
206
}
170
207
171
208
func TestCredentialsFromJSONWithParams_User_UniverseDomain (t * testing.T ) {
@@ -182,6 +219,13 @@ func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
182
219
if want := "googleapis.com" ; creds .UniverseDomain () != want {
183
220
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
184
221
}
222
+ got , err := creds .GetUniverseDomain ()
223
+ if err != nil {
224
+ t .Fatal (err )
225
+ }
226
+ if want := "googleapis.com" ; got != want {
227
+ t .Fatalf ("got %q, want %q" , got , want )
228
+ }
185
229
}
186
230
187
231
func TestCredentialsFromJSONWithParams_User_UniverseDomain_Params_UniverseDomain (t * testing.T ) {
@@ -199,4 +243,55 @@ func TestCredentialsFromJSONWithParams_User_UniverseDomain_Params_UniverseDomain
199
243
if want := "googleapis.com" ; creds .UniverseDomain () != want {
200
244
t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
201
245
}
246
+ got , err := creds .GetUniverseDomain ()
247
+ if err != nil {
248
+ t .Fatal (err )
249
+ }
250
+ if want := "googleapis.com" ; got != want {
251
+ t .Fatalf ("got %q, want %q" , got , want )
252
+ }
253
+ }
254
+
255
+ func TestComputeUniverseDomain (t * testing.T ) {
256
+ universeDomainPath := "/computeMetadata/v1/universe/universe_domain"
257
+ universeDomainResponseBody := "example.com"
258
+ s := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
259
+ if r .URL .Path != universeDomainPath {
260
+ t .Errorf ("got %s, want %s" , r .URL .Path , universeDomainPath )
261
+ }
262
+ w .Write ([]byte (universeDomainResponseBody ))
263
+ }))
264
+ defer s .Close ()
265
+ t .Setenv ("GCE_METADATA_HOST" , strings .TrimPrefix (s .URL , "http://" ))
266
+
267
+ scope := "https://www.googleapis.com/auth/cloud-platform"
268
+ params := CredentialsParams {
269
+ Scopes : []string {scope },
270
+ }
271
+ // Copied from FindDefaultCredentialsWithParams, metadata.OnGCE() = true block
272
+ creds := & Credentials {
273
+ ProjectID : "fake_project" ,
274
+ TokenSource : computeTokenSource ("" , params .EarlyTokenRefresh , params .Scopes ... ),
275
+ universeDomain : params .UniverseDomain , // empty
276
+ }
277
+ c := make (chan bool )
278
+ go func () {
279
+ got , err := creds .GetUniverseDomain () // First conflicting access.
280
+ if err != nil {
281
+ t .Error (err )
282
+ }
283
+ if want := universeDomainResponseBody ; got != want {
284
+ t .Errorf ("got %q, want %q" , got , want )
285
+ }
286
+ c <- true
287
+ }()
288
+ got , err := creds .GetUniverseDomain () // Second conflicting access.
289
+ <- c
290
+ if err != nil {
291
+ t .Error (err )
292
+ }
293
+ if want := universeDomainResponseBody ; got != want {
294
+ t .Errorf ("got %q, want %q" , got , want )
295
+ }
296
+
202
297
}
0 commit comments