github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/funding_test.go (about)

     1  package rest_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/bitfinexcom/bitfinex-api-go/v2/rest"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestKeepFunding(t *testing.T) {
    15  	t.Run("wrong type", func(t *testing.T) {
    16  		c := rest.NewClient()
    17  		kf, err := c.Funding.KeepFunding(rest.KeepFundingRequest{Type: "foo"})
    18  		require.NotNil(t, err)
    19  		require.Nil(t, kf)
    20  	})
    21  
    22  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
    23  		handler := func(w http.ResponseWriter, r *http.Request) {
    24  			assert.Equal(t, "/auth/w/funding/keep", r.RequestURI)
    25  			assert.Equal(t, "POST", r.Method)
    26  
    27  			gotReqPld := rest.KeepFundingRequest{}
    28  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
    29  			require.Nil(t, err)
    30  
    31  			expectedReqPld := rest.KeepFundingRequest{Type: "loan", ID: 123}
    32  			assert.Equal(t, expectedReqPld, gotReqPld)
    33  
    34  			respMock := []interface{}{1568711312683, nil, nil, nil, nil, nil, nil, nil}
    35  			payload, _ := json.Marshal(respMock)
    36  			_, err = w.Write(payload)
    37  			require.Nil(t, err)
    38  		}
    39  
    40  		server := httptest.NewServer(http.HandlerFunc(handler))
    41  		defer server.Close()
    42  
    43  		c := rest.NewClientWithURL(server.URL)
    44  		pld := rest.KeepFundingRequest{Type: "loan", ID: 123}
    45  		rsp, err := c.Funding.KeepFunding(pld)
    46  		require.Nil(t, err)
    47  		assert.Equal(t, int64(1568711312683), rsp.MTS)
    48  	})
    49  }