2020-04-29 19:51:43 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-05 14:56:17 +00:00
|
|
|
"encoding/json"
|
2021-03-17 13:40:24 +00:00
|
|
|
"fmt"
|
2020-05-12 08:18:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-05-18 13:23:51 +00:00
|
|
|
"strings"
|
2020-04-29 19:51:43 +00:00
|
|
|
"testing"
|
2020-05-12 08:18:44 +00:00
|
|
|
"time"
|
2020-04-29 19:51:43 +00:00
|
|
|
|
2020-05-12 08:18:44 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2020-06-18 06:43:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-05-13 10:16:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-04-29 19:51:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWSClientClose(t *testing.T) {
|
|
|
|
srv := initTestServer(t, "")
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-04-29 19:51:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
wsc.Close()
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
|
|
|
|
func TestWSClientSubscription(t *testing.T) {
|
|
|
|
var cases = map[string]func(*WSClient) (string, error){
|
2020-05-13 10:16:42 +00:00
|
|
|
"blocks": func(wsc *WSClient) (string, error) {
|
|
|
|
return wsc.SubscribeForNewBlocks(nil)
|
|
|
|
},
|
|
|
|
"transactions": func(wsc *WSClient) (string, error) {
|
|
|
|
return wsc.SubscribeForNewTransactions(nil, nil)
|
|
|
|
},
|
|
|
|
"notifications": func(wsc *WSClient) (string, error) {
|
2020-08-04 13:24:32 +00:00
|
|
|
return wsc.SubscribeForExecutionNotifications(nil, nil)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
"executions": func(wsc *WSClient) (string, error) {
|
|
|
|
return wsc.SubscribeForTransactionExecutions(nil)
|
|
|
|
},
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
for name, f := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
id, err := f(wsc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "55aaff00", id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("bad", func(t *testing.T) {
|
|
|
|
for name, f := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`)
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
_, err = f(wsc)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSClientUnsubscription(t *testing.T) {
|
|
|
|
type responseCheck struct {
|
|
|
|
response string
|
|
|
|
code func(*testing.T, *WSClient)
|
|
|
|
}
|
|
|
|
var cases = map[string]responseCheck{
|
|
|
|
"good": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
|
|
|
wsc.subscriptions["0"] = true
|
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}},
|
|
|
|
"all": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
|
|
|
wsc.subscriptions["0"] = true
|
|
|
|
err := wsc.UnsubscribeAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(wsc.subscriptions))
|
|
|
|
}},
|
|
|
|
"not subscribed": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
"error returned": {`{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
|
|
|
wsc.subscriptions["0"] = true
|
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
"false returned": {`{"jsonrpc": "2.0", "id": 1, "result": false}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
|
|
|
wsc.subscriptions["0"] = true
|
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for name, rc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, rc.response)
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
rc.code(t, wsc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSClientEvents(t *testing.T) {
|
|
|
|
var ok bool
|
|
|
|
// Events from RPC server test chain.
|
|
|
|
var events = []string{
|
2020-11-12 10:06:11 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"transaction_executed","params":[{"container":"0xe1cd5e57e721d2a2e05fb1f08721b12057b25ab1dd7fd0f33ee1639932fdfad7","trigger":"Application","vmstate":"HALT","gasconsumed":"22910000","stack":[],"notifications":[{"contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}]}},{"contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"transfer","state":{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}}]}]}`,
|
2021-09-24 09:15:25 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"notification_from_execution","params":[{"container":"0xe1cd5e57e721d2a2e05fb1f08721b12057b25ab1dd7fd0f33ee1639932fdfad7","contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}]}}]}`,
|
2020-11-12 10:06:11 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"transaction_executed","params":[{"container":"0xf97a72b7722c109f909a8bc16c22368c5023d85828b09b127b237aace33cf099","trigger":"Application","vmstate":"HALT","gasconsumed":"6042610","stack":[],"notifications":[{"contract":"0xe65ff7b3a02d207b584a5c27057d4e9862ef01da","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"ByteString","value":"IHKCdK+vw29DoHHTKM+j5inZy7A="},{"type":"Integer","value":"123"}]}]}},{"contract":"0xe65ff7b3a02d207b584a5c27057d4e9862ef01da","eventname":"transfer","state":{"type":"Array","value":[{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"ByteString","value":"IHKCdK+vw29DoHHTKM+j5inZy7A="},{"type":"Integer","value":"123"}]}}]}]}`,
|
2021-03-17 13:40:24 +00:00
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
2020-05-12 19:38:29 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"event_missed","params":[]}`,
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, event := range events {
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2020-05-12 08:18:44 +00:00
|
|
|
err = ws.WriteMessage(1, []byte(event))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2022-02-21 10:41:14 +00:00
|
|
|
wsc.cache.initDone = true // Our server mock is restricted, so perform initialisation manually.
|
2022-02-21 10:13:39 +00:00
|
|
|
wsc.cache.network = netmode.UnitTestNet
|
2020-05-12 08:18:44 +00:00
|
|
|
for range events {
|
|
|
|
select {
|
|
|
|
case _, ok = <-wsc.Notifications:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("timeout waiting for event")
|
|
|
|
}
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case _, ok = <-wsc.Notifications:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("timeout waiting for event")
|
|
|
|
}
|
|
|
|
// Connection closed by server.
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
2020-05-13 10:16:42 +00:00
|
|
|
|
|
|
|
func TestWSExecutionVMStateCheck(t *testing.T) {
|
|
|
|
// Will answer successfully if request slips through.
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-13 10:16:42 +00:00
|
|
|
filter := "NONE"
|
|
|
|
_, err = wsc.SubscribeForTransactionExecutions(&filter)
|
|
|
|
require.Error(t, err)
|
|
|
|
wsc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSFilteredSubscriptions(t *testing.T) {
|
|
|
|
var cases = []struct {
|
|
|
|
name string
|
|
|
|
clientCode func(*testing.T, *WSClient)
|
|
|
|
serverCode func(*testing.T, *request.Params)
|
|
|
|
}{
|
|
|
|
{"blocks",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
primary := 3
|
|
|
|
_, err := wsc.SubscribeForNewBlocks(&primary)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.BlockFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, 3, filt.Primary)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"transactions sender",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
sender := util.Uint160{1, 2, 3, 4, 5}
|
|
|
|
_, err := wsc.SubscribeForNewTransactions(&sender, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Nil(t, filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-29 16:57:38 +00:00
|
|
|
{"transactions signer",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
2020-07-29 16:57:38 +00:00
|
|
|
signer := util.Uint160{0, 42}
|
|
|
|
_, err := wsc.SubscribeForNewTransactions(nil, &signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Nil(t, filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Equal(t, util.Uint160{0, 42}, *filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-29 16:57:38 +00:00
|
|
|
{"transactions sender and signer",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
sender := util.Uint160{1, 2, 3, 4, 5}
|
2020-07-29 16:57:38 +00:00
|
|
|
signer := util.Uint160{0, 42}
|
|
|
|
_, err := wsc.SubscribeForNewTransactions(&sender, &signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Equal(t, util.Uint160{0, 42}, *filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-04 13:24:32 +00:00
|
|
|
{"notifications contract hash",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
contract := util.Uint160{1, 2, 3, 4, 5}
|
2020-08-04 13:24:32 +00:00
|
|
|
_, err := wsc.SubscribeForExecutionNotifications(&contract, nil)
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Contract)
|
|
|
|
require.Nil(t, filt.Name)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"notifications name",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
name := "my_pretty_notification"
|
|
|
|
_, err := wsc.SubscribeForExecutionNotifications(nil, &name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, "my_pretty_notification", *filt.Name)
|
|
|
|
require.Nil(t, filt.Contract)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"notifications contract hash and name",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
contract := util.Uint160{1, 2, 3, 4, 5}
|
|
|
|
name := "my_pretty_notification"
|
|
|
|
_, err := wsc.SubscribeForExecutionNotifications(&contract, &name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Contract)
|
|
|
|
require.Equal(t, "my_pretty_notification", *filt.Name)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{"executions",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
state := "FAULT"
|
|
|
|
_, err := wsc.SubscribeForTransactionExecutions(&state)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *request.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2021-08-05 14:56:17 +00:00
|
|
|
filt := new(request.ExecutionFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, "FAULT", filt.State)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2020-05-13 10:16:42 +00:00
|
|
|
req := request.In{}
|
|
|
|
err = ws.ReadJSON(&req)
|
|
|
|
require.NoError(t, err)
|
2021-10-28 11:10:18 +00:00
|
|
|
params := request.Params(req.RawParams)
|
|
|
|
c.serverCode(t, ¶ms)
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2020-05-13 10:16:42 +00:00
|
|
|
err = ws.WriteMessage(1, []byte(`{"jsonrpc": "2.0", "id": 1, "result": "0"}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
}))
|
2020-10-14 15:13:20 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2022-02-21 10:13:39 +00:00
|
|
|
wsc.cache.network = netmode.UnitTestNet
|
2020-05-13 10:16:42 +00:00
|
|
|
c.clientCode(t, wsc)
|
|
|
|
wsc.Close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 13:23:51 +00:00
|
|
|
|
|
|
|
func TestNewWS(t *testing.T) {
|
|
|
|
srv := initTestServer(t, "")
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
2020-10-14 15:13:20 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{})
|
2020-05-18 13:23:51 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
c.getNextRequestID = getTestRequestID
|
|
|
|
c.cache.network = netmode.UnitTestNet
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, c.Init())
|
2020-05-18 13:23:51 +00:00
|
|
|
})
|
|
|
|
t.Run("bad URL", func(t *testing.T) {
|
2021-05-12 16:15:21 +00:00
|
|
|
_, err := NewWS(context.TODO(), strings.TrimPrefix(srv.URL, "http://"), Options{})
|
2020-05-18 13:23:51 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|