...

Source file src/golang.org/x/oauth2/google/externalaccount/urlcredsource_test.go

Documentation: golang.org/x/oauth2/google/externalaccount

     1  // Copyright 2020 The Go Authors. All rights reserved.
     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 externalaccount
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  )
    14  
    15  var myURLToken = "testTokenValue"
    16  
    17  func TestRetrieveURLSubjectToken_Text(t *testing.T) {
    18  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		if r.Method != "GET" {
    20  			t.Errorf("Unexpected request method, %v is found", r.Method)
    21  		}
    22  		if r.Header.Get("Metadata") != "True" {
    23  			t.Errorf("Metadata header not properly included.")
    24  		}
    25  		w.Write([]byte("testTokenValue"))
    26  	}))
    27  	heads := make(map[string]string)
    28  	heads["Metadata"] = "True"
    29  	cs := CredentialSource{
    30  		URL:     ts.URL,
    31  		Format:  Format{Type: fileTypeText},
    32  		Headers: heads,
    33  	}
    34  	tfc := testFileConfig
    35  	tfc.CredentialSource = &cs
    36  
    37  	base, err := tfc.parse(context.Background())
    38  	if err != nil {
    39  		t.Fatalf("parse() failed %v", err)
    40  	}
    41  
    42  	out, err := base.subjectToken()
    43  	if err != nil {
    44  		t.Fatalf("retrieveSubjectToken() failed: %v", err)
    45  	}
    46  	if out != myURLToken {
    47  		t.Errorf("got %v but want %v", out, myURLToken)
    48  	}
    49  }
    50  
    51  // Checking that retrieveSubjectToken properly defaults to type text
    52  func TestRetrieveURLSubjectToken_Untyped(t *testing.T) {
    53  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    54  		if r.Method != "GET" {
    55  			t.Errorf("Unexpected request method, %v is found", r.Method)
    56  		}
    57  		w.Write([]byte("testTokenValue"))
    58  	}))
    59  	cs := CredentialSource{
    60  		URL: ts.URL,
    61  	}
    62  	tfc := testFileConfig
    63  	tfc.CredentialSource = &cs
    64  
    65  	base, err := tfc.parse(context.Background())
    66  	if err != nil {
    67  		t.Fatalf("parse() failed %v", err)
    68  	}
    69  
    70  	out, err := base.subjectToken()
    71  	if err != nil {
    72  		t.Fatalf("Failed to retrieve URL subject token: %v", err)
    73  	}
    74  	if out != myURLToken {
    75  		t.Errorf("got %v but want %v", out, myURLToken)
    76  	}
    77  }
    78  
    79  func TestRetrieveURLSubjectToken_JSON(t *testing.T) {
    80  	type tokenResponse struct {
    81  		TestToken string `json:"SubjToken"`
    82  	}
    83  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  		if got, want := r.Method, "GET"; got != want {
    85  			t.Errorf("got %v, but want %v", r.Method, want)
    86  		}
    87  		resp := tokenResponse{TestToken: "testTokenValue"}
    88  		jsonResp, err := json.Marshal(resp)
    89  		if err != nil {
    90  			t.Errorf("Failed to marshal values: %v", err)
    91  		}
    92  		w.Write(jsonResp)
    93  	}))
    94  	cs := CredentialSource{
    95  		URL:    ts.URL,
    96  		Format: Format{Type: fileTypeJSON, SubjectTokenFieldName: "SubjToken"},
    97  	}
    98  	tfc := testFileConfig
    99  	tfc.CredentialSource = &cs
   100  
   101  	base, err := tfc.parse(context.Background())
   102  	if err != nil {
   103  		t.Fatalf("parse() failed %v", err)
   104  	}
   105  
   106  	out, err := base.subjectToken()
   107  	if err != nil {
   108  		t.Fatalf("%v", err)
   109  	}
   110  	if out != myURLToken {
   111  		t.Errorf("got %v but want %v", out, myURLToken)
   112  	}
   113  }
   114  
   115  func TestURLCredential_CredentialSourceType(t *testing.T) {
   116  	cs := CredentialSource{
   117  		URL:    "http://example.com",
   118  		Format: Format{Type: fileTypeText},
   119  	}
   120  	tfc := testFileConfig
   121  	tfc.CredentialSource = &cs
   122  
   123  	base, err := tfc.parse(context.Background())
   124  	if err != nil {
   125  		t.Fatalf("parse() failed %v", err)
   126  	}
   127  
   128  	if got, want := base.credentialSourceType(), "url"; got != want {
   129  		t.Errorf("got %v but want %v", got, want)
   130  	}
   131  }
   132  

View as plain text