mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-18 13:10:02 +02:00
better null handling in (Request).params and (Response).result (#7)
This makes the MarshalJSON/UnmarshalJSON methods centralize the logic, instead of requiring callers to occasionally munge responses to make them valid to later JSON-marshal.
This commit is contained in:
parent
6e88a787fb
commit
cffa092597
2 changed files with 114 additions and 27 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package jsonrpc2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
|
@ -35,32 +36,93 @@ func TestAnyMessage(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMessageCodec(t *testing.T) {
|
||||
func TestRequest_MarshalUnmarshalJSON(t *testing.T) {
|
||||
null := json.RawMessage("null")
|
||||
obj := json.RawMessage(`{"foo":"bar"}`)
|
||||
tests := []struct {
|
||||
v, vempty interface{}
|
||||
data []byte
|
||||
want Request
|
||||
}{
|
||||
{
|
||||
v: &Request{ID: ID{Num: 123}},
|
||||
vempty: &Request{ID: ID{Num: 123}},
|
||||
data: []byte(`{"method":"m","params":{"foo":"bar"},"id":123,"jsonrpc":"2.0"}`),
|
||||
want: Request{ID: ID{Num: 123}, Method: "m", Params: &obj},
|
||||
},
|
||||
{
|
||||
v: &Response{ID: ID{Num: 123}, Result: &obj},
|
||||
vempty: &Response{ID: ID{Num: 123}, Result: &obj},
|
||||
data: []byte(`{"method":"m","params":null,"id":123,"jsonrpc":"2.0"}`),
|
||||
want: Request{ID: ID{Num: 123}, Method: "m", Params: &null},
|
||||
},
|
||||
{
|
||||
data: []byte(`{"method":"m","id":123,"jsonrpc":"2.0"}`),
|
||||
want: Request{ID: ID{Num: 123}, Method: "m", Params: nil},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
b, err := json.Marshal(test.v)
|
||||
var got 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.Fatal(err)
|
||||
t.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(b, test.vempty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(test.vempty, test.v) {
|
||||
t.Errorf("got %+v, want %+v", test.vempty, test.v)
|
||||
if !bytes.Equal(data, test.data) {
|
||||
t.Errorf("got JSON %q, want %q", data, test.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponse_MarshalUnmarshalJSON(t *testing.T) {
|
||||
null := json.RawMessage("null")
|
||||
obj := json.RawMessage(`{"foo":"bar"}`)
|
||||
tests := []struct {
|
||||
data []byte
|
||||
want Response
|
||||
error bool
|
||||
}{
|
||||
{
|
||||
data: []byte(`{"id":123,"result":{"foo":"bar"},"jsonrpc":"2.0"}`),
|
||||
want: Response{ID: ID{Num: 123}, Result: &obj},
|
||||
},
|
||||
{
|
||||
data: []byte(`{"id":123,"result":null,"jsonrpc":"2.0"}`),
|
||||
want: Response{ID: ID{Num: 123}, Result: &null},
|
||||
},
|
||||
{
|
||||
data: []byte(`{"id":123,"jsonrpc":"2.0"}`),
|
||||
want: Response{ID: ID{Num: 123}, Result: nil},
|
||||
error: true, // either result or error field must be set
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
var got Response
|
||||
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 {
|
||||
if test.error {
|
||||
continue
|
||||
}
|
||||
t.Error(err)
|
||||
continue
|
||||
}
|
||||
if test.error {
|
||||
t.Errorf("%q: expected error", test.data)
|
||||
continue
|
||||
}
|
||||
if !bytes.Equal(data, test.data) {
|
||||
t.Errorf("got JSON %q, want %q", data, test.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue