review comments:

- re-add test
- add TODO
- err instead of panic where possible
This commit is contained in:
Liamsi 2018-06-21 11:51:46 -07:00
parent c96b27136f
commit a39b2522d5
3 changed files with 52 additions and 2 deletions

View File

@ -268,6 +268,7 @@ func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
func signChallenge(challenge *[32]byte, locPrivKey crypto.PrivKey) (signature crypto.Signature) {
signature, err := locPrivKey.Sign(challenge[:])
// TODO(ismail): let signChallenge return an error instead
if err != nil {
panic(err)
}

View File

@ -16,8 +16,16 @@ import (
rs "github.com/tendermint/tendermint/rpc/lib/server"
types "github.com/tendermint/tendermint/rpc/lib/types"
"github.com/tendermint/tmlibs/log"
"github.com/gorilla/websocket"
)
//////////////////////////////////////////////////////////////////////////////
// HTTP REST API
// TODO
//////////////////////////////////////////////////////////////////////////////
// JSON-RPC over HTTP
func testMux() *http.ServeMux {
funcMap := map[string]*rs.RPCFunc{
"c": rs.NewRPCFunc(func(s string, i int) (string, error) { return "foo", nil }, "s,i"),
@ -108,3 +116,44 @@ func TestUnknownRPCPath(t *testing.T) {
// Always expecting back a 404 error
require.Equal(t, http.StatusNotFound, res.StatusCode, "should always return 404")
}
//////////////////////////////////////////////////////////////////////////////
// JSON-RPC over WEBSOCKETS
func TestWebsocketManagerHandler(t *testing.T) {
s := newWSServer()
defer s.Close()
// 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, err := types.MapToRequest(amino.NewCodec(), "TestWebsocketManager", "c", map[string]interface{}{"s": "a", "i": 10})
require.NoError(t, err)
err = c.WriteJSON(req)
require.NoError(t, err)
var resp types.RPCResponse
err = c.ReadJSON(&resp)
require.NoError(t, err)
require.Nil(t, resp.Error)
}
func newWSServer() *httptest.Server {
funcMap := map[string]*rs.RPCFunc{
"c": rs.NewWSRPCFunc(func(wsCtx types.WSRPCContext, s string, i int) (string, error) { return "foo", nil }, "s,i"),
}
wm := rs.NewWebsocketManager(funcMap, amino.NewCodec())
wm.SetLogger(log.TestingLogger())
mux := http.NewServeMux()
mux.HandleFunc("/websocket", wm.WebsocketHandler)
return httptest.NewServer(mux)
}

View File

@ -76,7 +76,7 @@ func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
signBytes := proposal.SignBytes(chainID)
sig, err := pv.privKey.Sign(signBytes)
if err != nil {
panic(err)
return err
}
proposal.Signature = sig
return nil
@ -86,7 +86,7 @@ func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
func (pv *MockPV) SignHeartbeat(chainID string, heartbeat *Heartbeat) error {
sig, err := pv.privKey.Sign(heartbeat.SignBytes(chainID))
if err != nil {
panic(err)
return err
}
heartbeat.Signature = sig
return nil