github.com/DFWallet/tendermint-cosmos@v0.0.2/test/fuzz/rpc/jsonrpc/server/handler.go (about)

     1  package handler
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  
    10  	"github.com/DFWallet/tendermint-cosmos/libs/log"
    11  	rs "github.com/DFWallet/tendermint-cosmos/rpc/jsonrpc/server"
    12  	types "github.com/DFWallet/tendermint-cosmos/rpc/jsonrpc/types"
    13  )
    14  
    15  var rpcFuncMap = map[string]*rs.RPCFunc{
    16  	"c": rs.NewRPCFunc(func(s string, i int) (string, int) { return "foo", 200 }, "s,i"),
    17  }
    18  var mux *http.ServeMux
    19  
    20  func init() {
    21  	mux := http.NewServeMux()
    22  	buf := new(bytes.Buffer)
    23  	lgr := log.NewTMLogger(buf)
    24  	rs.RegisterRPCFuncs(mux, rpcFuncMap, lgr)
    25  }
    26  
    27  func Fuzz(data []byte) int {
    28  	req, _ := http.NewRequest("POST", "http://localhost/", bytes.NewReader(data))
    29  	rec := httptest.NewRecorder()
    30  	mux.ServeHTTP(rec, req)
    31  	res := rec.Result()
    32  	blob, err := ioutil.ReadAll(res.Body)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	if err := res.Body.Close(); err != nil {
    37  		panic(err)
    38  	}
    39  	recv := new(types.RPCResponse)
    40  	if err := json.Unmarshal(blob, recv); err != nil {
    41  		panic(err)
    42  	}
    43  	return 1
    44  }