@@ -16,10 +16,12 @@ package datasource_test
1616
1717import (
1818 "bytes"
19+ "io"
1920 "net/http"
2021 "os"
2122 "path/filepath"
2223 "reflect"
24+ "sync"
2325 "testing"
2426
2527 "deps.dev/util/maven"
@@ -278,3 +280,90 @@ func TestMavenLocalRegistry(t *testing.T) {
278280 t .Errorf ("unexpected file content: got %s, want %s" , string (content ), string (resp ))
279281 }
280282}
283+
284+ type trackingTransport struct {
285+ mu sync.Mutex
286+ called bool
287+ }
288+
289+ func (t * trackingTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
290+ t .mu .Lock ()
291+ t .called = true
292+ t .mu .Unlock ()
293+ return & http.Response {
294+ StatusCode : http .StatusOK ,
295+ Body : io .NopCloser (bytes .NewReader ([]byte ("<project><groupId>g</groupId><artifactId>a</artifactId><version>v</version></project>" ))),
296+ }, nil
297+ }
298+
299+ func (t * trackingTransport ) wasCalled () bool {
300+ t .mu .Lock ()
301+ defer t .mu .Unlock ()
302+ return t .called
303+ }
304+
305+ // TestDisableGoogleAuthRespected tests that setting disableGoogleAuth = true in
306+ // NewMavenRegistryAPIClient prevents the Google client from being used for
307+ // Artifact Registry requests, falling back to the standard HTTP client.
308+ func TestDisableGoogleAuthRespected (t * testing.T ) {
309+ standardTransport := & trackingTransport {}
310+ googleTransport := & trackingTransport {}
311+
312+ standardClient := & http.Client {Transport : standardTransport }
313+ googleClient := & http.Client {Transport : googleTransport }
314+
315+ client , err := datasource .NewMavenRegistryAPIClient (
316+ t .Context (),
317+ datasource.MavenRegistry {URL : "artifactregistry://example.com" , ReleasesEnabled : true },
318+ "" , // localRegistry
319+ true , // disableGoogleAuth
320+ standardClient ,
321+ googleClient ,
322+ )
323+ if err != nil {
324+ t .Fatalf ("NewMavenRegistryAPIClient failed: %v" , err )
325+ }
326+
327+ _ , _ = client .GetProject (t .Context (), "g" , "a" , "v" )
328+
329+ if googleTransport .wasCalled () {
330+ t .Errorf ("Google client was called when disableGoogleAuth is true" )
331+ }
332+ if ! standardTransport .wasCalled () {
333+ t .Errorf ("Standard client was not called" )
334+ }
335+ }
336+
337+ // TestDisableGoogleAuthMethodRespected tests that dynamically calling
338+ // DisableGoogleAuth() post-construction prevents the Google client from being
339+ // used for Artifact Registry requests.
340+ func TestDisableGoogleAuthMethodRespected (t * testing.T ) {
341+ standardTransport := & trackingTransport {}
342+ googleTransport := & trackingTransport {}
343+
344+ standardClient := & http.Client {Transport : standardTransport }
345+ googleClient := & http.Client {Transport : googleTransport }
346+
347+ client , err := datasource .NewMavenRegistryAPIClient (
348+ t .Context (),
349+ datasource.MavenRegistry {URL : "artifactregistry://example.com" , ReleasesEnabled : true },
350+ "" , // localRegistry
351+ false , // disableGoogleAuth
352+ standardClient ,
353+ googleClient ,
354+ )
355+ if err != nil {
356+ t .Fatalf ("NewMavenRegistryAPIClient failed: %v" , err )
357+ }
358+
359+ client .DisableGoogleAuth ()
360+
361+ _ , _ = client .GetProject (t .Context (), "g" , "a" , "v" )
362+
363+ if googleTransport .wasCalled () {
364+ t .Errorf ("Google client was called after DisableGoogleAuth()" )
365+ }
366+ if ! standardTransport .wasCalled () {
367+ t .Errorf ("Standard client was not called" )
368+ }
369+ }
0 commit comments