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
19 #include "test/cpp/util/cli_call.h"
20
21 #include <grpc/grpc.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/client_context.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/server.h>
26 #include <grpcpp/server_builder.h>
27 #include <grpcpp/server_context.h>
28 #include <gtest/gtest.h>
29
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/test_util/port.h"
32 #include "test/core/test_util/test_config.h"
33 #include "test/cpp/util/string_ref_helper.h"
34
35 namespace grpc {
36 namespace testing {
37
38 class TestServiceImpl : public grpc::testing::EchoTestService::Service {
39 public:
Echo(ServerContext * context,const EchoRequest * request,EchoResponse * response)40 Status Echo(ServerContext* context, const EchoRequest* request,
41 EchoResponse* response) override {
42 if (!context->client_metadata().empty()) {
43 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
44 iter = context->client_metadata().begin();
45 iter != context->client_metadata().end(); ++iter) {
46 context->AddInitialMetadata(ToString(iter->first),
47 ToString(iter->second));
48 }
49 }
50 context->AddTrailingMetadata("trailing_key", "trailing_value");
51 response->set_message(request->message());
52 return Status::OK;
53 }
54 };
55
56 class CliCallTest : public ::testing::Test {
57 protected:
CliCallTest()58 CliCallTest() {}
59
SetUp()60 void SetUp() override {
61 int port = grpc_pick_unused_port_or_die();
62 server_address_ << "localhost:" << port;
63 // Setup server
64 ServerBuilder builder;
65 builder.AddListeningPort(server_address_.str(),
66 InsecureServerCredentials());
67 builder.RegisterService(&service_);
68 server_ = builder.BuildAndStart();
69 }
70
TearDown()71 void TearDown() override { server_->Shutdown(); }
72
ResetStub()73 void ResetStub() {
74 channel_ = grpc::CreateChannel(server_address_.str(),
75 InsecureChannelCredentials());
76 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
77 }
78
79 std::shared_ptr<Channel> channel_;
80 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
81 std::unique_ptr<Server> server_;
82 std::ostringstream server_address_;
83 TestServiceImpl service_;
84 };
85
86 // Send a rpc with a normal stub and then a CliCall. Verify they match.
TEST_F(CliCallTest,SimpleRpc)87 TEST_F(CliCallTest, SimpleRpc) {
88 ResetStub();
89 // Normal stub.
90 EchoRequest request;
91 EchoResponse response;
92 request.set_message("Hello");
93
94 ClientContext context;
95 context.AddMetadata("key1", "val1");
96 Status s = stub_->Echo(&context, request, &response);
97 EXPECT_EQ(response.message(), request.message());
98 EXPECT_TRUE(s.ok());
99
100 const std::string kMethod("/grpc.testing.EchoTestService/Echo");
101 std::string request_bin, response_bin, expected_response_bin;
102 EXPECT_TRUE(request.SerializeToString(&request_bin));
103 EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
104 std::multimap<std::string, std::string> client_metadata;
105 std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
106 server_trailing_metadata;
107 client_metadata.insert(std::pair<std::string, std::string>("key1", "val1"));
108 CliCall call(channel_, kMethod, client_metadata);
109 Status s2 = call.Call(request_bin, &response_bin, &server_initial_metadata,
110 &server_trailing_metadata);
111 EXPECT_TRUE(s2.ok());
112
113 EXPECT_EQ(expected_response_bin, response_bin);
114 EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
115 EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
116 }
117
118 } // namespace testing
119 } // namespace grpc
120
main(int argc,char ** argv)121 int main(int argc, char** argv) {
122 grpc::testing::TestEnvironment env(&argc, argv);
123 ::testing::InitGoogleTest(&argc, argv);
124 return RUN_ALL_TESTS();
125 }
126