mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-16 04:04:56 +02:00
With this commit, the JSON encoding of `Request` always omits the params
member when calling `Conn.Call`, `Conn.DispatchCall`, or `Conn.Notify`
with the `params` argument set to `nil`. This change also removes the
`OmitNilParams` call option that was added in commit 8012d496 (#62).
As of this commit, if users desire to send a JSON-RPC request with a
`params` value of `null`, then they may do so by explicitly setting the
`params` argument of `Conn.Call`/`Conn.DispatchCall`/`Conn.Notify` to
`json.RawMessage("null")`.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package jsonrpc2_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/sourcegraph/jsonrpc2"
|
|
)
|
|
|
|
func TestRequest_MarshalJSON_jsonrpc(t *testing.T) {
|
|
b, err := json.Marshal(&jsonrpc2.Request{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := `{"id":0,"jsonrpc":"2.0","method":""}`; string(b) != want {
|
|
t.Errorf("got %q, want %q", b, want)
|
|
}
|
|
}
|
|
|
|
func TestRequest_MarshalUnmarshalJSON(t *testing.T) {
|
|
obj := json.RawMessage(`{"foo":"bar"}`)
|
|
tests := []struct {
|
|
data []byte
|
|
want jsonrpc2.Request
|
|
}{
|
|
{
|
|
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","params":{"foo":"bar"}}`),
|
|
want: jsonrpc2.Request{ID: jsonrpc2.ID{Num: 123}, Method: "m", Params: &obj},
|
|
},
|
|
{
|
|
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","params":null}`),
|
|
want: jsonrpc2.Request{ID: jsonrpc2.ID{Num: 123}, Method: "m", Params: &jsonNull},
|
|
},
|
|
{
|
|
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m"}`),
|
|
want: jsonrpc2.Request{ID: jsonrpc2.ID{Num: 123}, Method: "m", Params: nil},
|
|
},
|
|
{
|
|
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","sessionId":"session"}`),
|
|
want: jsonrpc2.Request{ID: jsonrpc2.ID{Num: 123}, Method: "m", Params: nil, ExtraFields: []jsonrpc2.RequestField{{Name: "sessionId", Value: "session"}}},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
var got jsonrpc2.Request
|
|
if err := json.Unmarshal(test.data, &got); err != nil {
|
|
t.Error(err)
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
t.Errorf("%q: got %+v, want %+v", test.data, got, test.want)
|
|
continue
|
|
}
|
|
data, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Error(err)
|
|
continue
|
|
}
|
|
if !bytes.Equal(data, test.data) {
|
|
t.Errorf("got JSON %q, want %q", data, test.data)
|
|
}
|
|
}
|
|
}
|