MCPcopy Create free account
hub / github.com/CPChain/chain / TestJSONRequestParamsParsing

Function TestJSONRequestParamsParsing

api/rpc/json_test.go:101–178  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

99}
100
101func TestJSONRequestParamsParsing(t *testing.T) {
102
103 var (
104 stringT = reflect.TypeOf("")
105 intT = reflect.TypeOf(0)
106 intPtrT = reflect.TypeOf(new(int))
107
108 stringV = reflect.ValueOf("abc")
109 i = 1
110 intV = reflect.ValueOf(i)
111 intPtrV = reflect.ValueOf(&i)
112 )
113
114 var validTests = []struct {
115 input string
116 argTypes []reflect.Type
117 expected []reflect.Value
118 }{
119 {`[]`, []reflect.Type{}, []reflect.Value{}},
120 {`[]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}},
121 {`[1]`, []reflect.Type{intT}, []reflect.Value{intV}},
122 {`[1,"abc"]`, []reflect.Type{intT, stringT}, []reflect.Value{intV, stringV}},
123 {`[null]`, []reflect.Type{intPtrT}, []reflect.Value{intPtrV}},
124 {`[null,"abc"]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}},
125 {`[null,"abc",null]`, []reflect.Type{intPtrT, stringT, intPtrT}, []reflect.Value{intPtrV, stringV, intPtrV}},
126 }
127
128 codec := jsonCodec{}
129
130 for _, test := range validTests {
131 params := (json.RawMessage)([]byte(test.input))
132 args, err := codec.ParseRequestArguments(test.argTypes, params)
133
134 if err != nil {
135 t.Fatal(err)
136 }
137
138 var match []interface{}
139 json.Unmarshal([]byte(test.input), &match)
140
141 if len(args) != len(test.argTypes) {
142 t.Fatalf("expected %d parsed args, got %d", len(test.argTypes), len(args))
143 }
144
145 for i, arg := range args {
146 expected := test.expected[i]
147
148 if arg.Kind() != expected.Kind() {
149 t.Errorf("expected type for param %d in %s", i, test.input)
150 }
151
152 if arg.Kind() == reflect.Int && arg.Int() != expected.Int() {
153 t.Errorf("expected int(%d), got int(%d) in %s", expected.Int(), arg.Int(), test.input)
154 }
155
156 if arg.Kind() == reflect.String && arg.String() != expected.String() {
157 t.Errorf("expected string(%s), got string(%s) in %s", expected.String(), arg.String(), test.input)
158 }

Callers

nothing calls this directly

Calls 3

ParseRequestArgumentsMethod · 0.95
FatalMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected