...

Source file src/google.golang.org/api/transport/http/dial_test.go

Documentation: google.golang.org/api/transport/http

     1  // Copyright 2023 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 http
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  
    13  	"golang.org/x/oauth2"
    14  	"golang.org/x/oauth2/google"
    15  	"google.golang.org/api/option"
    16  )
    17  
    18  func TestNewClient(t *testing.T) {
    19  	client, endpoint, err := NewClient(context.Background())
    20  
    21  	if err != nil {
    22  		t.Fatalf("unable to create client: %v", err)
    23  	}
    24  	if client == nil {
    25  		t.Fatalf("client is nil")
    26  	}
    27  	if endpoint != "" {
    28  		t.Errorf("got: %s, want: ''", endpoint)
    29  	}
    30  	if got, want := fmt.Sprintf("%T", client.Transport), "*httptransport.authTransport"; got != want {
    31  		t.Fatalf("got %s, want: %s", got, want)
    32  	}
    33  }
    34  
    35  func TestNewClient_MismatchedUniverseChecks(t *testing.T) {
    36  	t.Setenv("GOOGLE_API_GO_EXPERIMENTAL_DISABLE_NEW_AUTH_LIB", "true")
    37  	rootTokenScope := "https://www.googleapis.com/auth/cloud-platform"
    38  	otherUniverse := "example.com"
    39  	defaultUniverse := "googleapis.com"
    40  	fakeCreds := `
    41  	{"type": "service_account",
    42       "project_id": "some-project",
    43       "universe_domain": "UNIVERSE"}`
    44  
    45  	// utility function to make a fake credential quickly
    46  	makeFakeCredF := func(universe string) option.ClientOption {
    47  		data := []byte(strings.ReplaceAll(fakeCreds, "UNIVERSE", universe))
    48  		creds, _ := google.CredentialsFromJSON(context.Background(), data, rootTokenScope)
    49  		return option.WithCredentials(creds)
    50  	}
    51  
    52  	testCases := []struct {
    53  		description string
    54  		opts        []option.ClientOption
    55  		wantErr     bool
    56  	}{
    57  		{
    58  			description: "default creds and no universe",
    59  			opts: []option.ClientOption{
    60  				option.WithCredentials(&google.Credentials{}),
    61  			},
    62  			wantErr: false,
    63  		},
    64  		{
    65  			description: "default creds and default universe",
    66  			opts: []option.ClientOption{
    67  				option.WithCredentials(&google.Credentials{}),
    68  				option.WithUniverseDomain(defaultUniverse),
    69  			},
    70  			wantErr: false,
    71  		},
    72  		{
    73  			description: "default creds and mismatched universe",
    74  			opts: []option.ClientOption{
    75  				option.WithCredentials(&google.Credentials{}),
    76  				option.WithUniverseDomain(otherUniverse),
    77  			},
    78  			wantErr: true,
    79  		},
    80  		{
    81  			description: "foreign universe creds and default universe",
    82  			opts: []option.ClientOption{
    83  				makeFakeCredF(otherUniverse),
    84  				option.WithUniverseDomain(defaultUniverse),
    85  			},
    86  			wantErr: true,
    87  		},
    88  		{
    89  			description: "foreign universe creds and foreign universe",
    90  			opts: []option.ClientOption{
    91  				makeFakeCredF(otherUniverse),
    92  				option.WithUniverseDomain(otherUniverse),
    93  			},
    94  			wantErr: false,
    95  		},
    96  		{
    97  			description: "tokensource + mismatched universe",
    98  			opts: []option.ClientOption{
    99  				option.WithTokenSource(oauth2.StaticTokenSource(&oauth2.Token{})),
   100  				option.WithUniverseDomain(otherUniverse),
   101  			},
   102  			wantErr: false,
   103  		},
   104  	}
   105  
   106  	for _, tc := range testCases {
   107  		opts := []option.ClientOption{
   108  			option.WithScopes(rootTokenScope),
   109  		}
   110  		opts = append(opts, tc.opts...)
   111  		_, _, gotErr := NewClient(context.Background(), opts...)
   112  		if tc.wantErr && gotErr == nil {
   113  			t.Errorf("%q: wanted error, got none", tc.description)
   114  		}
   115  		if !tc.wantErr && gotErr != nil {
   116  			t.Errorf("%q: wanted success, got err: %v", tc.description, gotErr)
   117  		}
   118  	}
   119  }
   120  

View as plain text