github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/rpc/jsonrpc/server/ws_handler_test.go (about) 1 package server 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/gorilla/websocket" 9 "github.com/stretchr/testify/require" 10 11 "github.com/tendermint/tendermint/libs/log" 12 types "github.com/tendermint/tendermint/rpc/jsonrpc/types" 13 ) 14 15 func TestWebsocketManagerHandler(t *testing.T) { 16 s := newWSServer() 17 defer s.Close() 18 19 // check upgrader works 20 d := websocket.Dialer{} 21 c, dialResp, err := d.Dial("ws://"+s.Listener.Addr().String()+"/websocket", nil) 22 require.NoError(t, err) 23 24 if got, want := dialResp.StatusCode, http.StatusSwitchingProtocols; got != want { 25 t.Errorf("dialResp.StatusCode = %q, want %q", got, want) 26 } 27 28 // check basic functionality works 29 req, err := types.MapToRequest( 30 types.JSONRPCStringID("TestWebsocketManager"), 31 "c", 32 map[string]interface{}{"s": "a", "i": 10}, 33 ) 34 require.NoError(t, err) 35 err = c.WriteJSON(req) 36 require.NoError(t, err) 37 38 var resp types.RPCResponse 39 err = c.ReadJSON(&resp) 40 require.NoError(t, err) 41 require.Nil(t, resp.Error) 42 dialResp.Body.Close() 43 } 44 45 func newWSServer() *httptest.Server { 46 funcMap := map[string]*RPCFunc{ 47 "c": NewWSRPCFunc(func(ctx *types.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"), 48 } 49 wm := NewWebsocketManager(funcMap) 50 wm.SetLogger(log.TestingLogger()) 51 52 mux := http.NewServeMux() 53 mux.HandleFunc("/websocket", wm.WebsocketHandler) 54 55 return httptest.NewServer(mux) 56 }