github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/rpc/json_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:45</date> 10 //</624342665156104192> 11 12 13 package rpc 14 15 import ( 16 "bufio" 17 "bytes" 18 "encoding/json" 19 "reflect" 20 "strconv" 21 "testing" 22 ) 23 24 type RWC struct { 25 *bufio.ReadWriter 26 } 27 28 func (rwc *RWC) Close() error { 29 return nil 30 } 31 32 func TestJSONRequestParsing(t *testing.T) { 33 server := NewServer() 34 service := new(Service) 35 36 if err := server.RegisterName("calc", service); err != nil { 37 t.Fatalf("%v", err) 38 } 39 40 req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`) 41 var str string 42 reply := bytes.NewBufferString(str) 43 rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))} 44 45 codec := NewJSONCodec(rw) 46 47 requests, batch, err := codec.ReadRequestHeaders() 48 if err != nil { 49 t.Fatalf("%v", err) 50 } 51 52 if batch { 53 t.Fatalf("Request isn't a batch") 54 } 55 56 if len(requests) != 1 { 57 t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests) 58 } 59 60 if requests[0].service != "calc" { 61 t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service) 62 } 63 64 if requests[0].method != "add" { 65 t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method) 66 } 67 68 if rawId, ok := requests[0].id.(*json.RawMessage); ok { 69 id, e := strconv.ParseInt(string(*rawId), 0, 64) 70 if e != nil { 71 t.Fatalf("%v", e) 72 } 73 if id != 1234 { 74 t.Fatalf("Expected id 1234 but got %d", id) 75 } 76 } else { 77 t.Fatalf("invalid request, expected *json.RawMesage got %T", requests[0].id) 78 } 79 80 var arg int 81 args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)} 82 83 v, err := codec.ParseRequestArguments(args, requests[0].params) 84 if err != nil { 85 t.Fatalf("%v", err) 86 } 87 88 if len(v) != 2 { 89 t.Fatalf("Expected 2 argument values, got %d", len(v)) 90 } 91 92 if v[0].Int() != 11 || v[1].Int() != 22 { 93 t.Fatalf("expected %d == 11 && %d == 22", v[0].Int(), v[1].Int()) 94 } 95 } 96 97 func TestJSONRequestParamsParsing(t *testing.T) { 98 99 var ( 100 stringT = reflect.TypeOf("") 101 intT = reflect.TypeOf(0) 102 intPtrT = reflect.TypeOf(new(int)) 103 104 stringV = reflect.ValueOf("abc") 105 i = 1 106 intV = reflect.ValueOf(i) 107 intPtrV = reflect.ValueOf(&i) 108 ) 109 110 var validTests = []struct { 111 input string 112 argTypes []reflect.Type 113 expected []reflect.Value 114 }{ 115 {`[]`, []reflect.Type{}, []reflect.Value{}}, 116 {`[]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}}, 117 {`[1]`, []reflect.Type{intT}, []reflect.Value{intV}}, 118 {`[1,"abc"]`, []reflect.Type{intT, stringT}, []reflect.Value{intV, stringV}}, 119 {`[null]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}}, 120 {`[null,"abc"]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}}, 121 {`[null,"abc",null]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}}, 122 } 123 124 codec := jsonCodec{} 125 126 for _, test := range validTests { 127 params := (json.RawMessage)([]byte(test.input)) 128 args, err := codec.ParseRequestArguments(test.argTypes, params) 129 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 var match []interface{} 135 json.Unmarshal([]byte(test.input), &match) 136 137 if len(args) != len(test.argTypes) { 138 t.Fatalf("expected %d parsed args, got %d", len(test.argTypes), len(args)) 139 } 140 141 for i, arg := range args { 142 expected := test.expected[i] 143 144 if arg.Kind() != expected.Kind() { 145 t.Errorf("expected type for param %d in %s", i, test.input) 146 } 147 148 if arg.Kind() == reflect.Int && arg.Int() != expected.Int() { 149 t.Errorf("expected int(%d), got int(%d) in %s", expected.Int(), arg.Int(), test.input) 150 } 151 152 if arg.Kind() == reflect.String && arg.String() != expected.String() { 153 t.Errorf("expected string(%s), got string(%s) in %s", expected.String(), arg.String(), test.input) 154 } 155 } 156 } 157 158 var invalidTests = []struct { 159 input string 160 argTypes []reflect.Type 161 }{ 162 {`[]`, []reflect.Type{intT}}, 163 {`[null]`, []reflect.Type{intT}}, 164 {`[1]`, []reflect.Type{stringT}}, 165 {`[1,2]`, []reflect.Type{stringT}}, 166 {`["abc", null]`, []reflect.Type{stringT, intT}}, 167 } 168 169 for i, test := range invalidTests { 170 if _, err := codec.ParseRequestArguments(test.argTypes, test.input); err == nil { 171 t.Errorf("expected test %d - %s to fail", i, test.input) 172 } 173 } 174 } 175