Skip to content

Commit 3aa155d

Browse files
committed
Merge branch 'artnim-batchresult'
2 parents 9cea646 + 09c18de commit 3aa155d

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

AustinHarris.JsonRpcTest/UnitTest1.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Security.Cryptography.X509Certificates;
3+
using System.Text.RegularExpressions;
4+
using System.Threading.Tasks;
25
using AustinHarris.JsonRpc;
36
using Microsoft.VisualStudio.TestTools.UnitTesting;
47

@@ -1335,15 +1338,37 @@ public void TestOptionalParametersBoolsAndStrings()
13351338
}
13361339

13371340
[TestMethod]
1338-
public void TestBatchResult()
1341+
public void TestBatchResultWrongRequests()
1342+
{
1343+
string request = @"[{},{""jsonrpc"":""2.0"",""id"":4}]";
1344+
1345+
var result = JsonRpcProcessor.Process(request);
1346+
result.Wait();
1347+
1348+
Assert.IsTrue(Regex.IsMatch(result.Result, @"\[(\{.*""error"":.*?,""id"":.*?\}),(\{.*""error"":.*?,""id"":.*?\})\]"), "Should have two errors.");
1349+
}
1350+
1351+
[TestMethod]
1352+
public void TestBatchResultMultipleMethodCallsNotificationAtLast()
13391353
{
13401354
string request =
1341-
@"[{},{""jsonrpc"":""2.0"",""id"":4},{""jsonrpc"":""2.0"",""method"":""ReturnsDateTime"",""params"":{},""id"":1},{""jsonrpc"":""2.0"",""method"":""Notify"",""params"":[""Hello World!""]}]";
1355+
@"[{""jsonrpc"":""2.0"",""method"":""ReturnsDateTime"",""params"":{},""id"":1},{""jsonrpc"":""2.0"",""method"":""Notify"",""params"":[""Hello World!""]}]";
13421356

13431357
var result = JsonRpcProcessor.Process(request);
13441358
result.Wait();
13451359

13461360
Assert.IsFalse(result.Result.EndsWith(@",]"), "result.Result.EndsWith(@',]')");
1361+
1362+
}
1363+
1364+
[TestMethod]
1365+
public void TestEmptyBatchResult()
1366+
{
1367+
var secondRequest = @"{""jsonrpc"":""2.0"",""method"":""Notify"",""params"":[""Hello World!""]}";
1368+
var result = JsonRpcProcessor.Process(secondRequest);
1369+
result.Wait();
1370+
1371+
Assert.IsTrue(string.IsNullOrEmpty(result.Result));
13471372
}
13481373

13491374
[TestMethod]
@@ -1357,5 +1382,26 @@ public void TestLeftOutParams()
13571382

13581383
Assert.IsFalse(result.Result.Contains(@"error"":{""code"":-32602"), @"According to JSON-RPC 2.0 the ""params"" member MAY be omitted.");
13591384
}
1385+
1386+
[TestMethod]
1387+
public void TestMultipleResults()
1388+
{
1389+
var result =
1390+
JsonRpcProcessor.Process(
1391+
@"[{""jsonrpc"":""2.0"",""method"":""ReturnsDateTime"",""params"":{},""id"":1},{""jsonrpc"":""2.0"",""method"":""ReturnsDateTime"",""params"":{},""id"":1}]");
1392+
result.Wait();
1393+
1394+
Assert.IsTrue(result.Result.EndsWith("]"));
1395+
}
1396+
1397+
[TestMethod]
1398+
public void TestSingleResultBatch()
1399+
{
1400+
var result =
1401+
JsonRpcProcessor.Process(@"[{""jsonrpc"":""2.0"",""method"":""ReturnsDateTime"",""params"":{},""id"":1}]");
1402+
result.Wait();
1403+
Assert.IsFalse(result.Result.EndsWith("]"));
1404+
}
13601405
}
13611406
}
1407+

Json-Rpc/JsonRpcProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static string ProcessInternal(string sessionId, string jsonRpc, object j
121121
responses[idx++] = JsonConvert.SerializeObject(resp.Item2);
122122
}
123123

124-
return responses.Length == 1 ? responses[0] : string.Format("[{0}]", string.Join(",", responses));
124+
return responses.Length == 0 ? string.Empty : responses.Length == 1 ? responses[0] : string.Format("[{0}]", string.Join(",", responses));
125125
}
126126
catch (Exception ex)
127127
{

0 commit comments

Comments
 (0)