github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/lib/types/types_test.go (about)

     1  package rpctypes
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"fmt"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/assert"
    11  	amino "github.com/evdatsion/go-amino"
    12  )
    13  
    14  type SampleResult struct {
    15  	Value string
    16  }
    17  
    18  type responseTest struct {
    19  	id       jsonrpcid
    20  	expected string
    21  }
    22  
    23  var responseTests = []responseTest{
    24  	{JSONRPCStringID("1"), `"1"`},
    25  	{JSONRPCStringID("alphabet"), `"alphabet"`},
    26  	{JSONRPCStringID(""), `""`},
    27  	{JSONRPCStringID("àáâ"), `"àáâ"`},
    28  	{JSONRPCIntID(-1), "-1"},
    29  	{JSONRPCIntID(0), "0"},
    30  	{JSONRPCIntID(1), "1"},
    31  	{JSONRPCIntID(100), "100"},
    32  }
    33  
    34  func TestResponses(t *testing.T) {
    35  	assert := assert.New(t)
    36  	cdc := amino.NewCodec()
    37  	for _, tt := range responseTests {
    38  		jsonid := tt.id
    39  		a := NewRPCSuccessResponse(cdc, jsonid, &SampleResult{"hello"})
    40  		b, _ := json.Marshal(a)
    41  		s := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)
    42  		assert.Equal(string(s), string(b))
    43  
    44  		d := RPCParseError(jsonid, errors.New("Hello world"))
    45  		e, _ := json.Marshal(d)
    46  		f := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"error":{"code":-32700,"message":"Parse error. Invalid JSON","data":"Hello world"}}`, tt.expected)
    47  		assert.Equal(string(f), string(e))
    48  
    49  		g := RPCMethodNotFoundError(jsonid)
    50  		h, _ := json.Marshal(g)
    51  		i := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"error":{"code":-32601,"message":"Method not found"}}`, tt.expected)
    52  		assert.Equal(string(h), string(i))
    53  	}
    54  }
    55  
    56  func TestUnmarshallResponses(t *testing.T) {
    57  	assert := assert.New(t)
    58  	cdc := amino.NewCodec()
    59  	for _, tt := range responseTests {
    60  		response := &RPCResponse{}
    61  		err := json.Unmarshal([]byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)), response)
    62  		assert.Nil(err)
    63  		a := NewRPCSuccessResponse(cdc, 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  }