-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always omit params member from request when empty #67
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,69 @@ package jsonrpc2_test | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"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 +102,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 +152,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I switched from |
||
} | ||
} | ||
|
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change doesn't actually have anything to do with the rest of this PR. I slipped this in because I kept seeing
jsonrpc2: protocol error: invalid character 'i' looking for beginning of value
in the test logs while working on tests. I introduced this test in 028a50b (#64) and somehow missed that this test was spamming the test logs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am only noticing now that this line is commented out. Wow, that's sloppy of me. Will fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f74a03b.