mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-18 21:20:02 +02:00
Always omit params member from request when empty
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")`.
This commit is contained in:
parent
6864d8cc6d
commit
7c2b0a0a06
9 changed files with 142 additions and 147 deletions
122
conn_test.go
122
conn_test.go
|
|
@ -2,14 +2,68 @@ package jsonrpc2_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sourcegraph/jsonrpc2"
|
||||
)
|
||||
|
||||
var paramsTests = []struct {
|
||||
sendParams interface{}
|
||||
wantParams *json.RawMessage
|
||||
}{
|
||||
{
|
||||
sendParams: nil,
|
||||
wantParams: nil,
|
||||
},
|
||||
{
|
||||
sendParams: jsonNull,
|
||||
wantParams: &jsonNull,
|
||||
},
|
||||
{
|
||||
sendParams: false,
|
||||
wantParams: rawJSONMessage("false"),
|
||||
},
|
||||
{
|
||||
sendParams: 0,
|
||||
wantParams: rawJSONMessage("0"),
|
||||
},
|
||||
{
|
||||
sendParams: "",
|
||||
wantParams: rawJSONMessage(`""`),
|
||||
},
|
||||
{
|
||||
sendParams: rawJSONMessage(`{"foo":"bar"}`),
|
||||
wantParams: rawJSONMessage(`{"foo":"bar"}`),
|
||||
},
|
||||
}
|
||||
|
||||
func TestConn_DispatchCall(t *testing.T) {
|
||||
for _, test := range paramsTests {
|
||||
t.Run(fmt.Sprintf("%s", test.sendParams), func(t *testing.T) {
|
||||
testParams(t, test.wantParams, func(c *jsonrpc2.Conn) error {
|
||||
_, err := c.DispatchCall(context.Background(), "f", test.sendParams)
|
||||
return err
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConn_Notify(t *testing.T) {
|
||||
for _, test := range paramsTests {
|
||||
t.Run(fmt.Sprintf("%s", test.sendParams), func(t *testing.T) {
|
||||
testParams(t, test.wantParams, func(c *jsonrpc2.Conn) error {
|
||||
return c.Notify(context.Background(), "f", test.sendParams)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConn_DisconnectNotify(t *testing.T) {
|
||||
|
||||
t.Run("EOF", func(t *testing.T) {
|
||||
|
|
@ -47,7 +101,16 @@ func TestConn_DisconnectNotify(t *testing.T) {
|
|||
|
||||
t.Run("protocol error", func(t *testing.T) {
|
||||
connA, connB := net.Pipe()
|
||||
c := jsonrpc2.NewConn(context.Background(), jsonrpc2.NewPlainObjectStream(connB), nil)
|
||||
c := jsonrpc2.NewConn(
|
||||
context.Background(),
|
||||
jsonrpc2.NewPlainObjectStream(connB),
|
||||
noopHandler{},
|
||||
// // Suppress log message. This connection receives an invalid JSON
|
||||
// // message that causes an error to be written to the logger. We
|
||||
// // don't want this expected error to appear in os.Stderr though when
|
||||
// // running tests in verbose mode or when other tests fail.
|
||||
// jsonrpc2.SetLogger(log.New(io.Discard, "", 0)),
|
||||
)
|
||||
connA.Write([]byte("invalid json"))
|
||||
assertDisconnect(t, c, connB)
|
||||
})
|
||||
|
|
@ -88,16 +151,69 @@ func TestConn_Close(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func testParams(t *testing.T, want *json.RawMessage, fn func(c *jsonrpc2.Conn) error) {
|
||||
wg := &sync.WaitGroup{}
|
||||
handler := handlerFunc(func(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.Request) {
|
||||
assertRawJSONMessage(t, r.Params, want)
|
||||
wg.Done()
|
||||
})
|
||||
|
||||
client, server := newClientServer(handler)
|
||||
defer client.Close()
|
||||
defer server.Close()
|
||||
|
||||
wg.Add(1)
|
||||
if err := fn(client); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func assertDisconnect(t *testing.T, c *jsonrpc2.Conn, conn io.Writer) {
|
||||
select {
|
||||
case <-c.DisconnectNotify():
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
t.Fatal("no disconnect notification")
|
||||
t.Error("no disconnect notification")
|
||||
return
|
||||
}
|
||||
// Assert that conn is closed by trying to write to it.
|
||||
_, got := conn.Write(nil)
|
||||
want := io.ErrClosedPipe
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
t.Errorf("got %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func assertRawJSONMessage(t *testing.T, got *json.RawMessage, want *json.RawMessage) {
|
||||
// Assert pointers.
|
||||
if got == nil || want == nil {
|
||||
if got != want {
|
||||
t.Errorf("pointer: got %s, want %s", got, want)
|
||||
}
|
||||
return
|
||||
}
|
||||
{
|
||||
// If pointers are not nil, then assert values.
|
||||
got := string(*got)
|
||||
want := string(*want)
|
||||
if got != want {
|
||||
t.Errorf("value: got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newClientServer(handler jsonrpc2.Handler) (client *jsonrpc2.Conn, server *jsonrpc2.Conn) {
|
||||
ctx := context.Background()
|
||||
connA, connB := net.Pipe()
|
||||
client = jsonrpc2.NewConn(
|
||||
ctx,
|
||||
jsonrpc2.NewPlainObjectStream(connA),
|
||||
noopHandler{},
|
||||
)
|
||||
server = jsonrpc2.NewConn(
|
||||
ctx,
|
||||
jsonrpc2.NewPlainObjectStream(connB),
|
||||
handler,
|
||||
)
|
||||
return client, server
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue