jsonrpc_test.go raw
1 package btcjson_test
2
3 import (
4 "encoding/json"
5 "reflect"
6 "testing"
7
8 "github.com/p9c/p9/pkg/btcjson"
9 )
10
11 // TestIsValidIDType ensures the IsValidIDType function behaves as expected.
12 func TestIsValidIDType(t *testing.T) {
13 t.Parallel()
14 tests := []struct {
15 name string
16 id interface{}
17 isValid bool
18 }{
19 {"int", 1, true},
20 {"int8", int8(1), true},
21 {"int16", int16(1), true},
22 {"int32", int32(1), true},
23 {"int64", int64(1), true},
24 {"uint", uint(1), true},
25 {"uint8", uint8(1), true},
26 {"uint16", uint16(1), true},
27 {"uint32", uint32(1), true},
28 {"uint64", uint64(1), true},
29 {"string", "1", true},
30 {"nil", nil, true},
31 {"float32", float32(1), true},
32 {"float64", float64(1), true},
33 {"bool", true, false},
34 {"chan int", make(chan int), false},
35 {"complex64", complex64(1), false},
36 {"complex128", complex128(1), false},
37 {"func", func() {
38 }, false,
39 },
40 }
41 t.Logf("Running %d tests", len(tests))
42 for i, test := range tests {
43 if btcjson.IsValidIDType(test.id) != test.isValid {
44 t.Errorf("Test #%d (%s) valid mismatch - got %v, "+
45 "want %v", i, test.name, !test.isValid,
46 test.isValid,
47 )
48 continue
49 }
50 }
51 }
52
53 // TestMarshalResponse ensures the MarshalResponse function works as expected.
54 func TestMarshalResponse(t *testing.T) {
55 t.Parallel()
56 testID := 1
57 tests := []struct {
58 name string
59 result interface{}
60 jsonErr *btcjson.RPCError
61 expected []byte
62 }{
63 {
64 name: "ordinary bool result with no error",
65 result: true,
66 jsonErr: nil,
67 expected: []byte(`{"result":true,"error":null,"id":1}`),
68 },
69 {
70 name: "result with error",
71 result: nil,
72 jsonErr: func() *btcjson.RPCError {
73 return btcjson.NewRPCError(btcjson.ErrRPCBlockNotFound, "123 not found")
74 }(),
75 expected: []byte(`{"result":null,"error":{"code":-5,"message":"123 not found"},"id":1}`),
76 },
77 }
78 t.Logf("Running %d tests", len(tests))
79 for i, test := range tests {
80 _, _ = i, test
81 marshalled, e := btcjson.MarshalResponse(testID, test.result, test.jsonErr)
82 if e != nil {
83 t.Errorf("Test #%d (%s) unexpected error: %v", i,
84 test.name, e,
85 )
86 continue
87 }
88 if !reflect.DeepEqual(marshalled, test.expected) {
89 t.Errorf("Test #%d (%s) mismatched result - got %s, "+
90 "want %s", i, test.name, marshalled,
91 test.expected,
92 )
93 }
94 }
95 }
96
97 // TestMiscErrors tests a few error conditions not covered elsewhere.
98 func TestMiscErrors(t *testing.T) {
99 t.Parallel()
100 // Force an error in NewRequest by giving it a parameter type that is not supported.
101 _, e := btcjson.NewRequest(nil, "test", []interface{}{make(chan int)})
102 if e == nil {
103 t.Error("NewRequest: did not receive error")
104 return
105 }
106 // Force an error in MarshalResponse by giving it an id type that is not supported.
107 wantErr := btcjson.GeneralError{ErrorCode: btcjson.ErrInvalidType}
108 _, e = btcjson.MarshalResponse(make(chan int), nil, nil)
109 if jerr, ok := e.(btcjson.GeneralError); !ok || jerr.ErrorCode != wantErr.ErrorCode {
110 t.Errorf("MarshalResult: did not receive expected error - got "+
111 "%v (%[1]T), want %v (%[2]T)", e, wantErr,
112 )
113 return
114 }
115 // Force an error in MarshalResponse by giving it a result type that can't be marshalled.
116 _, e = btcjson.MarshalResponse(1, make(chan int), nil)
117 if _, ok := e.(*json.UnsupportedTypeError); !ok {
118 wantErr := &json.UnsupportedTypeError{}
119 t.Errorf("MarshalResult: did not receive expected error - got "+
120 "%v (%[1]T), want %T", e, wantErr,
121 )
122 return
123 }
124 }
125
126 // TestRPCError tests the error output for the RPCError type.
127 func TestRPCError(t *testing.T) {
128 t.Parallel()
129 tests := []struct {
130 in *btcjson.RPCError
131 want string
132 }{
133 {btcjson.ErrRPCInvalidRequest, "-32600: Invalid request"},
134 {btcjson.ErrRPCMethodNotFound, "-32601: Method not found"},
135 }
136 t.Logf("Running %d tests", len(tests))
137 for i, test := range tests {
138 result := test.in.Error()
139 if result != test.want {
140 t.Errorf("BTCJSONError #%d\n got: %s want: %s", i, result,
141 test.want,
142 )
143 continue
144 }
145 }
146 }
147