github.com/sacloud/libsacloud/v2@v2.32.3/helper/api/caller_test.go (about)

     1  // Copyright 2016-2022 The Libsacloud 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  //      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 api
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"testing"
    21  	"time"
    22  
    23  	sacloudhttp "github.com/sacloud/go-http"
    24  	"github.com/sacloud/libsacloud/v2"
    25  	"github.com/sacloud/libsacloud/v2/sacloud"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestNewCaller(t *testing.T) {
    30  	t.Run("basic", func(t *testing.T) {
    31  		caller := newCaller(&CallerOptions{
    32  			AccessToken:       "token",
    33  			AccessTokenSecret: "secret",
    34  		})
    35  		require.NotNil(t, caller)
    36  
    37  		client, ok := caller.(*sacloud.Client)
    38  		require.True(t, ok)
    39  
    40  		expected := &sacloud.Client{
    41  			AccessToken:       "token",
    42  			AccessTokenSecret: "secret",
    43  			UserAgent:         fmt.Sprintf("libsacloud/%s", libsacloud.Version),
    44  			AcceptLanguage:    "",
    45  			HTTPClient:        http.DefaultClient,
    46  		}
    47  		require.EqualValues(t, expected, client)
    48  	})
    49  
    50  	t.Run("custom", func(t *testing.T) {
    51  		caller := newCaller(&CallerOptions{
    52  			AccessToken:          "token",
    53  			AccessTokenSecret:    "secret",
    54  			APIRootURL:           "https://example.com",
    55  			AcceptLanguage:       "ja-JP",
    56  			RetryMax:             1,
    57  			RetryWaitMax:         2,
    58  			RetryWaitMin:         3,
    59  			HTTPRequestTimeout:   4,
    60  			HTTPRequestRateLimit: 5,
    61  			UserAgent:            "dummy",
    62  		})
    63  		require.NotNil(t, caller)
    64  
    65  		client, ok := caller.(*sacloud.Client)
    66  		require.True(t, ok)
    67  
    68  		expected := &sacloud.Client{
    69  			AccessToken:       "token",
    70  			AccessTokenSecret: "secret",
    71  			UserAgent:         "dummy",
    72  			AcceptLanguage:    "ja-JP",
    73  			RetryMax:          1,
    74  			RetryWaitMax:      2 * time.Second,
    75  			RetryWaitMin:      3 * time.Second,
    76  			HTTPClient:        http.DefaultClient,
    77  		}
    78  		require.EqualValues(t, expected, client)
    79  		require.Equal(t, time.Second*4, client.HTTPClient.Timeout)
    80  		require.EqualValues(t, 5, client.HTTPClient.Transport.(*sacloudhttp.RateLimitRoundTripper).RateLimitPerSec)
    81  	})
    82  
    83  	t.Run("multiple-call", func(t *testing.T) {
    84  		caller1 := newCaller(&CallerOptions{
    85  			AccessToken:       "1",
    86  			AccessTokenSecret: "1",
    87  		})
    88  		require.NotNil(t, caller1)
    89  
    90  		caller2 := newCaller(&CallerOptions{
    91  			AccessToken:       "2",
    92  			AccessTokenSecret: "2",
    93  		})
    94  		require.NotNil(t, caller2)
    95  
    96  		require.NotEqual(t, caller1, caller2)
    97  	})
    98  }