github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/testing/provider_client_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  	"github.com/opentelekomcloud/gophertelekomcloud/testhelper/client"
    14  )
    15  
    16  func TestAuthenticatedHeaders(t *testing.T) {
    17  	p := &golangsdk.ProviderClient{
    18  		TokenID: "1234",
    19  	}
    20  	expected := map[string]string{"X-Auth-Token": "1234"}
    21  	actual := p.AuthenticatedHeaders()
    22  	th.CheckDeepEquals(t, expected, actual)
    23  }
    24  
    25  func TestUserAgent(t *testing.T) {
    26  	p := &golangsdk.ProviderClient{}
    27  
    28  	p.UserAgent.Prepend("custom-user-agent/2.4.0")
    29  	expected := "custom-user-agent/2.4.0 golangsdk/2.0.0"
    30  	actual := p.UserAgent.Join()
    31  	th.CheckEquals(t, expected, actual)
    32  
    33  	p.UserAgent.Prepend("another-custom-user-agent/0.3.0", "a-third-ua/5.9.0")
    34  	expected = "another-custom-user-agent/0.3.0 a-third-ua/5.9.0 custom-user-agent/2.4.0 golangsdk/2.0.0"
    35  	actual = p.UserAgent.Join()
    36  	th.CheckEquals(t, expected, actual)
    37  
    38  	p.UserAgent = golangsdk.UserAgent{}
    39  	expected = "golangsdk/2.0.0"
    40  	actual = p.UserAgent.Join()
    41  	th.CheckEquals(t, expected, actual)
    42  }
    43  
    44  func TestConcurrentReauth(t *testing.T) {
    45  	var info = struct {
    46  		numreauths int
    47  		mut        *sync.RWMutex
    48  	}{
    49  		0,
    50  		new(sync.RWMutex),
    51  	}
    52  
    53  	numconc := 20
    54  
    55  	prereauthTok := client.TokenID
    56  	postreauthTok := "12345678"
    57  
    58  	p := new(golangsdk.ProviderClient)
    59  	p.UseTokenLock()
    60  	p.SetToken(prereauthTok)
    61  	p.ReauthFunc = func() error {
    62  		time.Sleep(1 * time.Second)
    63  		p.AuthenticatedHeaders()
    64  		info.mut.Lock()
    65  		info.numreauths++
    66  		info.mut.Unlock()
    67  		p.TokenID = postreauthTok
    68  		return nil
    69  	}
    70  
    71  	th.SetupHTTP()
    72  	defer th.TeardownHTTP()
    73  
    74  	th.Mux.HandleFunc("/route", func(w http.ResponseWriter, r *http.Request) {
    75  		if r.Header.Get("X-Auth-Token") != postreauthTok {
    76  			w.WriteHeader(http.StatusUnauthorized)
    77  			return
    78  		}
    79  		info.mut.RLock()
    80  		hasReauthed := info.numreauths != 0
    81  		info.mut.RUnlock()
    82  
    83  		if hasReauthed {
    84  			th.CheckEquals(t, p.Token(), postreauthTok)
    85  		}
    86  
    87  		w.Header().Add("Content-Type", "application/json")
    88  		_, _ = fmt.Fprint(w, `{}`)
    89  	})
    90  
    91  	wg := new(sync.WaitGroup)
    92  	reqopts := new(golangsdk.RequestOpts)
    93  
    94  	for i := 0; i < numconc; i++ {
    95  		wg.Add(1)
    96  		go func() {
    97  			defer wg.Done()
    98  			resp, err := p.Request("GET", fmt.Sprintf("%s/route", th.Endpoint()), reqopts)
    99  			th.CheckNoErr(t, err)
   100  			if resp == nil {
   101  				t.Errorf("got a nil response")
   102  				return
   103  			}
   104  			if resp.Body == nil {
   105  				t.Errorf("response body was nil")
   106  				return
   107  			}
   108  			defer resp.Body.Close()
   109  			actual, err := ioutil.ReadAll(resp.Body)
   110  			if err != nil {
   111  				t.Errorf("error reading response body: %s", err)
   112  				return
   113  			}
   114  			th.CheckByteArrayEquals(t, []byte(`{}`), actual)
   115  		}()
   116  	}
   117  
   118  	wg.Wait()
   119  
   120  	th.AssertEquals(t, 1, info.numreauths)
   121  }