github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sign/auto_sign_transport_test.go (about)

     1  package sign
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/artisanhe/tools/courier/client"
    11  	"github.com/artisanhe/tools/courier/status_error"
    12  )
    13  
    14  func TestAutoSignTransport(t *testing.T) {
    15  	tt := assert.New(t)
    16  
    17  	ipInfoClient := client.Client{
    18  		Service: "test",
    19  		Mode:    "http",
    20  		Host:    "ip-api.com",
    21  		Timeout: 100 * time.Second,
    22  		WrapTransport: NewAutoSignTransport(func(key string) (string, error) {
    23  			return "111", nil
    24  		}),
    25  	}
    26  
    27  	p := SignParams{
    28  		AccessKey:  "123",
    29  		RandString: "1123",
    30  	}
    31  
    32  	ipInfo := IpInfo{}
    33  	err := ipInfoClient.
    34  		Request("id", "GET", "/json", p).
    35  		Do().
    36  		Into(&ipInfo)
    37  
    38  	if err == nil {
    39  		tt.Equal("China", ipInfo.Country)
    40  		tt.Equal("CN", ipInfo.CountryCode)
    41  	} else {
    42  		spew.Dump(err.(*status_error.StatusError))
    43  	}
    44  }
    45  
    46  type IpInfo struct {
    47  	Country     string `json:"country"`
    48  	CountryCode string `json:"countryCode"`
    49  }