blob: 474ac282cecf8f517481e630cab9139e08c7184a [file] [log] [blame]
Yang Gaob946b5e2015-03-27 13:20:59 -07001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, Google Inc.
Yang Gaob946b5e2015-03-27 13:20:59 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Yang Gaob946b5e2015-03-27 13:20:59 -070034#include "test/cpp/util/cli_call.h"
yang-g9e2f90c2015-08-21 15:35:03 -070035
yang-g8c2be9f2015-08-19 16:28:09 -070036#include <grpc++/channel.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070037#include <grpc++/client_context.h>
38#include <grpc++/create_channel.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070039#include <grpc++/server.h>
40#include <grpc++/server_builder.h>
41#include <grpc++/server_context.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <grpc/grpc.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070043#include <gtest/gtest.h>
44
Craig Tillerf40df232016-03-25 13:38:14 -070045#include "src/proto/grpc/testing/echo.grpc.pb.h"
yang-g9e2f90c2015-08-21 15:35:03 -070046#include "test/core/util/port.h"
47#include "test/core/util/test_config.h"
yang-ge21908f2015-08-25 13:47:51 -070048#include "test/cpp/util/string_ref_helper.h"
Yang Gaob946b5e2015-03-27 13:20:59 -070049
Craig Tiller1b4e3302015-12-17 16:35:00 -080050using grpc::testing::EchoRequest;
51using grpc::testing::EchoResponse;
Yang Gaob946b5e2015-03-27 13:20:59 -070052
53namespace grpc {
54namespace testing {
55
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080056class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Yang Gaob946b5e2015-03-27 13:20:59 -070057 public:
58 Status Echo(ServerContext* context, const EchoRequest* request,
59 EchoResponse* response) GRPC_OVERRIDE {
Yang Gao102eccb2015-06-16 00:43:25 -070060 if (!context->client_metadata().empty()) {
yang-ge21908f2015-08-25 13:47:51 -070061 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
62 iter = context->client_metadata().begin();
Yang Gao102eccb2015-06-16 00:43:25 -070063 iter != context->client_metadata().end(); ++iter) {
yang-ge21908f2015-08-25 13:47:51 -070064 context->AddInitialMetadata(ToString(iter->first),
65 ToString(iter->second));
Yang Gao102eccb2015-06-16 00:43:25 -070066 }
67 }
68 context->AddTrailingMetadata("trailing_key", "trailing_value");
Yang Gaob946b5e2015-03-27 13:20:59 -070069 response->set_message(request->message());
70 return Status::OK;
71 }
72};
73
74class CliCallTest : public ::testing::Test {
75 protected:
Vijay Paie8a7e302015-08-24 10:52:33 -070076 CliCallTest() {}
Yang Gaob946b5e2015-03-27 13:20:59 -070077
78 void SetUp() GRPC_OVERRIDE {
79 int port = grpc_pick_unused_port_or_die();
80 server_address_ << "localhost:" << port;
81 // Setup server
82 ServerBuilder builder;
83 builder.AddListeningPort(server_address_.str(),
84 InsecureServerCredentials());
85 builder.RegisterService(&service_);
Yang Gaob946b5e2015-03-27 13:20:59 -070086 server_ = builder.BuildAndStart();
87 }
88
89 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
90
91 void ResetStub() {
Julien Boeufe5adc0e2015-10-12 14:08:10 -070092 channel_ =
93 CreateChannel(server_address_.str(), InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080094 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
Yang Gaob946b5e2015-03-27 13:20:59 -070095 }
96
yang-g8c2be9f2015-08-19 16:28:09 -070097 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080098 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Yang Gaob946b5e2015-03-27 13:20:59 -070099 std::unique_ptr<Server> server_;
100 std::ostringstream server_address_;
101 TestServiceImpl service_;
Yang Gaob946b5e2015-03-27 13:20:59 -0700102};
103
104// Send a rpc with a normal stub and then a CliCall. Verify they match.
105TEST_F(CliCallTest, SimpleRpc) {
106 ResetStub();
107 // Normal stub.
108 EchoRequest request;
109 EchoResponse response;
110 request.set_message("Hello");
111
112 ClientContext context;
Yang Gao102eccb2015-06-16 00:43:25 -0700113 context.AddMetadata("key1", "val1");
Yang Gaob946b5e2015-03-27 13:20:59 -0700114 Status s = stub_->Echo(&context, request, &response);
115 EXPECT_EQ(response.message(), request.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700116 EXPECT_TRUE(s.ok());
Yang Gaob946b5e2015-03-27 13:20:59 -0700117
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800118 const grpc::string kMethod("/grpc.testing.EchoTestService/Echo");
Yang Gaob946b5e2015-03-27 13:20:59 -0700119 grpc::string request_bin, response_bin, expected_response_bin;
120 EXPECT_TRUE(request.SerializeToString(&request_bin));
121 EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
yang-ge21908f2015-08-25 13:47:51 -0700122 std::multimap<grpc::string, grpc::string> client_metadata;
123 std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
124 server_trailing_metadata;
Yang Gao102eccb2015-06-16 00:43:25 -0700125 client_metadata.insert(std::pair<grpc::string, grpc::string>("key1", "val1"));
126 Status s2 = CliCall::Call(channel_, kMethod, request_bin, &response_bin,
127 client_metadata, &server_initial_metadata,
128 &server_trailing_metadata);
Yang Gao45bd28e2015-06-16 22:07:58 -0700129 EXPECT_TRUE(s2.ok());
Yang Gao102eccb2015-06-16 00:43:25 -0700130
Yang Gaob946b5e2015-03-27 13:20:59 -0700131 EXPECT_EQ(expected_response_bin, response_bin);
Yang Gao102eccb2015-06-16 00:43:25 -0700132 EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
133 EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
Yang Gaob946b5e2015-03-27 13:20:59 -0700134}
135
136} // namespace testing
137} // namespace grpc
138
139int main(int argc, char** argv) {
140 grpc_test_init(argc, argv);
Yang Gaob946b5e2015-03-27 13:20:59 -0700141 ::testing::InitGoogleTest(&argc, argv);
Yang Gaoc4b6ffb2015-04-23 16:35:24 -0700142 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800143}