Skip to content

Commit 51682b5

Browse files
committed
Added Tests for BatchClient
1 parent 80ce5c6 commit 51682b5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

test/batchclient.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "catch/catch.hpp"
2+
#include "testclientconnector.hpp"
23
#include <iostream>
34
#include <jsonrpccxx/batchclient.hpp>
45

@@ -8,7 +9,7 @@ using namespace std;
89
using namespace jsonrpccxx;
910
using namespace Catch::Matchers;
1011

11-
TEST_CASE("batchresponse_parsing", TEST_MODULE) {
12+
TEST_CASE("batchresponse", TEST_MODULE) {
1213
BatchResponse br({{{"jsonrpc", "2.0"}, {"id", "1"}, {"result", "someresultstring"}},
1314
{{"jsonrpc", "2.0"}, {"id", "2"}, {"result", 33}},
1415
{{"jsonrpc", "2.0"}, {"id", "3"}, {"error", {{"code", -111}, {"message", "the error message"}}}},
@@ -28,4 +29,43 @@ TEST_CASE("batchresponse_parsing", TEST_MODULE) {
2829
CHECK(br.GetResponse().size() == 5);
2930
CHECK(br.GetResponse()[br.GetInvalidIndexes()[0]]["error"]["code"] == -112);
3031
CHECK(br.GetResponse()[br.GetInvalidIndexes()[1]] == 3);
32+
}
33+
34+
TEST_CASE("batchrequest", TEST_MODULE) {
35+
BatchRequest br;
36+
TestClientConnector c;
37+
json request = br.AddMethodCall(1, "some_method1", {"value1"})
38+
.AddNamedMethodCall(2, "some_method2", {{"param1", "value1"}})
39+
.AddNotificationCall("some_notification1", {"value2"})
40+
.AddNamedNotificationCall("some_notification2", {{"param2", "value2"}})
41+
.Build();
42+
43+
CHECK(request.is_array());
44+
CHECK(request.size() == 4);
45+
c.Send(request[0].dump());
46+
c.VerifyMethodRequest(version::v2, "some_method1", 1);
47+
c.Send(request[1].dump());
48+
c.VerifyMethodRequest(version::v2, "some_method2", 2);
49+
c.Send(request[2].dump());
50+
c.VerifyNotificationRequest(version::v2, "some_notification1");
51+
c.Send(request[3].dump());
52+
c.VerifyNotificationRequest(version::v2, "some_notification2");
53+
}
54+
55+
TEST_CASE("batchclient", TEST_MODULE) {
56+
TestClientConnector c;
57+
BatchClient client(c);
58+
c.SetBatchResult({TestClientConnector::BuildResult("result1", 1), TestClientConnector::BuildResult(33, 2)});
59+
60+
BatchRequest r;
61+
r.AddMethodCall(1, "some_method", {"value1"});
62+
r.AddMethodCall(2, "some_method", {"value2"});
63+
BatchResponse response = client.BatchCall(r);
64+
CHECK(response.Get<string>(1) == "result1");
65+
CHECK(response.Get<int>(2) == 33);
66+
67+
c.SetBatchResult("{}");
68+
CHECK_THROWS_WITH(client.BatchCall(r), Contains("invalid JSON response from server: expected array"));
69+
c.raw_response = "somestring";
70+
CHECK_THROWS_WITH(client.BatchCall(r), Contains("invalid JSON response from server") && Contains("parse_error"));
3171
}

test/testclientconnector.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class TestClientConnector : public IClientConnector {
1919
return raw_response;
2020
}
2121

22+
void SetBatchResult(const json &result) {
23+
raw_response = result.dump();
24+
}
25+
26+
static json BuildResult(const json &result, int id) {
27+
return {{"jsonrpc", "2.0"}, {"id", id}, {"result", result}};
28+
}
29+
2230
void SetResult(const json &result) {
2331
json response = {{"jsonrpc", "2.0"}, {"id", "1"}, {"result", result}};
2432
raw_response = response.dump();

0 commit comments

Comments
 (0)