Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions v2/json2/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"errors"
"net/http"
"strings"
"testing"

"github.com/gorilla/rpc/v2"
Expand Down Expand Up @@ -136,6 +137,16 @@ func executeRaw(t *testing.T, s *rpc.Server, req interface{}, res interface{}) e
return DecodeClientResponse(w.Body, res)
}

func executeInvalidJSON(t *testing.T, s *rpc.Server, res interface{}) error {
r, _ := http.NewRequest("POST", "http://localhost:8080/", strings.NewReader(`not even a json`))
r.Header.Set("Content-Type", "application/json")

w := NewRecorder()
s.ServeHTTP(w, r)

return DecodeClientResponse(w.Body, res)
}

func TestService(t *testing.T) {
s := rpc.NewServer()
s.RegisterCodec(NewCodec(), "application/json")
Expand Down Expand Up @@ -182,6 +193,15 @@ func TestService(t *testing.T) {
if res.Result != Service1DefaultResponse {
t.Errorf("Wrong response: got %v, want %v", res.Result, Service1DefaultResponse)
}

res = Service1Response{}
if err := executeInvalidJSON(t, s, &res); err == nil {
t.Error("Expected to receive an E_PARSE error, but got nil")
} else if jsonRpcErr, ok := err.(*Error); !ok {
t.Errorf("Expected to receive an Error, but got %T: %s", err, err)
} else if jsonRpcErr.Code != E_PARSE {
t.Errorf("Expected to receive an E_PARSE JSON-RPC error (%d) but got %d", E_PARSE, jsonRpcErr.Code)
}
}

func TestDecodeNullResult(t *testing.T) {
Expand Down
14 changes: 10 additions & 4 deletions v2/json2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,21 @@ func newCodecRequest(r *http.Request, encoder rpc.Encoder) rpc.CodecRequest {
// Decode the request body and check if RPC method is valid.
req := new(serverRequest)
err := json.NewDecoder(r.Body).Decode(req)

if err != nil {
err = &Error{
Code: E_PARSE,
Message: err.Error(),
Data: req,
}
}
if req.Version != Version {
} else if req.Version != Version {
err = &Error{
Code: E_INVALID_REQ,
Message: "jsonrpc must be " + Version,
Data: req,
}
}

r.Body.Close()
return &CodecRequest{request: req, err: err, encoder: encoder}
}
Expand Down Expand Up @@ -185,8 +186,9 @@ func (c *CodecRequest) WriteError(w http.ResponseWriter, status int, err error)
}

func (c *CodecRequest) writeServerResponse(w http.ResponseWriter, res *serverResponse) {
// Id is null for notifications and they don't have a response.
if c.request.Id != nil {
// Id is null for notifications and they don't have a response, unless we couldn't even parse the JSON, in that
// case we can't know whether it was intended to be a notification
if c.request.Id != nil || isParseErrorResponse(res) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
encoder := json.NewEncoder(c.encoder.Encode(w))
err := encoder.Encode(res)
Expand All @@ -198,5 +200,9 @@ func (c *CodecRequest) writeServerResponse(w http.ResponseWriter, res *serverRes
}
}

func isParseErrorResponse(res *serverResponse) bool {
return res != nil && res.Error != nil && res.Error.Code == E_PARSE
}

type EmptyResponse struct {
}