aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/util/grpc_tool_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/cpp/util/grpc_tool_test.cc')
-rw-r--r--test/cpp/util/grpc_tool_test.cc397
1 files changed, 388 insertions, 9 deletions
diff --git a/test/cpp/util/grpc_tool_test.cc b/test/cpp/util/grpc_tool_test.cc
index 3aae090e81..be9a624a2c 100644
--- a/test/cpp/util/grpc_tool_test.cc
+++ b/test/cpp/util/grpc_tool_test.cc
@@ -74,11 +74,20 @@ using grpc::testing::EchoResponse;
" rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
"{}\n"
-#define ECHO_RESPONSE_MESSAGE \
- "message: \"echo\"\n" \
- "param {\n" \
- " host: \"localhost\"\n" \
- " peer: \"peer\"\n" \
+#define ECHO_RESPONSE_MESSAGE_TEXT_FORMAT \
+ "message: \"echo\"\n" \
+ "param {\n" \
+ " host: \"localhost\"\n" \
+ " peer: \"peer\"\n" \
+ "}\n\n"
+
+#define ECHO_RESPONSE_MESSAGE_JSON_FORMAT \
+ "{\n" \
+ " \"message\": \"echo\",\n" \
+ " \"param\": {\n" \
+ " \"host\": \"localhost\",\n" \
+ " \"peer\": \"peer\"\n" \
+ " }\n" \
"}\n\n"
DECLARE_string(channel_creds_type);
@@ -89,6 +98,8 @@ namespace testing {
DECLARE_bool(binary_input);
DECLARE_bool(binary_output);
+DECLARE_bool(json_input);
+DECLARE_bool(json_output);
DECLARE_bool(l);
DECLARE_bool(batch);
DECLARE_string(metadata);
@@ -426,6 +437,61 @@ TEST_F(GrpcToolTest, CallCommand) {
// Expected output: "message: \"Hello\""
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "message: \"Hello\""));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello"
+ // }
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello\"\n}"));
+
+ ShutdownServer();
+}
+
+TEST_F(GrpcToolTest, CallCommandJsonInput) {
+ // Test input "grpc_cli call localhost:<port> Echo "{ \"message\": \"Hello\"}"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
+ "{ \"message\": \"Hello\"}"};
+
+ FLAGS_json_input = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ // Expected output: "message: \"Hello\""
+ EXPECT_TRUE(nullptr !=
+ strstr(output_stream.str().c_str(), "message: \"Hello\""));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_json_input = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello"
+ // }
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello\"\n}"));
+
ShutdownServer();
}
@@ -453,6 +519,101 @@ TEST_F(GrpcToolTest, CallCommandBatch) {
EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
"message: \"Hello0\"\nmessage: "
"\"Hello1\"\nmessage: \"Hello2\"\n"));
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ ss.clear();
+ ss.seekg(0);
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_batch = true;
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_batch = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello0"
+ // }
+ // {
+ // "message": "Hello1"
+ // }
+ // {
+ // "message": "Hello2"
+ // }
+ // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
+ // "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello0\"\n}\n"
+ "{\n \"message\": \"Hello1\"\n}\n"
+ "{\n \"message\": \"Hello2\"\n}\n"));
+
+ std::cin.rdbuf(orig);
+ ShutdownServer();
+}
+
+TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {
+ // Test input "grpc_cli call Echo"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
+ "{\"message\": \"Hello0\"}"};
+
+ // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss(
+ "{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_json_input = true;
+ FLAGS_batch = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_batch = false;
+
+ // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
+ // "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "message: \"Hello0\"\nmessage: "
+ "\"Hello1\"\nmessage: \"Hello2\"\n"));
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ ss.clear();
+ ss.seekg(0);
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_batch = true;
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_batch = false;
+ FLAGS_json_input = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello0"
+ // }
+ // {
+ // "message": "Hello1"
+ // }
+ // {
+ // "message": "Hello2"
+ // }
+ // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
+ // "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello0\"\n}\n"
+ "{\n \"message\": \"Hello1\"\n}\n"
+ "{\n \"message\": \"Hello2\"\n}\n"));
+
std::cin.rdbuf(orig);
ShutdownServer();
}
@@ -479,6 +640,95 @@ TEST_F(GrpcToolTest, CallCommandBatchWithBadRequest) {
// Expected output: "message: "Hello0"\nmessage: "Hello2"\n"
EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
"message: \"Hello0\"\nmessage: \"Hello2\"\n"));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ ss.clear();
+ ss.seekg(0);
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_batch = true;
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_batch = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello0"
+ // }
+ // {
+ // "message": "Hello2"
+ // }
+ // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
+ // "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello0\"\n}\n"
+ "{\n \"message\": \"Hello2\"\n}\n"));
+
+ std::cin.rdbuf(orig);
+ ShutdownServer();
+}
+
+TEST_F(GrpcToolTest, CallCommandBatchJsonInputWithBadRequest) {
+ // Test input "grpc_cli call Echo"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
+ "{ \"message\": \"Hello0\"}"};
+
+ // Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss(
+ "{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_batch = true;
+ FLAGS_json_input = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_input = false;
+ FLAGS_batch = false;
+
+ // Expected output: "message: "Hello0"\nmessage: "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ ss.clear();
+ ss.seekg(0);
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_batch = true;
+ FLAGS_json_input = true;
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_json_input = false;
+ FLAGS_batch = false;
+
+ // Expected output:
+ // {
+ // "message": "Hello0"
+ // }
+ // {
+ // "message": "Hello2"
+ // }
+ // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
+ // "Hello2"\n"
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "{\n \"message\": \"Hello0\"\n}\n"
+ "{\n \"message\": \"Hello2\"\n}\n"));
+
std::cin.rdbuf(orig);
ShutdownServer();
}
@@ -508,6 +758,34 @@ TEST_F(GrpcToolTest, CallCommandRequestStream) {
ShutdownServer();
}
+TEST_F(GrpcToolTest, CallCommandRequestStreamJsonInput) {
+ // Test input: grpc_cli call localhost:<port> RequestStream "{ \"message\":
+ // \"Hello0\"}"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
+ "RequestStream", "{ \"message\": \"Hello0\" }"};
+
+ // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss(
+ "{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_json_input = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_input = false;
+
+ // Expected output: "message: \"Hello0Hello1Hello2\""
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ "message: \"Hello0Hello1Hello2\""));
+ std::cin.rdbuf(orig);
+ ShutdownServer();
+}
+
TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequest) {
// Test input: grpc_cli call localhost:<port> RequestStream "message:
// 'Hello0'"
@@ -533,6 +811,34 @@ TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequest) {
ShutdownServer();
}
+TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequestJsonInput) {
+ // Test input: grpc_cli call localhost:<port> RequestStream "message:
+ // 'Hello0'"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
+ "RequestStream", "{ \"message\": \"Hello0\" }"};
+
+ // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss(
+ "{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ FLAGS_json_input = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_input = false;
+
+ // Expected output: "message: \"Hello0Hello2\""
+ EXPECT_TRUE(nullptr !=
+ strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));
+ std::cin.rdbuf(orig);
+ ShutdownServer();
+}
+
TEST_F(GrpcToolTest, CallCommandResponseStream) {
// Test input: grpc_cli call localhost:<port> ResponseStream "message:
// 'Hello'"
@@ -554,6 +860,24 @@ TEST_F(GrpcToolTest, CallCommandResponseStream) {
expected_response_text.c_str()));
}
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+
+ // Expected output: "{\n \"message\": \"Hello{n}\"\n}\n"
+ for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {
+ grpc::string expected_response_text =
+ "{\n \"message\": \"Hello" + grpc::to_string(i) + "\"\n}\n";
+ EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
+ expected_response_text.c_str()));
+ }
+
ShutdownServer();
}
@@ -617,15 +941,31 @@ TEST_F(GrpcToolTest, ParseCommand) {
const grpc::string server_address = SetUpServer();
const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
- "grpc.testing.EchoResponse", ECHO_RESPONSE_MESSAGE};
+ "grpc.testing.EchoResponse",
+ ECHO_RESPONSE_MESSAGE_TEXT_FORMAT};
FLAGS_binary_input = false;
FLAGS_binary_output = false;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
- // Expected output: ECHO_RESPONSE_MESSAGE
- EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+ // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
+ ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+
+ // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
+ ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
// Parse text message to binary message and then parse it back to text message
output_stream.str(grpc::string());
@@ -645,13 +985,52 @@ TEST_F(GrpcToolTest, ParseCommand) {
std::placeholders::_1)));
// Expected output: ECHO_RESPONSE_MESSAGE
- EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
+ ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
FLAGS_binary_input = false;
FLAGS_binary_output = false;
ShutdownServer();
}
+TEST_F(GrpcToolTest, ParseCommandJsonFormat) {
+ // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse
+ // ECHO_RESPONSE_MESSAGE_JSON_FORMAT"
+ std::stringstream output_stream;
+ std::stringstream binary_output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
+ "grpc.testing.EchoResponse",
+ ECHO_RESPONSE_MESSAGE_JSON_FORMAT};
+
+ FLAGS_json_input = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+
+ // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
+ ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
+
+ // with json_output
+ output_stream.str(grpc::string());
+ output_stream.clear();
+
+ FLAGS_json_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ FLAGS_json_output = false;
+ FLAGS_json_input = false;
+
+ // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
+ ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
+
+ ShutdownServer();
+}
+
TEST_F(GrpcToolTest, TooFewArguments) {
// Test input "grpc_cli call Echo"
std::stringstream output_stream;