1
0
Fork 0
mirror of https://github.com/sourcegraph/jsonrpc2.git synced 2026-06-16 04:04:56 +02:00

Split jsonrpc2.go file into multiple files (#65)

This merge request moves some of the contents from the jsonrpc2.go file
into their own designated file. The new files being introduced
(excluding test files) are as follows:

* conn.go
* request.go
* response.go

The motive of this change is to make it easier to navigate the code.
Without this change, the jsonrpc2.go file is 813 lines of code.
This commit is contained in:
Sam Herrmann 2023-02-09 01:56:42 -05:00 committed by GitHub
parent 028a50bb39
commit 846c29e96d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 1028 additions and 978 deletions

65
request_test.go Normal file
View file

@ -0,0 +1,65 @@
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) {
null := json.RawMessage("null")
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: &null},
},
{
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)
}
}
}