@@ -90,7 +90,22 @@ func TestRawRequests(t *testing.T) {
9090 testServ := httptest .NewServer (rpcServer )
9191 defer testServ .Close ()
9292
93- tc := func (req , resp string , n int32 ) func (t * testing.T ) {
93+ removeSpaces := func (jsonStr string ) (string , error ) {
94+ var jsonObj interface {}
95+ err := json .Unmarshal ([]byte (jsonStr ), & jsonObj )
96+ if err != nil {
97+ return "" , err
98+ }
99+
100+ compactJSONBytes , err := json .Marshal (jsonObj )
101+ if err != nil {
102+ return "" , err
103+ }
104+
105+ return string (compactJSONBytes ), nil
106+ }
107+
108+ tc := func (req , resp string , n int32 , statusCode int ) func (t * testing.T ) {
94109 return func (t * testing.T ) {
95110 rpcHandler .n = 0
96111
@@ -100,16 +115,29 @@ func TestRawRequests(t *testing.T) {
100115 b , err := ioutil .ReadAll (res .Body )
101116 require .NoError (t , err )
102117
103- assert .Equal (t , resp , strings .TrimSpace (string (b )))
118+ expectedResp , err := removeSpaces (resp )
119+ require .NoError (t , err )
120+
121+ responseBody , err := removeSpaces (string (b ))
122+ require .NoError (t , err )
123+
124+ assert .Equal (t , expectedResp , responseBody )
104125 require .Equal (t , n , rpcHandler .n )
126+ require .Equal (t , statusCode , res .StatusCode )
105127 }
106128 }
107129
108- t .Run ("inc" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}` , `{"jsonrpc":"2.0","id":1}` , 1 ))
109- t .Run ("inc-null" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}` , `{"jsonrpc":"2.0","id":1}` , 1 ))
110- t .Run ("inc-noparam" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}` , `{"jsonrpc":"2.0","id":2}` , 1 ))
111- t .Run ("add" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}` , `{"jsonrpc":"2.0","id":4}` , 10 ))
112-
130+ t .Run ("inc" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}` , `{"jsonrpc":"2.0","id":1}` , 1 , 200 ))
131+ t .Run ("inc-null" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}` , `{"jsonrpc":"2.0","id":1}` , 1 , 200 ))
132+ t .Run ("inc-noparam" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}` , `{"jsonrpc":"2.0","id":2}` , 1 , 200 ))
133+ t .Run ("add" , tc (`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}` , `{"jsonrpc":"2.0","id":4}` , 10 , 200 ))
134+ // Batch requests
135+ t .Run ("add" , tc (`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 5}` , `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}` , 0 , 500 ))
136+ t .Run ("add" , tc (`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 6}]` , `[{"jsonrpc":"2.0","id":6}]` , 123 , 200 ))
137+ t .Run ("add" , tc (`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 7},{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-122], "id": 8}]` , `[{"jsonrpc":"2.0","id":7},{"jsonrpc":"2.0","id":8}]` , 1 , 200 ))
138+ t .Run ("add" , tc (`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 9},{"jsonrpc": "2.0", "params": [-122], "id": 10}]` , `[{"jsonrpc":"2.0","id":9},{"error":{"code":-32601,"message":"method '' not found"},"id":10,"jsonrpc":"2.0"}]` , 123 , 200 ))
139+ t .Run ("add" , tc (` [{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-1], "id": 11}] ` , `[{"jsonrpc":"2.0","id":11}]` , - 1 , 200 ))
140+ t .Run ("add" , tc (`` , `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}` , 0 , 400 ))
113141}
114142
115143func TestReconnection (t * testing.T ) {
0 commit comments