cli_call_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/cpp/util/cli_call.h"
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpcpp/channel.h>
  22. #include <grpcpp/client_context.h>
  23. #include <grpcpp/create_channel.h>
  24. #include <grpcpp/server.h>
  25. #include <grpcpp/server_builder.h>
  26. #include <grpcpp/server_context.h>
  27. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  28. #include "test/core/util/port.h"
  29. #include "test/core/util/test_config.h"
  30. #include "test/cpp/util/string_ref_helper.h"
  31. using grpc::testing::EchoRequest;
  32. using grpc::testing::EchoResponse;
  33. namespace grpc {
  34. namespace testing {
  35. class TestServiceImpl : public grpc::testing::EchoTestService::Service {
  36. public:
  37. Status Echo(ServerContext* context, const EchoRequest* request,
  38. EchoResponse* response) override {
  39. if (!context->client_metadata().empty()) {
  40. for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
  41. iter = context->client_metadata().begin();
  42. iter != context->client_metadata().end(); ++iter) {
  43. context->AddInitialMetadata(ToString(iter->first),
  44. ToString(iter->second));
  45. }
  46. }
  47. context->AddTrailingMetadata("trailing_key", "trailing_value");
  48. response->set_message(request->message());
  49. return Status::OK;
  50. }
  51. };
  52. class CliCallTest : public ::testing::Test {
  53. protected:
  54. CliCallTest() {}
  55. void SetUp() override {
  56. int port = grpc_pick_unused_port_or_die();
  57. server_address_ << "localhost:" << port;
  58. // Setup server
  59. ServerBuilder builder;
  60. builder.AddListeningPort(server_address_.str(),
  61. InsecureServerCredentials());
  62. builder.RegisterService(&service_);
  63. server_ = builder.BuildAndStart();
  64. }
  65. void TearDown() override { server_->Shutdown(); }
  66. void ResetStub() {
  67. channel_ = grpc::CreateChannel(server_address_.str(),
  68. InsecureChannelCredentials());
  69. stub_ = grpc::testing::EchoTestService::NewStub(channel_);
  70. }
  71. std::shared_ptr<Channel> channel_;
  72. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
  73. std::unique_ptr<Server> server_;
  74. std::ostringstream server_address_;
  75. TestServiceImpl service_;
  76. };
  77. // Send a rpc with a normal stub and then a CliCall. Verify they match.
  78. TEST_F(CliCallTest, SimpleRpc) {
  79. ResetStub();
  80. // Normal stub.
  81. EchoRequest request;
  82. EchoResponse response;
  83. request.set_message("Hello");
  84. ClientContext context;
  85. context.AddMetadata("key1", "val1");
  86. Status s = stub_->Echo(&context, request, &response);
  87. EXPECT_EQ(response.message(), request.message());
  88. EXPECT_TRUE(s.ok());
  89. const std::string kMethod("/grpc.testing.EchoTestService/Echo");
  90. std::string request_bin, response_bin, expected_response_bin;
  91. EXPECT_TRUE(request.SerializeToString(&request_bin));
  92. EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
  93. std::multimap<std::string, std::string> client_metadata;
  94. std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
  95. server_trailing_metadata;
  96. client_metadata.insert(std::pair<std::string, std::string>("key1", "val1"));
  97. CliCall call(channel_, kMethod, client_metadata);
  98. Status s2 = call.Call(request_bin, &response_bin, &server_initial_metadata,
  99. &server_trailing_metadata);
  100. EXPECT_TRUE(s2.ok());
  101. EXPECT_EQ(expected_response_bin, response_bin);
  102. EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
  103. EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
  104. }
  105. } // namespace testing
  106. } // namespace grpc
  107. int main(int argc, char** argv) {
  108. grpc::testing::TestEnvironment env(argc, argv);
  109. ::testing::InitGoogleTest(&argc, argv);
  110. return RUN_ALL_TESTS();
  111. }