github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/rpc/jsonrpc/types/types_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type SampleResult struct {
    14  	Value string
    15  }
    16  
    17  type responseTest struct {
    18  	id       jsonrpcid
    19  	expected string
    20  }
    21  
    22  var responseTests = []responseTest{
    23  	{JSONRPCStringID("1"), `"1"`},
    24  	{JSONRPCStringID("alphabet"), `"alphabet"`},
    25  	{JSONRPCStringID(""), `""`},
    26  	{JSONRPCStringID("àáâ"), `"àáâ"`},
    27  	{JSONRPCIntID(-1), "-1"},
    28  	{JSONRPCIntID(0), "0"},
    29  	{JSONRPCIntID(1), "1"},
    30  	{JSONRPCIntID(100), "100"},
    31  }
    32  
    33  func TestResponses(t *testing.T) {
    34  	assert := assert.New(t)
    35  	for _, tt := range responseTests {
    36  		jsonid := tt.id
    37  		a := NewRPCSuccessResponse(jsonid, &SampleResult{"hello"})
    38  		b, _ := json.Marshal(a)
    39  		s := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)
    40  		assert.Equal(s, string(b))
    41  
    42  		d := RPCParseError(errors.New("hello world"))
    43  		e, _ := json.Marshal(d)
    44  		f := `{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error. Invalid JSON","data":"hello world"}}`
    45  		assert.Equal(f, string(e))
    46  
    47  		g := RPCMethodNotFoundError(jsonid)
    48  		h, _ := json.Marshal(g)
    49  		i := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"error":{"code":-32601,"message":"Method not found"}}`, tt.expected)
    50  		assert.Equal(string(h), i)
    51  	}
    52  }
    53  
    54  func TestUnmarshallResponses(t *testing.T) {
    55  	assert := assert.New(t)
    56  	for _, tt := range responseTests {
    57  		response := &RPCResponse{}
    58  		err := json.Unmarshal(
    59  			[]byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)),
    60  			response,
    61  		)
    62  		assert.Nil(err)
    63  		a := NewRPCSuccessResponse(tt.id, &SampleResult{"hello"})
    64  		assert.Equal(*response, a)
    65  	}
    66  	response := &RPCResponse{}
    67  	err := json.Unmarshal([]byte(`{"jsonrpc":"2.0","id":true,"result":{"Value":"hello"}}`), response)
    68  	assert.NotNil(err)
    69  }
    70  
    71  func TestRPCError(t *testing.T) {
    72  	assert.Equal(t, "RPC error 12 - Badness: One worse than a code 11",
    73  		fmt.Sprintf("%v", &RPCError{
    74  			Code:    12,
    75  			Message: "Badness",
    76  			Data:    "One worse than a code 11",
    77  		}))
    78  
    79  	assert.Equal(t, "RPC error 12 - Badness",
    80  		fmt.Sprintf("%v", &RPCError{
    81  			Code:    12,
    82  			Message: "Badness",
    83  		}))
    84  }
    85  
    86  func TestRPCRequestUnmarshalJSON(t *testing.T) {
    87  	tcs := []struct {
    88  		assert   bool
    89  		expected RPCRequest
    90  	}{
    91  		{
    92  			assert:   true,
    93  			expected: RPCRequest{ID: JSONRPCIntID(1), Method: "id int", Params: json.RawMessage(`{"p1":"v1"}`)},
    94  		}, {
    95  			assert:   true,
    96  			expected: RPCRequest{ID: JSONRPCStringID("s"), Method: "id string", Params: json.RawMessage(`{"p1":"v1"}`)},
    97  		}, {
    98  			assert:   true,
    99  			expected: RPCRequest{ID: JSONRPCStringID(""), Method: "id empty", Params: json.RawMessage(`{"p1":"v1"}`)},
   100  		}, {
   101  			// can't treat nil as jsonrpcid
   102  			assert:   false,
   103  			expected: RPCRequest{ID: nil, Method: "id nil", Params: json.RawMessage(`{"p1":"v1"}`)},
   104  		}, {
   105  			assert:   true,
   106  			expected: RPCRequest{ID: JSONRPCIntID(1), Method: "params null", Params: json.RawMessage(`null`)},
   107  		}, {
   108  			// can't treat nil as json.RawMessage: the value of `nil` become "null" string
   109  			assert:   false,
   110  			expected: RPCRequest{ID: JSONRPCIntID(1), Method: "params nil", Params: nil},
   111  		},
   112  	}
   113  	for _, tc := range tcs {
   114  		data, _ := json.Marshal(tc.expected)
   115  		actual := RPCRequest{}
   116  		json.Unmarshal(data, &actual) // nolint: errcheck
   117  		assert.Equal(t, reflect.DeepEqual(tc.expected, actual), tc.assert,
   118  			"expected:", tc.expected, "actual:", actual)
   119  		actual2 := RPCRequest{}
   120  		actual2.UnmarshalJSON(data) // nolint: errcheck
   121  		assert.Equal(t, reflect.DeepEqual(tc.expected, actual2), tc.assert,
   122  			"expected:", tc.expected, "actual2:", actual2)
   123  	}
   124  }