github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/tickers_hist_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/pkg/models/tickerhist" 10 "github.com/bitfinexcom/bitfinex-api-go/v2/rest" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestPublicTickerHistoryResource(t *testing.T) { 16 t.Run("missing arguments", func(t *testing.T) { 17 c := rest.NewClient() 18 th, err := c.TickersHistory.Get(rest.GetTickerHistPayload{}) 19 require.NotNil(t, err) 20 require.Nil(t, th) 21 }) 22 23 t.Run("response data slice too short", func(t *testing.T) { 24 handler := func(w http.ResponseWriter, r *http.Request) { 25 respMock := []interface{}{"abc123"} 26 payload, _ := json.Marshal(respMock) 27 _, err := w.Write(payload) 28 require.Nil(t, err) 29 } 30 31 server := httptest.NewServer(http.HandlerFunc(handler)) 32 defer server.Close() 33 34 c := rest.NewClientWithURL(server.URL) 35 th, err := c.TickersHistory.Get(rest.GetTickerHistPayload{}) 36 require.NotNil(t, err) 37 require.Nil(t, th) 38 }) 39 40 t.Run("valid req payload and resp data", func(t *testing.T) { 41 handler := func(w http.ResponseWriter, r *http.Request) { 42 assert.Equal(t, "/tickers/hist?end=123456&limit=5&start=123456&symbols=tBTCUSD", r.RequestURI) 43 respMock := []interface{}{ 44 []interface{}{ 45 "tBTCUSD", 54281, nil, 54282, nil, nil, nil, nil, nil, nil, nil, nil, 1619769715000, 46 }, 47 } 48 payload, _ := json.Marshal(respMock) 49 _, err := w.Write(payload) 50 require.Nil(t, err) 51 } 52 53 server := httptest.NewServer(http.HandlerFunc(handler)) 54 defer server.Close() 55 56 c := rest.NewClientWithURL(server.URL) 57 th, err := c.TickersHistory.Get(rest.GetTickerHistPayload{ 58 Symbols: []string{"tBTCUSD"}, 59 Limit: 5, 60 Start: 123456, 61 End: 123456, 62 }) 63 require.Nil(t, err) 64 65 expected := []tickerhist.TickerHist{ 66 { 67 Symbol: "tBTCUSD", 68 Bid: 54281, 69 Ask: 54282, 70 MTS: 1619769715000, 71 }, 72 } 73 assert.Equal(t, expected, th) 74 }) 75 }