-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathtest_basic_protocol.cc
More file actions
635 lines (554 loc) · 22.5 KB
/
test_basic_protocol.cc
File metadata and controls
635 lines (554 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <catch2/catch_test_macros.hpp> /* catch unit-test framework */
#include <tsutil/ts_bw_format.h>
#include <tsutil/ts_errata.h>
#include "mgmt/rpc/jsonrpc/JsonRPCManager.h"
#include "mgmt/rpc/jsonrpc/JsonRPC.h"
#include "mgmt/rpc/handlers/common/ErrorUtils.h"
namespace
{
// Not using the singleton logic.
struct JsonRpcUnitTest : rpc::JsonRPCManager {
JsonRpcUnitTest() : JsonRPCManager() {}
using base = JsonRPCManager;
bool
remove_handler(std::string const &name)
{
return base::remove_handler(name);
}
template <typename Func>
bool
add_notification_handler(const std::string &name, Func &&call)
{
return base::add_notification_handler(name, std::forward<Func>(call), nullptr, {});
}
template <typename Func>
bool
add_method_handler(const std::string &name, Func &&call)
{
return base::add_method_handler(name, std::forward<Func>(call), nullptr, {});
}
std::optional<std::string>
handle_call(std::string const &jsonString)
{
return base::handle_call(rpc::Context{}, jsonString);
}
};
enum class TestErrors { ERR1 = 9999, ERR2 };
static const std::error_code ERR1{ts::make_errno_code(9999)};
static const std::error_code ERR2{ts::make_errno_code(10000)};
static std::string_view err{"Just an error message to add more meaning to the failure"};
inline swoc::Rv<YAML::Node>
test_callback_ok_or_error(std::string_view const & /* id ATS_UNUSED */, YAML::Node const ¶ms)
{
swoc::Rv<YAML::Node> resp;
// play with the req.id if needed.
if (YAML::Node n = params["return_error"]) {
auto yesOrNo = n.as<std::string>();
if (yesOrNo == "yes") {
resp.errata().assign(ERR1).note(err);
} else {
resp.result()["ran"] = "ok";
}
}
return resp;
}
inline swoc::Rv<YAML::Node>
test_callback_with_severity(std::string_view const & /* id ATS_UNUSED */, YAML::Node const & /* params ATS_UNUSED */)
{
swoc::Rv<YAML::Node> resp;
resp.errata().assign(ERR1).note(ERRATA_WARN, "this is a warning").note("this has no severity");
return resp;
}
static int notificationCallCount{0};
inline void
test_nofitication(YAML::Node const & /* params ATS_UNUSED */)
{
notificationCallCount++;
}
} // namespace
TEST_CASE("Multiple Registrations - methods", "[methods]")
{
JsonRpcUnitTest rpc;
SECTION("More than one method")
{
REQUIRE(rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error));
REQUIRE(rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error) == false);
}
}
TEST_CASE("Multiple Registrations - notifications", "[notifications]")
{
JsonRpcUnitTest rpc;
SECTION("inserting several notifications with the same name")
{
REQUIRE(rpc.add_notification_handler("test_nofitication", &test_nofitication));
REQUIRE(rpc.add_notification_handler("test_nofitication", &test_nofitication) == false);
}
}
TEST_CASE("Register/call method", "[method]")
{
JsonRpcUnitTest rpc;
SECTION("Registering the method")
{
REQUIRE(rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error));
SECTION("Calling the method")
{
const auto json = rpc.handle_call(
R"({"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": "13"})");
REQUIRE(json);
const std::string_view expected = R"({"jsonrpc": "2.0", "result": {"ran": "ok"}, "id": "13"})";
REQUIRE(*json == expected);
}
}
}
// VALID RESPONSES WITH CUSTOM ERRORS
TEST_CASE("Register/call method - respond with errors (data field)", "[method][error.data]")
{
JsonRpcUnitTest rpc;
SECTION("Registering the method")
{
REQUIRE(rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error));
SECTION("Calling the method")
{
const auto json = rpc.handle_call(
R"({"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "yes"}, "id": "14"})");
REQUIRE(json);
const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 9, "message": "Error during execution", "data": [{"code": 9999, "severity": 0, "message": "Just an error message to add more meaning to the failure"}]}, "id": "14"})";
REQUIRE(*json == expected);
}
}
}
TEST_CASE("Register/call notification", "[notifications]")
{
JsonRpcUnitTest rpc;
SECTION("Registering the notification")
{
REQUIRE(rpc.add_notification_handler("test_nofitication", &test_nofitication));
SECTION("Calling the notification")
{
rpc.handle_call(R"({"jsonrpc": "2.0", "method": "test_nofitication", "params": {"json": "rpc"}})");
REQUIRE(notificationCallCount == 1);
notificationCallCount = 0; // for further use.
}
}
}
TEST_CASE("Basic test, batch calls", "[methods][notifications]")
{
JsonRpcUnitTest rpc;
SECTION("inserting multiple functions, mixed method and notifications.")
{
const auto f1_added = rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error);
const bool f2_added = rpc.add_notification_handler("test_nofitication", &test_nofitication);
REQUIRE(f1_added);
REQUIRE(f2_added);
SECTION("we call in batch, two functions and one notification")
{
const auto resp1 = rpc.handle_call(
R"([{"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": "13"}
,{"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "yes"}, "id": "14"}
,{"jsonrpc": "2.0", "method": "test_nofitication", "params": {"name": "damian"}}])");
REQUIRE(resp1);
const std::string_view expected =
R"([{"jsonrpc": "2.0", "result": {"ran": "ok"}, "id": "13"}, {"jsonrpc": "2.0", "error": {"code": 9, "message": "Error during execution", "data": [{"code": 9999, "severity": 0, "message": "Just an error message to add more meaning to the failure"}]}, "id": "14"}])";
REQUIRE(*resp1 == expected);
}
}
}
TEST_CASE("Single registered notification", "[notifications]")
{
notificationCallCount = 0;
JsonRpcUnitTest rpc;
SECTION("Single notification, only notifications in the batch.")
{
REQUIRE(rpc.add_notification_handler("test_nofitication", &test_nofitication));
SECTION("Call the notifications in batch")
{
const auto should_be_no_response = rpc.handle_call(
R"([{"jsonrpc": "2.0", "method": "test_nofitication", "params": {"name": "JSON"}},
{"jsonrpc": "2.0", "method": "test_nofitication", "params": {"name": "RPC"}},
{"jsonrpc": "2.0", "method": "test_nofitication", "params": {"name": "2.0"}}])");
REQUIRE(!should_be_no_response);
REQUIRE(notificationCallCount == 3);
}
}
}
TEST_CASE("Valid json but invalid messages", "[errors]")
{
JsonRpcUnitTest rpc;
SECTION("Valid json but invalid protocol {}")
{
const auto resp = rpc.handle_call(R"({})");
REQUIRE(resp);
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}})";
REQUIRE(*resp == expected);
}
SECTION("Valid json but invalid protocol [{},{}] - batch response")
{
const auto resp = rpc.handle_call(R"([{},{}])");
REQUIRE(resp);
std::string_view expected =
R"([{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}])";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Invalid json messages", "[errors][invalid json]")
{
JsonRpcUnitTest rpc;
SECTION("Invalid json in an attempt of batch")
{
const auto resp = rpc.handle_call(
R"([{"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": "13"}
,{"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "yes
,{"jsonrpc": "2.0", "method": "test_nofitication", "params": {"name": "damian"}}])");
REQUIRE(resp);
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}})";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Invalid parameters base on the jsonrpc 2.0 protocol", "[protocol]")
{
JsonRpcUnitTest rpc;
REQUIRE(rpc.add_method_handler("test_callback_ok_or_error", &test_callback_ok_or_error));
REQUIRE(rpc.add_notification_handler("test_nofitication", &test_nofitication));
SECTION("version field")
{
SECTION("number instead of a string")
{
// THIS WILL FAIL BASE ON THE YAMLCPP WAY TO TEST TYPES. We can get the number first, which will be ok and then fail, but
// seems not the right way to do it. ok for now.
[[maybe_unused]] const auto resp =
rpc.handle_call(R"({"jsonrpc": 2.0, "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": "13"})");
// REQUIRE(resp);
[[maybe_unused]] const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 2, "message": "Invalid version type, should be a string"}, "id": "13"})";
// REQUIRE(*resp == expected);
}
SECTION("2.8 instead of 2.0")
{
const auto resp = rpc.handle_call(
R"({"jsonrpc": "2.8", "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": "15"})");
REQUIRE(resp);
const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 1, "message": "Invalid version, 2.0 only"}, "id": "15"})";
REQUIRE(*resp == expected);
}
}
SECTION("method field")
{
SECTION("using a number")
{
// THIS WILL FAIL BASE ON THE YAMLCPP WAY TO TEST TYPES, there is no explicit way to test for a particular type, we can get
// the number first, then as it should not be converted we get the string instead, this seems rather not the best way to do
// it. ok for now.
[[maybe_unused]] const auto resp =
rpc.handle_call(R"({"jsonrpc": "2.0", "method": 123, "params": {"return_error": "no"}, "id": "14"})");
// REQUIRE(resp);
[[maybe_unused]] const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 4, "message": "Invalid method type, should be a string"}, "id": "14"})";
// REQUIRE(*resp == expected);
}
}
SECTION("param field")
{
SECTION("Invalidtype")
{
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": 13, "id": "13"})");
REQUIRE(resp);
const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 6, "message": "Invalid params type. A Structured value is expected"}, "id": "13"})";
REQUIRE(*resp == expected);
}
}
SECTION("id field")
{
SECTION("null id")
{
const auto resp = rpc.handle_call(
R"({"jsonrpc": "2.0", "method": "test_callback_ok_or_error", "params": {"return_error": "no"}, "id": null})");
REQUIRE(resp);
const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 8, "message": "Use of null as id is discouraged"}})";
REQUIRE(*resp == expected);
}
}
}
TEST_CASE("Basic test with member functions(add, remove)", "[basic][member_functions]")
{
struct TestMemberFunctionCall {
TestMemberFunctionCall() {}
bool
register_member_function_as_callback(JsonRpcUnitTest &rpc)
{
return rpc.add_method_handler(
"member_function",
[this](std::string_view const &id, const YAML::Node &req) -> swoc::Rv<YAML::Node> { return test(id, req); });
}
swoc::Rv<YAML::Node>
test(std::string_view const & /* id ATS_UNUSED */, const YAML::Node & /* req ATS_UNUSED */)
{
swoc::Rv<YAML::Node> resp;
resp.result() = "grand!";
return resp;
}
// TODO: test notification as well.
};
SECTION("A RPC object and a custom class")
{
JsonRpcUnitTest rpc;
TestMemberFunctionCall tmfc;
REQUIRE(tmfc.register_member_function_as_callback(rpc) == true);
SECTION("call the member function")
{
const auto response = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "member_function", "id": "AbC"})");
REQUIRE(response);
REQUIRE(*response == R"({"jsonrpc": "2.0", "result": "grand!", "id": "AbC"})");
}
SECTION("We remove the callback handler")
{
REQUIRE(rpc.remove_handler("member_function") == true);
const auto response = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "member_function", "id": "AbC"})");
REQUIRE(response);
REQUIRE(*response == R"({"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "AbC"})");
}
}
}
TEST_CASE("Test Dispatcher rpc method", "[dispatcher]")
{
JsonRpcUnitTest rpc;
const auto response = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "show_registered_handlers", "id": "AbC"})");
REQUIRE(*response ==
R"({"jsonrpc": "2.0", "result": {"methods": ["get_service_descriptor", "show_registered_handlers"]}, "id": "AbC"})");
}
[[maybe_unused]] static swoc::Rv<YAML::Node>
subtract(std::string_view const & /* id ATS_UNUSED */, YAML::Node const &numbers)
{
swoc::Rv<YAML::Node> res;
if (numbers.Type() == YAML::NodeType::Sequence) {
auto it = numbers.begin();
int total = it->as<int>();
++it;
for (; it != numbers.end(); ++it) {
total -= it->as<int>();
}
res.result() = total;
} else if (numbers.Type() == YAML::NodeType::Map) {
if (numbers["subtrahend"] && numbers["minuend"]) {
int total = numbers["minuend"].as<int>() - numbers["subtrahend"].as<int>();
res.result() = total;
}
}
return res;
}
[[maybe_unused]] static swoc::Rv<YAML::Node>
sum(std::string_view const & /* id ATS_UNUSED */, YAML::Node const ¶ms)
{
swoc::Rv<YAML::Node> res;
int total{0};
for (auto n : params) {
total += n.as<int>();
}
res.result() = total;
return res;
}
[[maybe_unused]] static swoc::Rv<YAML::Node>
get_data(std::string_view const & /* id ATS_UNUSED */, YAML::Node const & /* params ATS_UNUSED */)
{
swoc::Rv<YAML::Node> res;
res.result().push_back("hello");
res.result().push_back("5");
return res;
}
[[maybe_unused]] static void
update(YAML::Node const & /* params ATS_UNUSED */)
{
}
[[maybe_unused]] static void
foobar(YAML::Node const & /* params ATS_UNUSED */)
{
}
[[maybe_unused]] static void
notify_hello(YAML::Node const & /* params ATS_UNUSED */)
{
}
// TODO: add tests base on the protocol example doc.
TEST_CASE("Basic tests from the jsonrpc 2.0 doc.")
{
SECTION("rpc call with positional parameters")
{
JsonRpcUnitTest rpc;
REQUIRE(rpc.add_method_handler("subtract", &subtract));
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "1"})");
REQUIRE(resp);
REQUIRE(*resp == R"({"jsonrpc": "2.0", "result": "19", "id": "1"})");
const auto resp1 = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": "1"})");
REQUIRE(resp1);
REQUIRE(*resp1 == R"({"jsonrpc": "2.0", "result": "-19", "id": "1"})");
}
SECTION("rpc call with named parameters")
{
JsonRpcUnitTest rpc;
REQUIRE(rpc.add_method_handler("subtract", &subtract));
const auto resp =
rpc.handle_call(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": "3"})");
REQUIRE(resp);
REQUIRE(*resp == R"({"jsonrpc": "2.0", "result": "19", "id": "3"})");
const auto resp1 =
rpc.handle_call(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": "3"})");
REQUIRE(resp1);
REQUIRE(*resp1 == R"({"jsonrpc": "2.0", "result": "19", "id": "3"})");
}
SECTION("A notification")
{
JsonRpcUnitTest rpc;
REQUIRE(rpc.add_notification_handler("update", &update));
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]})");
REQUIRE(!resp);
REQUIRE(rpc.add_notification_handler("foobar", &foobar));
const auto resp1 = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "foobar"})");
REQUIRE(!resp1);
}
SECTION("rpc call of non-existent method")
{
JsonRpcUnitTest rpc;
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "foobar", "id": "1"})");
REQUIRE(resp);
REQUIRE(*resp == R"({"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"})");
}
SECTION("rpc call with invalid JSON")
{
JsonRpcUnitTest rpc;
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz])");
REQUIRE(resp);
REQUIRE(*resp == R"({"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}})");
}
SECTION("rpc call with invalid Request object")
{
// We do have a custom object here, which will return invalid param object.
// skip it
}
SECTION("rpc call Batch, invalid JSON")
{
JsonRpcUnitTest rpc;
const auto resp =
rpc.handle_call(R"( {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method")");
REQUIRE(resp);
REQUIRE(*resp == R"({"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}})");
}
SECTION("rpc call with an empty Array")
{
JsonRpcUnitTest rpc;
const auto resp = rpc.handle_call(R"([])");
REQUIRE(resp);
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}})";
REQUIRE(*resp == expected);
}
SECTION("rpc call with an invalid Batch")
{
JsonRpcUnitTest rpc;
const auto resp = rpc.handle_call(R"([1])");
REQUIRE(resp);
std::string_view expected = R"([{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}])";
REQUIRE(*resp == expected);
}
SECTION("rpc call with invalid Batch")
{
JsonRpcUnitTest rpc;
const auto resp = rpc.handle_call(R"([1,2,3])");
REQUIRE(resp);
std::string expected = R"([{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}})"
R"(, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}})"
R"(, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}])";
REQUIRE(*resp == expected);
}
SECTION("rpc call Batch(all notifications")
{
JsonRpcUnitTest rpc;
REQUIRE(rpc.add_notification_handler("notify_hello", ¬ify_hello));
REQUIRE(rpc.add_notification_handler("notify_sum", ¬ify_hello));
const auto resp = rpc.handle_call(
R"( [{"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}])");
REQUIRE(!resp);
}
}
TEST_CASE("Handle un-handle handler's error", "[throw]")
{
JsonRpcUnitTest rpc;
SECTION("Basic exception thrown")
{
REQUIRE(rpc.add_method_handler("oops_i_did_it_again",
[](std::string_view const & /* id ATS_UNUSED */, const YAML::Node & /* params ATS_UNUSED */)
-> swoc::Rv<YAML::Node> { throw std::runtime_error("Oops, I did it again"); }));
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "oops_i_did_it_again", "id": "1"})");
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": 9, "message": "Error during execution"}, "id": "1"})";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Call registered method with no ID", "[no-id]")
{
JsonRpcUnitTest rpc;
SECTION("Basic test, no id on method call")
{
REQUIRE(rpc.add_method_handler("call_me_with_no_id",
[](std::string_view const & /* id ATS_UNUSED */, const YAML::Node & /* params ATS_UNUSED */)
-> swoc::Rv<YAML::Node> { throw std::runtime_error("Oops, I did it again"); }));
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "call_me_with_no_id"})");
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}})";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Call registered notification with ID", "[notification_and_id]")
{
JsonRpcUnitTest rpc;
SECTION("Basic test, id on a notification call")
{
REQUIRE(rpc.add_notification_handler("call_me_with_id", [](const YAML::Node & /* params ATS_UNUSED */) -> void {
throw std::runtime_error("Oops, I did it again");
}));
const auto resp = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "call_me_with_id", "id": "1"})");
std::string_view expected = R"({"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": "1"})";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Call method with invalid ID", "[invalid_id]")
{
JsonRpcUnitTest rpc;
SECTION("Basic test, invalid ids")
{
std::string req = R"([{"id": "", "jsonrpc": "2.0", "method": "will_not_pass_the_validation"},)"
R"({"id": {}, "jsonrpc": "2.0", "method": "will_not_pass_the_validation"}])";
auto resp = rpc.handle_call(req);
std::string expected =
R"([{"jsonrpc": "2.0", "error": {"code": 11, "message": "Use of an empty string as id is discouraged"}}, )"
R"({"jsonrpc": "2.0", "error": {"code": 7, "message": "Invalid id type"}}])";
REQUIRE(*resp == expected);
}
}
TEST_CASE("Severity field in error data entries", "[severity]")
{
JsonRpcUnitTest rpc;
SECTION("Annotation with severity emits it, annotation without defaults to DIAG (0)")
{
REQUIRE(rpc.add_method_handler("test_callback_with_severity", &test_callback_with_severity));
const auto json = rpc.handle_call(R"({"jsonrpc": "2.0", "method": "test_callback_with_severity", "params": {}, "id": "50"})");
REQUIRE(json);
const std::string_view expected =
R"({"jsonrpc": "2.0", "error": {"code": 9, "message": "Error during execution", "data": [{"code": 9999, "severity": 4, "message": "this is a warning"}, {"code": 9999, "severity": 0, "message": "this has no severity"}]}, "id": "50"})";
REQUIRE(*json == expected);
}
}