Skip to content

Commit

Permalink
fixup! Huobi: Add V2 websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 30, 2024
1 parent 7af5aea commit d226051
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions exchanges/huobi/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package huobi

import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"sync"
"testing"
"time"

"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
Expand All @@ -27,6 +31,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
mockws "github.com/thrasher-corp/gocryptotrader/internal/testing/websocket"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)

Expand Down Expand Up @@ -1917,36 +1922,32 @@ func TestGenerateSubscriptions(t *testing.T) {
testsubs.EqualLists(t, exp, subs)
}

// TestSubscribe exercises live subscriptions for public channels
func TestSubscribe(t *testing.T) {
t.Parallel()
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Test instance Setup must not error")
h.Websocket.SetCanUseAuthenticatedEndpoints(true)
subs, err := h.Features.Subscriptions.ExpandTemplates(h)
require.NoError(t, err, "ExpandTemplates must not error")
h.Features.Subscriptions = subscription.List{}
testexch.SetupWs(t, h)
err = h.Subscribe(subs)
require.NoError(t, err, "Subscribe must not error")
got := h.Websocket.GetSubscriptions()
require.Len(t, got, len(subs))
for _, s := range got {
assert.Equal(t, subscription.SubscribedState, s.State())
func wsFixture(tb testing.TB, msg []byte, w *websocket.Conn) error {
tb.Helper()
action, _ := jsonparser.GetString(msg, "action")
ch, _ := jsonparser.GetString(msg, "ch")
if action == "req" && ch == "auth" {
return w.WriteMessage(websocket.TextMessage, []byte(`{"action":"req","code":200,"ch":"auth","data":{}}`))
}
if action == "sub" {
return w.WriteMessage(websocket.TextMessage, []byte(`{"action":"sub","code":200,"ch":"`+ch+`"}`))
}
id, _ := jsonparser.GetString(msg, "id")
sub, _ := jsonparser.GetString(msg, "sub")
if id != "" && sub != "" {
return w.WriteMessage(websocket.TextMessage, []byte(`{"id":"`+id+`","status":"ok","subbed":"`+sub+`"}`))
}
return fmt.Errorf("%w: %s", errors.New("Unhandled mock websocket message"), msg)
}

// TestAuthSubscribe exercises mock subscriptions for private channels
func TestAuthSubscribe(t *testing.T) {
// TODO: currently using live - need to implement mock
// TestSubscribe exercises mock subscriptions
func TestSubscribe(t *testing.T) {
t.Parallel()
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Test instance Setup must not error")
subCfg := h.Features.Subscriptions
h := testexch.MockWsInstance[HUOBI](t, mockws.CurryWsMockUpgrader(t, wsFixture)) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
h.Websocket.SetCanUseAuthenticatedEndpoints(true)
subs, err := h.Features.Subscriptions.ExpandTemplates(h)
subs, err := subCfg.ExpandTemplates(h)
require.NoError(t, err, "ExpandTemplates must not error")
h.Features.Subscriptions = subscription.List{}
testexch.SetupWs(t, h)
err = h.Subscribe(subs)
require.NoError(t, err, "Subscribe must not error")
got := h.Websocket.GetSubscriptions()
Expand All @@ -1962,10 +1963,6 @@ func TestChannelName(t *testing.T) {
t.Error("more to come")
}

func TestAuthLogin(t *testing.T) {
t.Log(`{"action":"req","code":2002,"ch":"auth","message":"auth.fail"}`)
}

func TestGetErrResp(t *testing.T) {
err := getErrResp([]byte(`{"status":"error","err-code":"bad-request","err-msg":"invalid topic promiscuous.drop🐻s.nearby"}`))
assert.ErrorContains(t, err, "invalid topic promiscuous.drop🐻s.nearby (bad-request)", "V1 errors must return correctly")
Expand Down

0 comments on commit d226051

Please sign in to comment.