MCPcopy Create free account
hub / github.com/DeAI-Artist/Linkis / TestJSONRPCID

Function TestJSONRPCID

rpc/jsonrpc/server/http_json_handler_test.go:90–135  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

88}
89
90func TestJSONRPCID(t *testing.T) {
91 mux := testMux()
92 tests := []struct {
93 payload string
94 wantErr bool
95 expectedID interface{}
96 }{
97 // good id
98 {`{"jsonrpc": "2.0", "method": "c", "id": "0", "params": ["a", "10"]}`, false, types.JSONRPCStringID("0")},
99 {`{"jsonrpc": "2.0", "method": "c", "id": "abc", "params": ["a", "10"]}`, false, types.JSONRPCStringID("abc")},
100 {`{"jsonrpc": "2.0", "method": "c", "id": 0, "params": ["a", "10"]}`, false, types.JSONRPCIntID(0)},
101 {`{"jsonrpc": "2.0", "method": "c", "id": 1, "params": ["a", "10"]}`, false, types.JSONRPCIntID(1)},
102 {`{"jsonrpc": "2.0", "method": "c", "id": 1.3, "params": ["a", "10"]}`, false, types.JSONRPCIntID(1)},
103 {`{"jsonrpc": "2.0", "method": "c", "id": -1, "params": ["a", "10"]}`, false, types.JSONRPCIntID(-1)},
104
105 // bad id
106 {`{"jsonrpc": "2.0", "method": "c", "id": {}, "params": ["a", "10"]}`, true, nil},
107 {`{"jsonrpc": "2.0", "method": "c", "id": [], "params": ["a", "10"]}`, true, nil},
108 }
109
110 for i, tt := range tests {
111 req, _ := http.NewRequest("POST", "http://localhost/", strings.NewReader(tt.payload))
112 rec := httptest.NewRecorder()
113 mux.ServeHTTP(rec, req)
114 res := rec.Result()
115 // Always expecting back a JSONRPCResponse
116 assert.NotZero(t, res.StatusCode, "#%d: should always return code", i)
117 blob, err := io.ReadAll(res.Body)
118 if err != nil {
119 t.Errorf("#%d: err reading body: %v", i, err)
120 continue
121 }
122 res.Body.Close()
123
124 recv := new(types.RPCResponse)
125 err = json.Unmarshal(blob, recv)
126 assert.Nil(t, err, "#%d: expecting successful parsing of an RPCResponse:\nblob: %s", i, blob)
127 if !tt.wantErr {
128 assert.NotEqual(t, recv, new(types.RPCResponse), "#%d: not expecting a blank RPCResponse", i)
129 assert.Equal(t, tt.expectedID, recv.ID, "#%d: expected ID not matched in RPCResponse", i)
130 assert.Nil(t, recv.Error, "#%d: not expecting an error", i)
131 } else {
132 assert.True(t, recv.Error.Code < 0, "#%d: not expecting a positive JSONRPC code", i)
133 }
134 }
135}
136
137func TestRPCNotification(t *testing.T) {
138 mux := testMux()

Callers

nothing calls this directly

Calls 8

JSONRPCStringIDTypeAlias · 0.92
JSONRPCIntIDTypeAlias · 0.92
testMuxFunction · 0.85
NewReaderMethod · 0.80
CloseMethod · 0.65
EqualMethod · 0.65
ServeHTTPMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected