github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/client/client_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/xml"
     6  	"net/http"
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/client"
    14  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
    15  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/mock"
    16  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    17  )
    18  
    19  type IpInfo struct {
    20  	xml.Name    `xml:"query"`
    21  	Country     string `json:"country" xml:"country"`
    22  	CountryCode string `json:"countryCode" xml:"countryCode"`
    23  }
    24  
    25  type GetByJSON struct {
    26  	httpx.MethodGet
    27  }
    28  
    29  func (GetByJSON) Path() string {
    30  	return "/me.json"
    31  }
    32  
    33  type GetByXML struct {
    34  	httpx.MethodGet
    35  }
    36  
    37  func (GetByXML) Path() string {
    38  	return "/me.xml"
    39  }
    40  
    41  func TestClient(t *testing.T) {
    42  	if runtime.GOOS == `darwin` {
    43  		return
    44  	}
    45  
    46  	cli := &client.Client{
    47  		Protocol: "https",
    48  		Host:     "ip.nf",
    49  		Timeout:  100 * time.Second,
    50  	}
    51  	cli.SetDefault()
    52  
    53  	t.Run("direct request", func(t *testing.T) {
    54  		req, _ := http.NewRequest("GET", "https://api.github.com", nil)
    55  		_, err := cli.Do(context.Background(), req).Into(nil)
    56  		require.NoError(t, err)
    57  	})
    58  
    59  	t.Run("direct request 404", func(t *testing.T) {
    60  		req, _ := http.NewRequest("GET", "https://api.github.com/xxxxn", nil)
    61  
    62  		meta, err := cli.Do(context.Background(), req).Into(nil)
    63  		require.Error(t, err)
    64  
    65  		t.Log(err)
    66  		t.Log(meta)
    67  	})
    68  
    69  	t.Run("request by struct", func(t *testing.T) {
    70  		rsp := IpInfo{}
    71  
    72  		meta, err := cli.Do(context.Background(), &GetByJSON{}).Into(&rsp)
    73  		require.NoError(t, err)
    74  
    75  		t.Log(rsp)
    76  		t.Log(meta)
    77  	})
    78  
    79  	t.Run("request by struct as xml", func(t *testing.T) {
    80  		rsp := IpInfo{}
    81  
    82  		meta, err := cli.Do(context.Background(), &GetByXML{}).Into(&rsp)
    83  		require.NoError(t, err)
    84  
    85  		t.Log(rsp)
    86  		t.Log(meta)
    87  	})
    88  
    89  	t.Run("cancel request", func(t *testing.T) {
    90  		ctx, cancel := context.WithCancel(context.Background())
    91  		go func() {
    92  			time.Sleep(1 * time.Millisecond)
    93  			cancel()
    94  		}()
    95  
    96  		rsp := IpInfo{}
    97  		_, err := cli.Do(ctx, &GetByJSON{}).Into(&rsp)
    98  		require.Equal(t, "ClientClosedRequest", err.(*statusx.StatusErr).Key)
    99  	})
   100  
   101  	t.Run("err request", func(t *testing.T) {
   102  		errcli := &client.Client{
   103  			Timeout: 100 * time.Second,
   104  		}
   105  		errcli.SetDefault()
   106  
   107  		{
   108  			rsp := IpInfo{}
   109  
   110  			_, err := errcli.Do(
   111  				client.ContextWithClient(
   112  					context.Background(),
   113  					client.GetShortConnClientContext(context.Background(), 10*time.Second),
   114  				),
   115  				&GetByJSON{},
   116  			).Into(&rsp)
   117  			require.Error(t, err)
   118  		}
   119  	})
   120  
   121  	t.Run("result pass", func(t *testing.T) {
   122  		req1, _ := http.NewRequest("GET", "https://ip.nf/me.json", nil)
   123  		res := cli.Do(context.Background(), req1)
   124  
   125  		req2, _ := http.NewRequest(http.MethodGet, "/", nil)
   126  		rw := mock.NewMockResponseWriter()
   127  		_ = httpx.ResponseFrom(res).WriteTo(rw, req2, nil)
   128  
   129  		require.Equal(t, 200, rw.StatusCode)
   130  	})
   131  }