...

Source file src/cloud.google.com/go/auth/credentials/internal/externalaccount/url_provider_test.go

Documentation: cloud.google.com/go/auth/credentials/internal/externalaccount

     1  // Copyright 2023 Google LLC
     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  //      http://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 externalaccount
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"cloud.google.com/go/auth/internal/credsfile"
    24  )
    25  
    26  func TestRetrieveURLSubjectToken_Text(t *testing.T) {
    27  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    28  		if want := http.MethodGet; r.Method != want {
    29  			t.Errorf("got %v, want %v", r.Method, want)
    30  		}
    31  		if got, want := r.Header.Get("Metadata"), "True"; got != want {
    32  			t.Errorf("got %v, want %v", got, want)
    33  		}
    34  		w.Write([]byte("testTokenValue"))
    35  	}))
    36  	defer ts.Close()
    37  
    38  	opts := cloneTestOpts()
    39  	opts.CredentialSource = &credsfile.CredentialSource{
    40  		URL:    ts.URL,
    41  		Format: &credsfile.Format{Type: fileTypeText},
    42  		Headers: map[string]string{
    43  			"Metadata": "True",
    44  		},
    45  	}
    46  
    47  	base, err := newSubjectTokenProvider(opts)
    48  	if err != nil {
    49  		t.Fatalf("newSubjectTokenProvider() = %v", err)
    50  	}
    51  
    52  	got, err := base.subjectToken(context.Background())
    53  	if err != nil {
    54  		t.Fatalf("base.subjectToken() = %v", err)
    55  	}
    56  	if want := "testTokenValue"; got != want {
    57  		t.Errorf("got %q, want %q", got, want)
    58  	}
    59  	if got, want := base.providerType(), urlProviderType; got != want {
    60  		t.Fatalf("got %q, want %q", got, want)
    61  	}
    62  }
    63  
    64  func TestRetrieveURLSubjectToken_Untyped(t *testing.T) {
    65  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    66  		if want := http.MethodGet; r.Method != want {
    67  			t.Errorf("got %v, want %v", r.Method, want)
    68  		}
    69  		w.Write([]byte("testTokenValue"))
    70  	}))
    71  	defer ts.Close()
    72  
    73  	opts := cloneTestOpts()
    74  	opts.CredentialSource = &credsfile.CredentialSource{
    75  		URL: ts.URL,
    76  	}
    77  
    78  	base, err := newSubjectTokenProvider(opts)
    79  	if err != nil {
    80  		t.Fatalf("newSubjectTokenProvider() failed %v", err)
    81  	}
    82  
    83  	got, err := base.subjectToken(context.Background())
    84  	if err != nil {
    85  		t.Fatalf("base.subjectToken() = %v", err)
    86  	}
    87  	if want := "testTokenValue"; got != want {
    88  		t.Errorf("got %v, want %v", got, want)
    89  	}
    90  }
    91  
    92  func TestRetrieveURLSubjectToken_JSON(t *testing.T) {
    93  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    94  		if got, want := r.Method, "GET"; got != want {
    95  			t.Errorf("got %v, want %v", r.Method, want)
    96  		}
    97  		w.Write([]byte(`{"SubjToken":"testTokenValue"}`))
    98  	}))
    99  	defer ts.Close()
   100  
   101  	opts := cloneTestOpts()
   102  	opts.CredentialSource = &credsfile.CredentialSource{
   103  		URL:    ts.URL,
   104  		Format: &credsfile.Format{Type: fileTypeJSON, SubjectTokenFieldName: "SubjToken"},
   105  	}
   106  
   107  	base, err := newSubjectTokenProvider(opts)
   108  	if err != nil {
   109  		t.Fatalf("newSubjectTokenProvider() = %v", err)
   110  	}
   111  
   112  	got, err := base.subjectToken(context.Background())
   113  	if err != nil {
   114  		t.Fatalf("base.subjectToken() = %v", err)
   115  	}
   116  	if want := "testTokenValue"; got != want {
   117  		t.Errorf("got %v, want %v", got, want)
   118  	}
   119  }
   120  

View as plain text