1
1
#include " catch/catch.hpp"
2
+ #include " testclientconnector.hpp"
2
3
#include < iostream>
3
4
#include < jsonrpccxx/batchclient.hpp>
4
5
@@ -8,7 +9,7 @@ using namespace std;
8
9
using namespace jsonrpccxx ;
9
10
using namespace Catch ::Matchers;
10
11
11
- TEST_CASE (" batchresponse_parsing " , TEST_MODULE) {
12
+ TEST_CASE (" batchresponse " , TEST_MODULE) {
12
13
BatchResponse br ({{{" jsonrpc" , " 2.0" }, {" id" , " 1" }, {" result" , " someresultstring" }},
13
14
{{" jsonrpc" , " 2.0" }, {" id" , " 2" }, {" result" , 33 }},
14
15
{{" jsonrpc" , " 2.0" }, {" id" , " 3" }, {" error" , {{" code" , -111 }, {" message" , " the error message" }}}},
@@ -28,4 +29,43 @@ TEST_CASE("batchresponse_parsing", TEST_MODULE) {
28
29
CHECK (br.GetResponse ().size () == 5 );
29
30
CHECK (br.GetResponse ()[br.GetInvalidIndexes ()[0 ]][" error" ][" code" ] == -112 );
30
31
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" ));
31
71
}
0 commit comments