Files
tendermint/rpc/jsonrpc/server/ws_handler_test.go
M. J. Fromberger 75b1b1d6c5 rpc: simplify the handling of JSON-RPC request and response IDs (#7738)
* rpc: simplify the handling of JSON-RPC request and response IDs

Replace the ID wrapper interface with plain JSON. Internally, the client
libraries use only integer IDs, and the server does not care about the ID
structure apart from checking its validity.

Basic structure of this change:

- Remove the jsonrpcid interface and its helpers.
- Unexport the ID field of request and response.
- Add helpers for constructing requests and responses.
- Fix up usage and tests.
2022-01-31 12:11:42 -08:00

61 lines
1.5 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/fortytw2/leaktest"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
func TestWebsocketManagerHandler(t *testing.T) {
logger := log.NewNopLogger()
s := newWSServer(t, logger)
defer s.Close()
t.Cleanup(leaktest.Check(t))
// check upgrader works
d := websocket.Dialer{}
c, dialResp, err := d.Dial("ws://"+s.Listener.Addr().String()+"/websocket", nil)
require.NoError(t, err)
if got, want := dialResp.StatusCode, http.StatusSwitchingProtocols; got != want {
t.Errorf("dialResp.StatusCode = %q, want %q", got, want)
}
// check basic functionality works
req := rpctypes.NewRequest(1001)
require.NoError(t, req.SetMethodAndParams("c", map[string]interface{}{"s": "a", "i": 10}))
require.NoError(t, c.WriteJSON(req))
var resp rpctypes.RPCResponse
err = c.ReadJSON(&resp)
require.NoError(t, err)
require.Nil(t, resp.Error)
dialResp.Body.Close()
}
func newWSServer(t *testing.T, logger log.Logger) *httptest.Server {
funcMap := map[string]*RPCFunc{
"c": NewWSRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s", "i"),
}
wm := NewWebsocketManager(logger, funcMap)
mux := http.NewServeMux()
mux.HandleFunc("/websocket", wm.WebsocketHandler)
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv
}