-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the jrpc2.ParseRequests function. (#81)
Add a new ParsedRequest type and return that instead of Request. This is a breaking change to the jrpc2.ParseRequests function. The Request is meant for consumption by Handler methods where validation has already been handled. The new type exposes validation errors, allowing the caller to detect specific problems with each request in a batch. This change also allows us to simplify checking of version errors, and to remove most of the special case version checks in the client and server. - Update the jhttp.Bridge to use the new ParsedRequest type. - Update test cases where affected. Add a testutil package for internal tests. - Fix handling of invalid requests in jhttp.Bridge (fixes #80).
- Loading branch information
1 parent
90e8e7f
commit 2377525
Showing
13 changed files
with
276 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (C) 2022 Michael J. Fromberger. All Rights Reserved. | ||
|
||
// Package testutil defines internal support code for writing tests. | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/creachadair/jrpc2" | ||
"github.com/creachadair/jrpc2/channel" | ||
) | ||
|
||
// ParseRequest parses a single JSON request object. | ||
func ParseRequest(s string) (_ *jrpc2.Request, err error) { | ||
// Check syntax. | ||
if _, err := jrpc2.ParseRequests([]byte(s)); err != nil { | ||
return nil, err | ||
} | ||
|
||
cch, sch := channel.Direct() | ||
rs := newRequestStub() | ||
srv := jrpc2.NewServer(rs, nil).Start(sch) | ||
defer func() { | ||
cch.Close() | ||
serr := srv.Wait() | ||
if err == nil { | ||
err = serr | ||
} | ||
}() | ||
if err := cch.Send([]byte(s)); err != nil { | ||
return nil, err | ||
} | ||
req := <-rs.reqc | ||
if !rs.isNote { | ||
cch.Recv() | ||
} | ||
return req, nil | ||
} | ||
|
||
// MustParseRequest calls ParseRequest and fails t if it reports an error. | ||
func MustParseRequest(t *testing.T, s string) *jrpc2.Request { | ||
t.Helper() | ||
|
||
req, err := ParseRequest(s) | ||
if err != nil { | ||
t.Fatalf("Parsing %#q failed: %v", s, err) | ||
} | ||
return req | ||
} | ||
|
||
func newRequestStub() *requestStub { | ||
return &requestStub{reqc: make(chan *jrpc2.Request, 1)} | ||
} | ||
|
||
type requestStub struct { | ||
reqc chan *jrpc2.Request | ||
isNote bool | ||
} | ||
|
||
func (r *requestStub) Assign(context.Context, string) jrpc2.Handler { return r } | ||
|
||
func (r *requestStub) Handle(_ context.Context, req *jrpc2.Request) (interface{}, error) { | ||
defer close(r.reqc) | ||
r.isNote = req.IsNotification() | ||
r.reqc <- req | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2022 Michael J. Fromberger. All Rights Reserved. | ||
|
||
package testutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/creachadair/jrpc2/internal/testutil" | ||
) | ||
|
||
func TestParseRequest(t *testing.T) { | ||
t.Run("Invalid", func(t *testing.T) { | ||
req, err := testutil.ParseRequest(`{this is invalid}`) | ||
if err == nil { | ||
t.Errorf("ParseRequest: got %+v, wanted error", req) | ||
} else { | ||
t.Logf("Invalid OK: %v", err) | ||
} | ||
}) | ||
t.Run("Call", func(t *testing.T) { | ||
req := testutil.MustParseRequest(t, `{"jsonrpc":"2.0","id":1,"method":"OK"}`) | ||
t.Logf("Call OK: %+v", req) | ||
}) | ||
t.Run("Notification", func(t *testing.T) { | ||
req := testutil.MustParseRequest(t, `{"jsonrpc":"2.0","id":null,"method":"OK"}`) | ||
t.Logf("Note OK: %+v", req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.