2017-09-14 20:14:31 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSuccessResponse(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
result json.RawMessage
|
|
|
|
id json.RawMessage
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"string", json.RawMessage(`"3434=done"`), nil, `{"jsonrpc":"2.0","id":0,"result":"3434=done"}`},
|
|
|
|
{"struct_nil_id", json.RawMessage(`{"field": "value"}`), nil, `{"jsonrpc":"2.0","id":0,"result":{"field":"value"}}`},
|
|
|
|
{"struct_non_nil_id", json.RawMessage(`{"field": "value"}`), json.RawMessage(`42`), `{"jsonrpc":"2.0","id":42,"result":{"field":"value"}}`},
|
2017-09-26 10:19:17 +00:00
|
|
|
{"null", json.RawMessage(`null`), json.RawMessage(`7`), `{"jsonrpc":"2.0","id":7,"result":null}`},
|
|
|
|
{"null_nil", nil, json.RawMessage(`7`), `{"jsonrpc":"2.0","id":7,"result":null}`},
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
got := newSuccessResponse(test.result, test.id)
|
|
|
|
require.Equal(t, test.expected, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewErrorResponse(t *testing.T) {
|
|
|
|
got := newErrorResponse(-32601, errors.New("Method not found"), json.RawMessage(`42`))
|
|
|
|
|
|
|
|
expected := `{"jsonrpc":"2.0","id":42,"error":{"code":-32601,"message":"Method not found"}}`
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalMessage(t *testing.T) {
|
2017-09-18 14:24:35 +00:00
|
|
|
body := json.RawMessage(`{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}}`)
|
2017-09-14 20:14:31 +00:00
|
|
|
got, err := unmarshalMessage(body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &jsonrpcMessage{
|
|
|
|
Version: "2.0",
|
|
|
|
Method: "subtract",
|
|
|
|
Params: json.RawMessage(`{"subtrahend": 23, "minuend": 42}`),
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodAndParamsFromBody(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-09-17 14:06:18 +00:00
|
|
|
name string
|
2017-09-18 14:24:35 +00:00
|
|
|
body json.RawMessage
|
2017-09-17 14:06:18 +00:00
|
|
|
params []interface{}
|
|
|
|
method string
|
|
|
|
id json.RawMessage
|
|
|
|
shouldFail bool
|
2017-09-14 20:14:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"params_array",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`{"jsonrpc": "2.0", "id": 42, "method": "subtract", "params": [{"subtrahend": 23, "minuend": 42}]}`),
|
2017-09-14 20:14:31 +00:00
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"subtrahend": float64(23),
|
|
|
|
"minuend": float64(42),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"subtract",
|
|
|
|
json.RawMessage(`42`),
|
2017-09-17 14:06:18 +00:00
|
|
|
false,
|
2017-09-14 20:14:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"params_empty_array",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`{"jsonrpc": "2.0", "method": "test", "params": []}`),
|
2017-09-14 20:14:31 +00:00
|
|
|
[]interface{}{},
|
|
|
|
"test",
|
2017-09-17 14:06:18 +00:00
|
|
|
nil,
|
|
|
|
false,
|
2017-09-14 20:14:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"params_none",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`{"jsonrpc": "2.0", "method": "test"}`),
|
2017-09-14 20:14:31 +00:00
|
|
|
[]interface{}{},
|
|
|
|
"test",
|
2017-09-17 14:06:18 +00:00
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"getFilterMessage",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`{"jsonrpc":"2.0","id":44,"method":"shh_getFilterMessages","params":["3de6a8867aeb75be74d68478b853b4b0e063704d30f8231c45d0fcbd97af207e"]}`),
|
2017-09-17 14:06:18 +00:00
|
|
|
[]interface{}{string("3de6a8867aeb75be74d68478b853b4b0e063704d30f8231c45d0fcbd97af207e")},
|
|
|
|
"shh_getFilterMessages",
|
|
|
|
json.RawMessage(`44`),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"getFilterMessage_array",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`[{"jsonrpc":"2.0","id":44,"method":"shh_getFilterMessages","params":["3de6a8867aeb75be74d68478b853b4b0e063704d30f8231c45d0fcbd97af207e"]}]`),
|
|
|
|
[]interface{}{},
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
true,
|
2017-09-17 14:06:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_array",
|
2017-09-18 14:24:35 +00:00
|
|
|
json.RawMessage(`[]`),
|
2017-09-17 14:06:18 +00:00
|
|
|
[]interface{}{},
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
true,
|
2017-09-14 20:14:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
method, params, id, err := methodAndParamsFromBody(test.body)
|
2017-09-17 14:06:18 +00:00
|
|
|
if test.shouldFail {
|
|
|
|
require.Error(t, err)
|
2017-09-18 14:24:35 +00:00
|
|
|
return
|
2017-09-17 14:06:18 +00:00
|
|
|
}
|
2017-09-14 20:14:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.method, method)
|
|
|
|
require.Equal(t, test.params, params)
|
2017-09-17 14:06:18 +00:00
|
|
|
require.EqualValues(t, test.id, id)
|
2017-09-14 20:14:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 14:24:35 +00:00
|
|
|
|
|
|
|
func TestIsBatch(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
body json.RawMessage
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"single", json.RawMessage(`{"jsonrpc":"2.0","id":44,"method":"shh_getFilterMessages","params":["3de6a8867aeb75be74d68478b853b4b0e063704d30f8231c45d0fcbd97af207e"]}`), false},
|
|
|
|
{"array", json.RawMessage(`[{"jsonrpc":"2.0","id":44,"method":"shh_getFilterMessages","params":["3de6a8867aeb75be74d68478b853b4b0e063704d30f8231c45d0fcbd97af207e"]}]`), true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
got := isBatch(test.body)
|
|
|
|
require.Equal(t, test.expected, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|