...

Source file src/google.golang.org/api/option/internaloption/internaloption_test.go

Documentation: google.golang.org/api/option/internaloption

     1  // Copyright 2021 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package internaloption
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/google/go-cmp/cmp/cmpopts"
    12  	"golang.org/x/oauth2"
    13  	"golang.org/x/oauth2/google"
    14  	"google.golang.org/api/internal"
    15  	"google.golang.org/api/option"
    16  	"google.golang.org/grpc"
    17  )
    18  
    19  func TestWithCredentials(t *testing.T) {
    20  	want := "fakeToken"
    21  	fakeCreds := &google.Credentials{
    22  		TokenSource: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: want}),
    23  	}
    24  	opt := WithCredentials(fakeCreds)
    25  	ds := &internal.DialSettings{}
    26  	opt.Apply(ds)
    27  	if ds.InternalCredentials == nil || ds.InternalCredentials.TokenSource == nil {
    28  		t.Errorf("ds.InternalCredentials should be initialized")
    29  	}
    30  	if tok, err := ds.InternalCredentials.TokenSource.Token(); err != nil || tok.AccessToken != "fakeToken" {
    31  		t.Errorf("ts.Token() = %q, want %q", tok.AccessToken, "")
    32  	}
    33  }
    34  
    35  func TestDefaultApply(t *testing.T) {
    36  	opts := []option.ClientOption{
    37  		WithDefaultEndpoint("https://example.com:443"),
    38  		WithDefaultEndpointTemplate("https://foo.UNIVERSE_DOMAIN/"),
    39  		WithDefaultMTLSEndpoint("http://mtls.example.com:445"),
    40  		WithDefaultScopes("a"),
    41  		WithDefaultUniverseDomain("foo.com"),
    42  		WithDefaultAudience("audience"),
    43  	}
    44  	var got internal.DialSettings
    45  	for _, opt := range opts {
    46  		opt.Apply(&got)
    47  	}
    48  	want := internal.DialSettings{
    49  		DefaultScopes:           []string{"a"},
    50  		DefaultEndpoint:         "https://example.com:443",
    51  		DefaultEndpointTemplate: "https://foo.UNIVERSE_DOMAIN/",
    52  		DefaultUniverseDomain:   "foo.com",
    53  		DefaultAudience:         "audience",
    54  		DefaultMTLSEndpoint:     "http://mtls.example.com:445",
    55  	}
    56  	ignore := []cmp.Option{
    57  		cmpopts.IgnoreUnexported(grpc.ClientConn{}),
    58  		cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain"),
    59  	}
    60  	if !cmp.Equal(got, want, ignore...) {
    61  		t.Errorf(cmp.Diff(got, want, ignore...))
    62  	}
    63  }
    64  

View as plain text