Browse Source

WSClient failed to "echo_bytes"

Error:
```
Expected nil, but got: encoding/hex: invalid byte: U+0078 'x'
```
pull/456/head
Anton Kaliaev 8 years ago
parent
commit
3233c9c003
No known key found for this signature in database GPG Key ID: 7B6881D965918214
1 changed files with 77 additions and 35 deletions
  1. +77
    -35
      rpc_test.go

+ 77
- 35
rpc_test.go View File

@ -97,10 +97,9 @@ func init() {
// wait for servers to start
time.Sleep(time.Second * 2)
}
func echo(cl client.HTTPClient, val string) (string, error) {
func echoViaHTTP(cl client.HTTPClient, val string) (string, error) {
params := map[string]interface{}{
"arg": val,
}
@ -111,15 +110,30 @@ func echo(cl client.HTTPClient, val string) (string, error) {
return result.(*ResultEcho).Value, nil
}
func echoBytesViaHTTP(cl client.HTTPClient, bytes []byte) ([]byte, error) {
params := map[string]interface{}{
"arg": bytes,
}
var result Result
if _, err := cl.Call("echo_bytes", params, &result); err != nil {
return []byte{}, err
}
return result.(*ResultEchoBytes).Value, nil
}
func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
val := "acbd"
got, err := echo(cl, val)
got, err := echoViaHTTP(cl, val)
require.Nil(t, err)
assert.Equal(t, got, val)
val2 := randBytes(t)
got2, err := echoBytesViaHTTP(cl, val2)
require.Nil(t, err)
assert.Equal(t, got2, val2)
}
func testWithWSClient(t *testing.T, cl *client.WSClient) {
val := "acbd"
func echoViaWS(cl *client.WSClient, val string) (string, error) {
params := map[string]interface{}{
"arg": val,
}
@ -129,20 +143,62 @@ func testWithWSClient(t *testing.T, cl *client.WSClient) {
Method: "echo",
Params: params,
})
require.Nil(t, err)
if err != nil {
return "", err
}
select {
case msg := <-cl.ResultsCh:
result := new(Result)
wire.ReadJSONPtr(result, msg, &err)
require.Nil(t, err)
got := (*result).(*ResultEcho).Value
assert.Equal(t, got, val)
if err != nil {
return "", nil
}
return (*result).(*ResultEcho).Value, nil
case err := <-cl.ErrorsCh:
t.Fatal(err)
return "", err
}
}
func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
params := map[string]interface{}{
"arg": bytes,
}
err := cl.WriteJSON(types.RPCRequest{
JSONRPC: "2.0",
ID: "",
Method: "echo_bytes",
Params: params,
})
if err != nil {
return []byte{}, err
}
select {
case msg := <-cl.ResultsCh:
result := new(Result)
wire.ReadJSONPtr(result, msg, &err)
if err != nil {
return []byte{}, nil
}
return (*result).(*ResultEchoBytes).Value, nil
case err := <-cl.ErrorsCh:
return []byte{}, err
}
}
func testWithWSClient(t *testing.T, cl *client.WSClient) {
val := "acbd"
got, err := echoViaWS(cl, val)
require.Nil(t, err)
assert.Equal(t, got, val)
val2 := randBytes(t)
got2, err := echoBytesViaWS(cl, val2)
require.Nil(t, err)
assert.Equal(t, got2, val2)
}
//-------------
func TestServersAndClientsBasic(t *testing.T) {
@ -169,7 +225,7 @@ func TestHexStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr)
// should NOT be handled as hex
val := "0xabc"
got, err := echo(cl, val)
got, err := echoViaHTTP(cl, val)
require.Nil(t, err)
assert.Equal(t, got, val)
}
@ -178,35 +234,13 @@ func TestQuotedStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr)
// should NOT be unquoted
val := "\"abc\""
got, err := echo(cl, val)
got, err := echoViaHTTP(cl, val)
require.Nil(t, err)
assert.Equal(t, got, val)
}
func randBytes(t *testing.T) []byte {
n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
require.Nil(t, err)
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
}
func TestByteSliceViaJSONRPC(t *testing.T) {
cl := client.NewJSONRPCClient(unixAddr)
val := randBytes(t)
params := map[string]interface{}{
"arg": val,
}
var result Result
_, err := cl.Call("echo_bytes", params, &result)
require.Nil(t, err)
got := result.(*ResultEchoBytes).Value
assert.Equal(t, got, val)
}
func TestWSNewWSRPCFunc(t *testing.T) {
cl := client.NewWSClient(unixAddr, websocketEndpoint)
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
_, err := cl.Start()
require.Nil(t, err)
defer cl.Stop()
@ -234,3 +268,11 @@ func TestWSNewWSRPCFunc(t *testing.T) {
t.Fatal(err)
}
}
func randBytes(t *testing.T) []byte {
n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
require.Nil(t, err)
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
}

Loading…
Cancel
Save