Files
gocryptotrader/exchanges/kraken/mock_ws_test.go
Ryan O'Hara-Reid e99adca86f encoding/json: Add custom JSON package with build tag support for Sonic (#1623)
* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2025-02-20 16:05:55 +11:00

77 lines
2.0 KiB
Go

package kraken
import (
"fmt"
"strings"
"testing"
"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
func mockWsServer(tb testing.TB, msg []byte, w *websocket.Conn) error {
tb.Helper()
event, err := jsonparser.GetUnsafeString(msg, "event")
if err != nil {
return err
}
switch event {
case krakenWsCancelOrder:
return mockWsCancelOrders(tb, msg, w)
case krakenWsAddOrder:
return mockWsAddOrder(tb, msg, w)
}
return nil
}
func mockWsCancelOrders(tb testing.TB, msg []byte, w *websocket.Conn) error {
tb.Helper()
var req WsCancelOrderRequest
if err := json.Unmarshal(msg, &req); err != nil {
return err
}
resp := WsCancelOrderResponse{
Event: krakenWsCancelOrderStatus,
Status: "ok",
RequestID: req.RequestID,
Count: int64(len(req.TransactionIDs)),
}
if len(req.TransactionIDs) == 0 || strings.Contains(req.TransactionIDs[0], "FISH") { // Reject anything that smells suspicious
resp.Status = "error"
resp.ErrorMessage = "[EOrder:Unknown order]"
}
msg, err := json.Marshal(resp)
if err != nil {
return err
}
return w.WriteMessage(websocket.TextMessage, msg)
}
func mockWsAddOrder(tb testing.TB, msg []byte, w *websocket.Conn) error {
tb.Helper()
var req WsAddOrderRequest
if err := json.Unmarshal(msg, &req); err != nil {
return err
}
assert.Equal(tb, "buy", req.OrderSide, "OrderSide should be correct")
assert.Equal(tb, "limit", req.OrderType, "OrderType should be correct")
assert.Equal(tb, "XBT/USD", req.Pair, "Pair should be correct")
assert.Equal(tb, 80000.0, req.Price, "Pair should be correct")
resp := WsAddOrderResponse{
Event: krakenWsAddOrderStatus,
Status: "ok",
RequestID: req.RequestID,
TransactionID: "ONPNXH-KMKMU-F4MR5V",
Description: fmt.Sprintf("%s %.f %s @ %s %.f", req.OrderSide, req.Volume, req.Pair, req.OrderSide, req.Price),
}
msg, err := json.Marshal(resp)
if err != nil {
return err
}
return w.WriteMessage(websocket.TextMessage, msg)
}