OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include "test/cpp/util/cli_call.h" |
| 35 |
| 36 #include <grpc/grpc.h> |
| 37 #include <grpc++/channel.h> |
| 38 #include <grpc++/client_context.h> |
| 39 #include <grpc++/create_channel.h> |
| 40 #include <grpc++/server.h> |
| 41 #include <grpc++/server_builder.h> |
| 42 #include <grpc++/server_context.h> |
| 43 #include <gtest/gtest.h> |
| 44 |
| 45 #include "test/core/util/port.h" |
| 46 #include "test/core/util/test_config.h" |
| 47 #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 48 #include "test/cpp/util/string_ref_helper.h" |
| 49 |
| 50 using grpc::testing::EchoRequest; |
| 51 using grpc::testing::EchoResponse; |
| 52 |
| 53 namespace grpc { |
| 54 namespace testing { |
| 55 |
| 56 class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
| 57 public: |
| 58 Status Echo(ServerContext* context, const EchoRequest* request, |
| 59 EchoResponse* response) GRPC_OVERRIDE { |
| 60 if (!context->client_metadata().empty()) { |
| 61 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator |
| 62 iter = context->client_metadata().begin(); |
| 63 iter != context->client_metadata().end(); ++iter) { |
| 64 context->AddInitialMetadata(ToString(iter->first), |
| 65 ToString(iter->second)); |
| 66 } |
| 67 } |
| 68 context->AddTrailingMetadata("trailing_key", "trailing_value"); |
| 69 response->set_message(request->message()); |
| 70 return Status::OK; |
| 71 } |
| 72 }; |
| 73 |
| 74 class CliCallTest : public ::testing::Test { |
| 75 protected: |
| 76 CliCallTest() {} |
| 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_); |
| 86 server_ = builder.BuildAndStart(); |
| 87 } |
| 88 |
| 89 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
| 90 |
| 91 void ResetStub() { |
| 92 channel_ = |
| 93 CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
| 94 stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 95 } |
| 96 |
| 97 std::shared_ptr<Channel> channel_; |
| 98 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 99 std::unique_ptr<Server> server_; |
| 100 std::ostringstream server_address_; |
| 101 TestServiceImpl service_; |
| 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; |
| 113 context.AddMetadata("key1", "val1"); |
| 114 Status s = stub_->Echo(&context, request, &response); |
| 115 EXPECT_EQ(response.message(), request.message()); |
| 116 EXPECT_TRUE(s.ok()); |
| 117 |
| 118 const grpc::string kMethod("/grpc.testing.EchoTestService/Echo"); |
| 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)); |
| 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; |
| 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); |
| 129 EXPECT_TRUE(s2.ok()); |
| 130 |
| 131 EXPECT_EQ(expected_response_bin, response_bin); |
| 132 EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata); |
| 133 EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata); |
| 134 } |
| 135 |
| 136 } // namespace testing |
| 137 } // namespace grpc |
| 138 |
| 139 int main(int argc, char** argv) { |
| 140 grpc_test_init(argc, argv); |
| 141 ::testing::InitGoogleTest(&argc, argv); |
| 142 return RUN_ALL_TESTS(); |
| 143 } |
OLD | NEW |