From f1fb9cc076d3c6c9635f94553cf063195a9574aa Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Fri, 3 Aug 2018 15:50:45 +0800 Subject: [PATCH 1/3] jsonrpc add id from request to response --- transport/http/jsonrpc/request_response_types.go | 11 +++++++++++ transport/http/jsonrpc/server.go | 1 + 2 files changed, 12 insertions(+) diff --git a/transport/http/jsonrpc/request_response_types.go b/transport/http/jsonrpc/request_response_types.go index b4752baa1..1c6630bc8 100644 --- a/transport/http/jsonrpc/request_response_types.go +++ b/transport/http/jsonrpc/request_response_types.go @@ -35,6 +35,16 @@ func (id *RequestID) UnmarshalJSON(b []byte) error { return nil } +func (id *RequestID) MarshalJSON() ([]byte, error) { + if id.intError == nil { + return json.Marshal(id.intValue) + } else if id.floatError == nil { + return json.Marshal(id.floatValue) + } else { + return json.Marshal(id.stringValue) + } +} + // Int returns the ID as an integer value. // An error is returned if the ID can't be treated as an int. func (id *RequestID) Int() (int, error) { @@ -59,6 +69,7 @@ type Response struct { JSONRPC string `json:"jsonrpc"` Result json.RawMessage `json:"result,omitempty"` Error *Error `json:"error,omitempty"` + ID *RequestID `json:"id"` } const ( diff --git a/transport/http/jsonrpc/server.go b/transport/http/jsonrpc/server.go index 1b49fe7b5..0310f3edd 100644 --- a/transport/http/jsonrpc/server.go +++ b/transport/http/jsonrpc/server.go @@ -136,6 +136,7 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } res := Response{ + ID: req.ID, JSONRPC: Version, } From 91cb6bfda00e17207c7fa3c22ba013c76d313aab Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Fri, 3 Aug 2018 17:05:16 +0800 Subject: [PATCH 2/3] add test for marshal id --- .../jsonrpc/request_response_types_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/transport/http/jsonrpc/request_response_types_test.go b/transport/http/jsonrpc/request_response_types_test.go index 4f4abf3d6..9006534cd 100644 --- a/transport/http/jsonrpc/request_response_types_test.go +++ b/transport/http/jsonrpc/request_response_types_test.go @@ -109,3 +109,43 @@ func TestCanUnmarshalNullID(t *testing.T) { t.Fatalf("Expected ID to be nil, got %+v.\n", r.ID) } } + +func TestCanMarshalID(t *testing.T) { + cases := []struct { + JSON string + expType string + expValue interface{} + }{ + {`12345`, "int", 12345}, + {`12345.6`, "float", 12345.6}, + {`"stringaling"`, "string", "stringaling"}, + } + + for _, c := range cases { + req := jsonrpc.Request{} + JSON := fmt.Sprintf(`{"jsonrpc":"2.0","id":%s}`, c.JSON) + json.Unmarshal([]byte(JSON), &req) + resp := jsonrpc.Response{ID: req.ID, JSONRPC: req.JSONRPC} + + want := JSON + bol, _ := json.Marshal(resp) + got := string(bol) + if got != want { + t.Fatalf("'%s': want %s, got %s.", c.expType, want, got) + } + } +} + +func TestCanMarshalNullID(t *testing.T) { + req := jsonrpc.Request{} + JSON := `{"jsonrpc":"2.0","id":null}` + json.Unmarshal([]byte(JSON), &req) + resp := jsonrpc.Response{ID: req.ID, JSONRPC: req.JSONRPC} + + want := JSON + bol, _ := json.Marshal(resp) + got := string(bol) + if got != want { + t.Fatalf("'null': want %s, got %s.", want, got) + } +} From cbf741b2122e6b682ccb8c6dcbb0e4dd78b38fd3 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Sat, 4 Aug 2018 00:00:54 +0800 Subject: [PATCH 3/3] organize null test --- .../http/jsonrpc/request_response_types_test.go | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/transport/http/jsonrpc/request_response_types_test.go b/transport/http/jsonrpc/request_response_types_test.go index 9006534cd..8eb03ee93 100644 --- a/transport/http/jsonrpc/request_response_types_test.go +++ b/transport/http/jsonrpc/request_response_types_test.go @@ -119,6 +119,7 @@ func TestCanMarshalID(t *testing.T) { {`12345`, "int", 12345}, {`12345.6`, "float", 12345.6}, {`"stringaling"`, "string", "stringaling"}, + {`null`, "null", nil}, } for _, c := range cases { @@ -135,17 +136,3 @@ func TestCanMarshalID(t *testing.T) { } } } - -func TestCanMarshalNullID(t *testing.T) { - req := jsonrpc.Request{} - JSON := `{"jsonrpc":"2.0","id":null}` - json.Unmarshal([]byte(JSON), &req) - resp := jsonrpc.Response{ID: req.ID, JSONRPC: req.JSONRPC} - - want := JSON - bol, _ := json.Marshal(resp) - got := string(bol) - if got != want { - t.Fatalf("'null': want %s, got %s.", want, got) - } -}