json_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "bufio"
  19. "bytes"
  20. "encoding/json"
  21. "reflect"
  22. "strconv"
  23. "testing"
  24. )
  25. type RWC struct {
  26. *bufio.ReadWriter
  27. }
  28. func (rwc *RWC) Close() error {
  29. return nil
  30. }
  31. func TestJSONRequestParsing(t *testing.T) {
  32. server := NewServer()
  33. service := new(Service)
  34. if err := server.RegisterName("calc", service); err != nil {
  35. t.Fatalf("%v", err)
  36. }
  37. req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`)
  38. var str string
  39. reply := bytes.NewBufferString(str)
  40. rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))}
  41. codec := NewJSONCodec(rw)
  42. requests, batch, err := codec.ReadRequestHeaders()
  43. if err != nil {
  44. t.Fatalf("%v", err)
  45. }
  46. if batch {
  47. t.Fatalf("Request isn't a batch")
  48. }
  49. if len(requests) != 1 {
  50. t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests)
  51. }
  52. if requests[0].service != "calc" {
  53. t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service)
  54. }
  55. if requests[0].method != "add" {
  56. t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method)
  57. }
  58. if rawId, ok := requests[0].id.(*json.RawMessage); ok {
  59. id, e := strconv.ParseInt(string(*rawId), 0, 64)
  60. if e != nil {
  61. t.Fatalf("%v", e)
  62. }
  63. if id != 1234 {
  64. t.Fatalf("Expected id 1234 but got %d", id)
  65. }
  66. } else {
  67. t.Fatalf("invalid request, expected *json.RawMesage got %T", requests[0].id)
  68. }
  69. var arg int
  70. args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)}
  71. v, err := codec.ParseRequestArguments(args, requests[0].params)
  72. if err != nil {
  73. t.Fatalf("%v", err)
  74. }
  75. if len(v) != 2 {
  76. t.Fatalf("Expected 2 argument values, got %d", len(v))
  77. }
  78. if v[0].Int() != 11 || v[1].Int() != 22 {
  79. t.Fatalf("expected %d == 11 && %d == 22", v[0].Int(), v[1].Int())
  80. }
  81. }
  82. func TestJSONRequestParamsParsing(t *testing.T) {
  83. var (
  84. stringT = reflect.TypeOf("")
  85. intT = reflect.TypeOf(0)
  86. intPtrT = reflect.TypeOf(new(int))
  87. stringV = reflect.ValueOf("abc")
  88. i = 1
  89. intV = reflect.ValueOf(i)
  90. intPtrV = reflect.ValueOf(&i)
  91. )
  92. var validTests = []struct {
  93. input string
  94. argTypes []reflect.Type
  95. expected []reflect.Value
  96. }{
  97. {`[]`, []reflect.Type{}, []reflect.Value{}},
  98. {`[]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}},
  99. {`[1]`, []reflect.Type{intT}, []reflect.Value{intV}},
  100. {`[1,"abc"]`, []reflect.Type{intT, stringT}, []reflect.Value{intV, stringV}},
  101. {`[null]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}},
  102. {`[null,"abc"]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}},
  103. {`[null,"abc",null]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}},
  104. }
  105. codec := jsonCodec{}
  106. for _, test := range validTests {
  107. params := (json.RawMessage)([]byte(test.input))
  108. args, err := codec.ParseRequestArguments(test.argTypes, params)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. var match []interface{}
  113. json.Unmarshal([]byte(test.input), &match)
  114. if len(args) != len(test.argTypes) {
  115. t.Fatalf("expected %d parsed args, got %d", len(test.argTypes), len(args))
  116. }
  117. for i, arg := range args {
  118. expected := test.expected[i]
  119. if arg.Kind() != expected.Kind() {
  120. t.Errorf("expected type for param %d in %s", i, test.input)
  121. }
  122. if arg.Kind() == reflect.Int && arg.Int() != expected.Int() {
  123. t.Errorf("expected int(%d), got int(%d) in %s", expected.Int(), arg.Int(), test.input)
  124. }
  125. if arg.Kind() == reflect.String && arg.String() != expected.String() {
  126. t.Errorf("expected string(%s), got string(%s) in %s", expected.String(), arg.String(), test.input)
  127. }
  128. }
  129. }
  130. var invalidTests = []struct {
  131. input string
  132. argTypes []reflect.Type
  133. }{
  134. {`[]`, []reflect.Type{intT}},
  135. {`[null]`, []reflect.Type{intT}},
  136. {`[1]`, []reflect.Type{stringT}},
  137. {`[1,2]`, []reflect.Type{stringT}},
  138. {`["abc", null]`, []reflect.Type{stringT, intT}},
  139. }
  140. for i, test := range invalidTests {
  141. if _, err := codec.ParseRequestArguments(test.argTypes, test.input); err == nil {
  142. t.Errorf("expected test %d - %s to fail", i, test.input)
  143. }
  144. }
  145. }