...

Source file src/google.golang.org/api/internal/settings_test.go

Documentation: google.golang.org/api/internal

     1  // Copyright 2017 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 internal supports the options and transport packages.
     6  package internal
     7  
     8  import (
     9  	"crypto/tls"
    10  	"net/http"
    11  	"testing"
    12  
    13  	"google.golang.org/api/internal/impersonate"
    14  	"google.golang.org/grpc"
    15  
    16  	"golang.org/x/oauth2"
    17  	"golang.org/x/oauth2/google"
    18  )
    19  
    20  func TestSettingsValidate(t *testing.T) {
    21  	dummyGetClientCertificate := func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { return nil, nil }
    22  
    23  	// Valid.
    24  	for _, ds := range []DialSettings{
    25  		{},
    26  		{APIKey: "x"},
    27  		{Scopes: []string{"s"}},
    28  		{CredentialsFile: "f"},
    29  		{TokenSource: dummyTS{}},
    30  		{CredentialsFile: "f", TokenSource: dummyTS{}}, // keep for backwards compatibility
    31  		{CredentialsJSON: []byte("json")},
    32  		{HTTPClient: &http.Client{}},
    33  		{GRPCConn: &grpc.ClientConn{}},
    34  		// Although NoAuth and Scopes are technically incompatible, too many
    35  		// cloud clients add WithScopes to user-provided options to make
    36  		// the check feasible.
    37  		{NoAuth: true, Scopes: []string{"s"}},
    38  		{ClientCertSource: dummyGetClientCertificate},
    39  		{ImpersonationConfig: &impersonate.Config{Scopes: []string{"x"}}},
    40  		{ImpersonationConfig: &impersonate.Config{}, Scopes: []string{"x"}},
    41  	} {
    42  		err := ds.Validate()
    43  		if err != nil {
    44  			t.Errorf("%+v: got %v, want nil", ds, err)
    45  		}
    46  	}
    47  
    48  	// Invalid.
    49  	for _, ds := range []DialSettings{
    50  		{NoAuth: true, APIKey: "x"},
    51  		{NoAuth: true, CredentialsFile: "f"},
    52  		{NoAuth: true, TokenSource: dummyTS{}},
    53  		{NoAuth: true, Credentials: &google.DefaultCredentials{}},
    54  		{Credentials: &google.DefaultCredentials{}, CredentialsFile: "f"},
    55  		{Credentials: &google.DefaultCredentials{}, TokenSource: dummyTS{}},
    56  		{Credentials: &google.DefaultCredentials{}, CredentialsJSON: []byte("json")},
    57  		{CredentialsFile: "f", CredentialsJSON: []byte("json")},
    58  		{CredentialsJSON: []byte("json"), TokenSource: dummyTS{}},
    59  		{HTTPClient: &http.Client{}, GRPCConn: &grpc.ClientConn{}},
    60  		{HTTPClient: &http.Client{}, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}},
    61  		{Audiences: []string{"foo"}, Scopes: []string{"foo"}},
    62  		{HTTPClient: &http.Client{}, QuotaProject: "foo"},
    63  		{HTTPClient: &http.Client{}, RequestReason: "foo"},
    64  		{HTTPClient: &http.Client{}, ClientCertSource: dummyGetClientCertificate},
    65  		{ClientCertSource: dummyGetClientCertificate, GRPCConn: &grpc.ClientConn{}},
    66  		{ClientCertSource: dummyGetClientCertificate, GRPCConnPool: struct{ ConnPool }{}},
    67  		{ClientCertSource: dummyGetClientCertificate, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}},
    68  		{ClientCertSource: dummyGetClientCertificate, GRPCConnPoolSize: 1},
    69  		{ImpersonationConfig: &impersonate.Config{}},
    70  	} {
    71  		err := ds.Validate()
    72  		if err == nil {
    73  			t.Errorf("%+v: got nil, want error", ds)
    74  		}
    75  	}
    76  
    77  }
    78  
    79  type dummyTS struct{}
    80  
    81  func (dummyTS) Token() (*oauth2.Token, error) { return nil, nil }
    82  
    83  func TestGetUniverseDomain(t *testing.T) {
    84  	testCases := []struct {
    85  		name                 string
    86  		ds                   *DialSettings
    87  		universeDomainEnvVar string
    88  		want                 string
    89  	}{
    90  		{
    91  			name: "none",
    92  			ds:   &DialSettings{},
    93  			want: "googleapis.com",
    94  		},
    95  		{
    96  			name: "settings",
    97  			ds: &DialSettings{
    98  				UniverseDomain: "settings-example.goog",
    99  			},
   100  			want: "settings-example.goog",
   101  		},
   102  		{
   103  			name:                 "env var",
   104  			ds:                   &DialSettings{},
   105  			universeDomainEnvVar: "env-example.goog",
   106  			want:                 "env-example.goog",
   107  		},
   108  		{
   109  			name: "both",
   110  			ds: &DialSettings{
   111  				UniverseDomain: "settings-example.goog",
   112  			},
   113  			universeDomainEnvVar: "env-example.goog",
   114  			want:                 "settings-example.goog",
   115  		},
   116  	}
   117  
   118  	for _, tc := range testCases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  			if tc.universeDomainEnvVar != "" {
   121  				t.Setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", tc.universeDomainEnvVar)
   122  			}
   123  
   124  			if got := tc.ds.GetUniverseDomain(); got != tc.want {
   125  				t.Errorf("got %s, want %s", got, tc.want)
   126  			}
   127  			if got, want := tc.ds.GetDefaultUniverseDomain(), "googleapis.com"; got != want {
   128  				t.Errorf("got %s, want %s", got, want)
   129  			}
   130  		})
   131  	}
   132  }
   133  

View as plain text