encode params before sending in JSONRPC

This commit is contained in:
Anton Kaliaev 2017-03-09 13:46:48 +04:00
parent cf11e6ba65
commit 05e1a22d5b
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 48 additions and 2 deletions

View File

@ -67,11 +67,16 @@ func NewClientJSONRPC(remote string) *ClientJSONRPC {
} }
func (c *ClientJSONRPC) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) { func (c *ClientJSONRPC) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
// Make request and get responseBytes // we need this step because we attempt to decode values using `go-wire`
// (handlers.go:176) on the server side
encodedParams := make(map[string]interface{})
for k, v := range params {
encodedParams[k] = json.RawMessage(wire.JSONBytes(v))
}
request := types.RPCRequest{ request := types.RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: method, Method: method,
Params: params, Params: encodedParams,
ID: "", ID: "",
} }
requestBytes, err := json.Marshal(request) requestBytes, err := json.Marshal(request)

View File

@ -1,6 +1,9 @@
package rpc package rpc
import ( import (
"bytes"
crand "crypto/rand"
"math/rand"
"net/http" "net/http"
"os/exec" "os/exec"
"testing" "testing"
@ -29,14 +32,20 @@ type ResultStatus struct {
Value string Value string
} }
type ResultBytes struct {
Value []byte
}
var _ = wire.RegisterInterface( var _ = wire.RegisterInterface(
struct{ Result }{}, struct{ Result }{},
wire.ConcreteType{&ResultStatus{}, 0x1}, wire.ConcreteType{&ResultStatus{}, 0x1},
wire.ConcreteType{&ResultBytes{}, 0x2},
) )
// Define some routes // Define some routes
var Routes = map[string]*server.RPCFunc{ var Routes = map[string]*server.RPCFunc{
"status": server.NewRPCFunc(StatusResult, "arg"), "status": server.NewRPCFunc(StatusResult, "arg"),
"bytes": server.NewRPCFunc(BytesResult, "arg"),
} }
// an rpc function // an rpc function
@ -44,6 +53,10 @@ func StatusResult(v string) (Result, error) {
return &ResultStatus{v}, nil return &ResultStatus{v}, nil
} }
func BytesResult(v []byte) (Result, error) {
return &ResultBytes{v}, nil
}
// launch unix and tcp servers // launch unix and tcp servers
func init() { func init() {
cmd := exec.Command("rm", "-f", unixSocket) cmd := exec.Command("rm", "-f", unixSocket)
@ -214,3 +227,31 @@ func TestQuotedStringArg(t *testing.T) {
t.Fatalf("Got: %v .... Expected: %v \n", got, val) t.Fatalf("Got: %v .... Expected: %v \n", got, val)
} }
} }
func randBytes(t *testing.T) []byte {
n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
if err != nil {
t.Fatal(err)
}
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
}
func TestByteSliceViaJSONRPC(t *testing.T) {
cl := client.NewClientJSONRPC(unixAddr)
val := randBytes(t)
params := map[string]interface{}{
"arg": val,
}
var result Result
_, err := cl.Call("bytes", params, &result)
if err != nil {
t.Fatal(err)
}
got := result.(*ResultBytes).Value
if bytes.Compare(got, val) != 0 {
t.Fatalf("Got: %v .... Expected: %v \n", got, val)
}
}