63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type TestProvider struct {
|
|
handlerCalled bool
|
|
}
|
|
|
|
func (mp *TestProvider) messageHandler(int, *WebsocketConnection, []byte) {
|
|
mp.handlerCalled = true
|
|
}
|
|
|
|
func TestWebsocketController_readSuccess(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
messageType int
|
|
bz []byte
|
|
shouldCallMessageHandler bool
|
|
}{
|
|
{
|
|
"message length is zero",
|
|
1,
|
|
[]byte{},
|
|
false,
|
|
},
|
|
{
|
|
"message is a pong",
|
|
1,
|
|
[]byte("pong"),
|
|
false,
|
|
},
|
|
{
|
|
"is a valid message for the messageHandler",
|
|
1,
|
|
[]byte("asdf"),
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
provider := TestProvider{}
|
|
mockClient := new(websocket.Conn)
|
|
c := &WebsocketController{
|
|
providerName: ProviderMock,
|
|
connections: []*WebsocketConnection{
|
|
{
|
|
messageHandler: provider.messageHandler,
|
|
client: mockClient,
|
|
},
|
|
},
|
|
}
|
|
c.connections[0].readSuccess(testCase.messageType, testCase.bz)
|
|
require.Equal(t, provider.handlerCalled, testCase.shouldCallMessageHandler)
|
|
})
|
|
}
|
|
}
|