Files
gocryptotrader/internal/testing/websocket/mock.go
Adrian Gallagher a5b638bfb7 GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues

These checks include:
- Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`).
- Replacing `%s` with %q
- Enforcing consistent usage of should/must wording for testify assert/require messages

* Add support for checking backticked string format specifiers and fix issues

* tests: Fix error comparisons

* tests: Replace errors.Is(err, nil) usage with testify and automate check

* refactor: Rename ExtractPort to ExtractPortOrDefault

* tests: Replace assert with require for error handling in multiple test files

* tests: Replace assert with require for error handling and improve assertions in data tests

* tests: Fix typo in assertion message for StreamVol test

* OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions

* OKX: Revert intentional error check

* Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
2025-05-28 12:26:51 +10:00

50 lines
1.6 KiB
Go

package websocket
import (
"net/http"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var upgrader = websocket.Upgrader{CheckOrigin: func(_ *http.Request) bool { return true }}
// WsMockFunc is a websocket handler to be called with each websocket message
type WsMockFunc func(testing.TB, []byte, *websocket.Conn) error
// CurryWsMockUpgrader curries a WsMockUpgrader with a testing.TB and a mock func
// bridging the gap between information known before the Server is created and during a request
func CurryWsMockUpgrader(tb testing.TB, wsHandler WsMockFunc) http.HandlerFunc {
tb.Helper()
return func(w http.ResponseWriter, r *http.Request) {
WsMockUpgrader(tb, w, r, wsHandler)
}
}
// WsMockUpgrader handles upgrading an initial HTTP request to WS, and then runs a for loop calling the mock func on each input
func WsMockUpgrader(tb testing.TB, w http.ResponseWriter, r *http.Request, wsHandler WsMockFunc) {
tb.Helper()
c, err := upgrader.Upgrade(w, r, nil)
require.NoError(tb, err, "Upgrade connection must not error")
defer c.Close()
for {
_, p, err := c.ReadMessage()
if err != nil {
// Any error here is likely due to the connection closing
return
}
err = wsHandler(tb, p, c)
assert.NoError(tb, err, "WS Mock Function should not error")
}
}
// EchoHandler is a simple echo function after a read, this doesn't need to worry if writing to the connection fails
func EchoHandler(_ testing.TB, p []byte, c *websocket.Conn) error {
time.Sleep(time.Nanosecond) // Shift clock to simulate time passing
_ = c.WriteMessage(websocket.TextMessage, p)
return nil
}