Skip to content

Commit

Permalink
Add test case for `pb_single_repeated_to_array'
Browse files Browse the repository at this point in the history
  • Loading branch information
wxf committed Nov 12, 2021
1 parent c0fa0da commit cec3569
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
83 changes: 83 additions & 0 deletions test/brpc_protobuf_json_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,89 @@ TEST_F(ProtobufJsonTest, pb_to_json_encode_decode) {
json2pb::JsonToProtoMessage(info1, &data1, NULL);
json2pb::ProtoMessageToJson(data1, &info3, NULL);
ASSERT_STREQ(info1.data(), info3.data());

printf("----------test single repeated pb to json array------------\n\n");

AddressBookEncDec single_repeated_json_data;

auto person = single_repeated_json_data.add_person();
person->set_id(1);
person->set_name("foo");
*person->mutable_json_body() = json_data;
option.bytes_to_base64 = true;

std::string text1;
printer.PrintToString(single_repeated_json_data, &text1);

printf("text1:\n\n%s\n", text1.data());

std::string info4;
option.single_repeated_to_array = false;
ASSERT_TRUE(ProtoMessageToJson(single_repeated_json_data, &info4, option, NULL));
#ifndef RAPIDJSON_VERSION_0_1
ASSERT_STREQ("{\"person\":["
"{\"name\":\"foo\",\"id\":1,\"json_body\":"
"{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
"\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
"[{\"uid*\":\"content info\",\"Distance_info_\":1234.56005859375,\"_ext%T_\":"
"{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
"\"enum--type\":\"WORK\"}}]}}]}",
info4.data());
#else
ASSERT_STREQ("{\"person\":["
"{\"name\":\"foo\",\"id\":1,\"json_body\":"
"{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
"\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
"[{\"uid*\":\"content info\",\"Distance_info_\":1234.560059,\"_ext%T_\":"
"{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
"\"enum--type\":\"WORK\"}}]}}]}",
info4.data());
#endif

std::string info5;
option.single_repeated_to_array = true;
ASSERT_TRUE(ProtoMessageToJson(single_repeated_json_data, &info5, option, NULL));
#ifndef RAPIDJSON_VERSION_0_1
ASSERT_STREQ("[{\"name\":\"foo\",\"id\":1,\"json_body\":"
"{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
"\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
"[{\"uid*\":\"content info\",\"Distance_info_\":1234.56005859375,\"_ext%T_\":"
"{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
"\"enum--type\":\"WORK\"}}]}}]",
info5.data());
#else
ASSERT_STREQ("[{\"name\":\"foo\",\"id\":1,\"json_body\":"
"{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
"\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
"[{\"uid*\":\"content info\",\"Distance_info_\":1234.560059,\"_ext%T_\":"
"{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
"\"enum--type\":\"WORK\"}}]}}]",
info5.data());
#endif

printf("----------test json array to single repeated pb------------\n\n");

std::string info6;
AddressBookEncDec data2;
// object -> pb
json2pb::JsonToProtoMessage(info4, &data2, NULL);
json2pb::ProtoMessageToJson(data2, &info6, option, NULL);

ASSERT_STREQ(info6.data(), info5.data());

std::string info7;
AddressBookEncDec data3;
json2pb::Json2PbOptions option2;
option2.array_to_single_repeated = true;
// array -> pb
json2pb::JsonToProtoMessage(info5, &data3, option2, NULL);
json2pb::ProtoMessageToJson(data3, &info7, option, NULL);
ASSERT_STREQ(info7.data(), info5.data());

std::string info8;
option.single_repeated_to_array = false;
json2pb::ProtoMessageToJson(data3, &info8, option, NULL);
ASSERT_STREQ(info8.data(), info4.data());
}

TEST_F(ProtobufJsonTest, pb_to_json_control_char_case) {
Expand Down
52 changes: 52 additions & 0 deletions test/brpc_server_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ class EchoServiceImpl : public test::EchoService {
}
}

virtual void ComboEcho(google::protobuf::RpcController*,
const test::ComboRequest* request,
test::ComboResponse* response,
google::protobuf::Closure* done) {
brpc::ClosureGuard done_guard(done);
for (int i = 0; i < request->requests_size(); ++i) {
response->add_responses()->set_message(request->requests(i).message());
}
}

virtual void BytesEcho1(google::protobuf::RpcController*,
const test::BytesRequest* request,
test::BytesResponse* response,
Expand Down Expand Up @@ -1299,6 +1309,48 @@ TEST_F(ServerTest, base64_to_string) {
}
}

TEST_F(ServerTest, single_repeated_to_array) {
for (int i = 0; i < 2; ++i) {
brpc::Server server;
EchoServiceImpl echo_svc;
brpc::ServiceOptions service_opt;
service_opt.pb_single_repeated_to_array = (i == 0);

ASSERT_EQ(0, server.AddService(&echo_svc, service_opt));
ASSERT_EQ(0, server.Start(8613, NULL));

for (int j = 0; j < 2; ++j) {
brpc::Channel chan;
brpc::ChannelOptions opt;
opt.protocol = brpc::PROTOCOL_HTTP;
ASSERT_EQ(0, chan.Init("localhost:8613", &opt));
brpc::Controller cntl;
cntl.http_request().uri() = "/EchoService/ComboEcho";
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.http_request().set_content_type("application/json");
cntl.set_pb_single_repeated_to_array(j == 0);
test::ComboRequest req;
req.add_requests()->set_message("foo");
req.add_requests()->set_message("bar");

test::ComboResponse res;
chan.CallMethod(NULL, &cntl, &req, &res, NULL);
if (i == j) {
EXPECT_FALSE(cntl.Failed());
EXPECT_EQ(res.responses_size(), req.requests_size());
for (int k = 0; k < req.requests_size(); ++k) {
EXPECT_EQ(req.requests(k).message(), res.responses(k).message());
}
} else {
EXPECT_TRUE(cntl.Failed());
}
}

server.Stop(0);
server.Join();
}
}

TEST_F(ServerTest, too_big_message) {
EchoServiceImpl echo_svc;
brpc::Server server;
Expand Down

0 comments on commit cec3569

Please sign in to comment.