github.com/yorinasub17/go-cloud@v0.27.40/gcp/gcp_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gcp_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"gocloud.dev/gcp"
    22  	"gocloud.dev/internal/testing/setup"
    23  )
    24  
    25  func TestNewHTTPClient(t *testing.T) {
    26  	transport := gcp.DefaultTransport()
    27  	_, err := gcp.NewHTTPClient(transport, nil)
    28  	if err == nil {
    29  		t.Error("got nil want error")
    30  	}
    31  	creds, err := setup.FakeGCPCredentials(context.Background())
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	_, err = gcp.NewHTTPClient(transport, gcp.CredentialsTokenSource(creds))
    36  	if err != nil {
    37  		t.Error(err)
    38  	}
    39  }
    40  
    41  func TestCredentialsTokenSource(t *testing.T) {
    42  	ts := gcp.CredentialsTokenSource(nil)
    43  	if ts != nil {
    44  		t.Error("got non-nil TokenSource from nil creds, want nil")
    45  	}
    46  	creds, err := setup.FakeGCPCredentials(context.Background())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	ts = gcp.CredentialsTokenSource(creds)
    51  	if ts == nil {
    52  		t.Error("got nil TokenSource from creds, want non-nil")
    53  	}
    54  }
    55  
    56  func TestDefaultProjectID(t *testing.T) {
    57  	_, err := gcp.DefaultProjectID(nil)
    58  	if err == nil {
    59  		t.Error("got nil error from nil creds, want error")
    60  	}
    61  	creds, err := setup.FakeGCPCredentials(context.Background())
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	_, err = gcp.DefaultProjectID(creds)
    66  	if err != nil {
    67  		t.Error(err)
    68  	}
    69  }