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

Function TestJSONRequestParsing

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

Source from the content-addressed store, hash-verified

34}
35
36func TestJSONRequestParsing(t *testing.T) {
37 server := NewServer()
38 service := new(Service)
39
40 if err := server.RegisterName("calc", service); err != nil {
41 t.Fatalf("%v", err)
42 }
43
44 req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`)
45 var str string
46 reply := bytes.NewBufferString(str)
47 rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))}
48
49 codec := NewJSONCodec(rw)
50
51 requests, batch, err := codec.ReadRequestHeaders()
52 if err != nil {
53 t.Fatalf("%v", err)
54 }
55
56 if batch {
57 t.Fatalf("Request isn't a batch")
58 }
59
60 if len(requests) != 1 {
61 t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests)
62 }
63
64 if requests[0].service != "calc" {
65 t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service)
66 }
67
68 if requests[0].method != "add" {
69 t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method)
70 }
71
72 if rawId, ok := requests[0].id.(*json.RawMessage); ok {
73 id, e := strconv.ParseInt(string(*rawId), 0, 64)
74 if e != nil {
75 t.Fatalf("%v", e)
76 }
77 if id != 1234 {
78 t.Fatalf("Expected id 1234 but got %d", id)
79 }
80 } else {
81 t.Fatalf("invalid request, expected *json.RawMesage got %T", requests[0].id)
82 }
83
84 var arg int
85 args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)}
86
87 v, err := codec.ParseRequestArguments(args, requests[0].params)
88 if err != nil {
89 t.Fatalf("%v", err)
90 }
91
92 if len(v) != 2 {
93 t.Fatalf("Expected 2 argument values, got %d", len(v))

Callers

nothing calls this directly

Calls 5

RegisterNameMethod · 0.95
NewServerFunction · 0.85
NewJSONCodecFunction · 0.85
ReadRequestHeadersMethod · 0.65
ParseRequestArgumentsMethod · 0.65

Tested by

no test coverage detected