Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 4 | * 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 Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 34 | #include "test/cpp/util/cli_call.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 35 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 36 | #include <grpc++/channel.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 37 | #include <grpc++/client_context.h> |
| 38 | #include <grpc++/create_channel.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 39 | #include <grpc++/server.h> |
| 40 | #include <grpc++/server_builder.h> |
| 41 | #include <grpc++/server_context.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 42 | #include <grpc/grpc.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 43 | #include <gtest/gtest.h> |
| 44 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 45 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 46 | #include "test/core/util/port.h" |
| 47 | #include "test/core/util/test_config.h" |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 48 | #include "test/cpp/util/string_ref_helper.h" |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 49 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 50 | using grpc::testing::EchoRequest; |
| 51 | using grpc::testing::EchoResponse; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 52 | |
| 53 | namespace grpc { |
| 54 | namespace testing { |
| 55 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 56 | class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 57 | public: |
| 58 | Status Echo(ServerContext* context, const EchoRequest* request, |
| 59 | EchoResponse* response) GRPC_OVERRIDE { |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 60 | if (!context->client_metadata().empty()) { |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 61 | for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator |
| 62 | iter = context->client_metadata().begin(); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 63 | iter != context->client_metadata().end(); ++iter) { |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 64 | context->AddInitialMetadata(ToString(iter->first), |
| 65 | ToString(iter->second)); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | context->AddTrailingMetadata("trailing_key", "trailing_value"); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 69 | response->set_message(request->message()); |
| 70 | return Status::OK; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | class CliCallTest : public ::testing::Test { |
| 75 | protected: |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 76 | CliCallTest() {} |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 77 | |
| 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 Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 86 | server_ = builder.BuildAndStart(); |
| 87 | } |
| 88 | |
| 89 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
| 90 | |
| 91 | void ResetStub() { |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 92 | channel_ = |
| 93 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 94 | stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 95 | } |
| 96 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 97 | std::shared_ptr<Channel> channel_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 98 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 99 | std::unique_ptr<Server> server_; |
| 100 | std::ostringstream server_address_; |
| 101 | TestServiceImpl service_; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // Send a rpc with a normal stub and then a CliCall. Verify they match. |
| 105 | TEST_F(CliCallTest, SimpleRpc) { |
| 106 | ResetStub(); |
| 107 | // Normal stub. |
| 108 | EchoRequest request; |
| 109 | EchoResponse response; |
| 110 | request.set_message("Hello"); |
| 111 | |
| 112 | ClientContext context; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 113 | context.AddMetadata("key1", "val1"); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 114 | Status s = stub_->Echo(&context, request, &response); |
| 115 | EXPECT_EQ(response.message(), request.message()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(s.ok()); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 117 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 118 | const grpc::string kMethod("/grpc.testing.EchoTestService/Echo"); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 119 | 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-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 122 | 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 Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 125 | 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 Gao | 45bd28e | 2015-06-16 22:07:58 -0700 | [diff] [blame] | 129 | EXPECT_TRUE(s2.ok()); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 130 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 131 | EXPECT_EQ(expected_response_bin, response_bin); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 132 | EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata); |
| 133 | EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace testing |
| 137 | } // namespace grpc |
| 138 | |
| 139 | int main(int argc, char** argv) { |
| 140 | grpc_test_init(argc, argv); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 141 | ::testing::InitGoogleTest(&argc, argv); |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 142 | return RUN_ALL_TESTS(); |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 143 | } |