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