aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/util
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-08-26 08:22:42 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-08-26 08:22:42 -0700
commitafbbaf9c3e21b6a4fe41242df9d052e077dc25c0 (patch)
tree155223718dd74cd94657fbd422275f052a800e7b /test/cpp/util
parentf1c76741e8b006964679acdf34e6831e80f39766 (diff)
parent9f8c100ea86aa439e1200d2111d22354f52351cf (diff)
Merge pull request #3065 from yang-g/string_ref
Use string_ref for incoming metadata
Diffstat (limited to 'test/cpp/util')
-rw-r--r--test/cpp/util/cli_call.cc10
-rw-r--r--test/cpp/util/cli_call.h12
-rw-r--r--test/cpp/util/cli_call_test.cc13
-rw-r--r--test/cpp/util/grpc_cli.cc22
-rw-r--r--test/cpp/util/string_ref_helper.cc44
-rw-r--r--test/cpp/util/string_ref_helper.h47
6 files changed, 126 insertions, 22 deletions
diff --git a/test/cpp/util/cli_call.cc b/test/cpp/util/cli_call.cc
index d60cee9c02..9a769848a4 100644
--- a/test/cpp/util/cli_call.cc
+++ b/test/cpp/util/cli_call.cc
@@ -51,14 +51,14 @@ void* tag(int i) { return (void*)(gpr_intptr)i; }
Status CliCall::Call(std::shared_ptr<grpc::Channel> channel,
const grpc::string& method, const grpc::string& request,
- grpc::string* response, const MetadataContainer& metadata,
- MetadataContainer* server_initial_metadata,
- MetadataContainer* server_trailing_metadata) {
+ grpc::string* response,
+ const OutgoingMetadataContainer& metadata,
+ IncomingMetadataContainer* server_initial_metadata,
+ IncomingMetadataContainer* server_trailing_metadata) {
std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel));
grpc::ClientContext ctx;
if (!metadata.empty()) {
- for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
- metadata.begin();
+ for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
iter != metadata.end(); ++iter) {
ctx.AddMetadata(iter->first, iter->second);
}
diff --git a/test/cpp/util/cli_call.h b/test/cpp/util/cli_call.h
index 7a3dcf2e9f..2fbc9618b6 100644
--- a/test/cpp/util/cli_call.h
+++ b/test/cpp/util/cli_call.h
@@ -38,18 +38,22 @@
#include <grpc++/channel.h>
#include <grpc++/support/status.h>
+#include <grpc++/support/string_ref.h>
namespace grpc {
namespace testing {
class CliCall GRPC_FINAL {
public:
- typedef std::multimap<grpc::string, grpc::string> MetadataContainer;
+ typedef std::multimap<grpc::string, grpc::string> OutgoingMetadataContainer;
+ typedef std::multimap<grpc::string_ref, grpc::string_ref>
+ IncomingMetadataContainer;
static Status Call(std::shared_ptr<grpc::Channel> channel,
const grpc::string& method, const grpc::string& request,
- grpc::string* response, const MetadataContainer& metadata,
- MetadataContainer* server_initial_metadata,
- MetadataContainer* server_trailing_metadata);
+ grpc::string* response,
+ const OutgoingMetadataContainer& metadata,
+ IncomingMetadataContainer* server_initial_metadata,
+ IncomingMetadataContainer* server_trailing_metadata);
};
} // namespace testing
diff --git a/test/cpp/util/cli_call_test.cc b/test/cpp/util/cli_call_test.cc
index 35bfad202f..111a0e9f76 100644
--- a/test/cpp/util/cli_call_test.cc
+++ b/test/cpp/util/cli_call_test.cc
@@ -47,6 +47,7 @@
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
#include "test/cpp/util/echo.grpc.pb.h"
+#include "test/cpp/util/string_ref_helper.h"
using grpc::cpp::test::util::EchoRequest;
using grpc::cpp::test::util::EchoResponse;
@@ -59,10 +60,11 @@ class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
Status Echo(ServerContext* context, const EchoRequest* request,
EchoResponse* response) GRPC_OVERRIDE {
if (!context->client_metadata().empty()) {
- for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
- context->client_metadata().begin();
+ for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
+ iter = context->client_metadata().begin();
iter != context->client_metadata().end(); ++iter) {
- context->AddInitialMetadata(iter->first, iter->second);
+ context->AddInitialMetadata(ToString(iter->first),
+ ToString(iter->second));
}
}
context->AddTrailingMetadata("trailing_key", "trailing_value");
@@ -119,8 +121,9 @@ TEST_F(CliCallTest, SimpleRpc) {
grpc::string request_bin, response_bin, expected_response_bin;
EXPECT_TRUE(request.SerializeToString(&request_bin));
EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
- std::multimap<grpc::string, grpc::string> client_metadata,
- server_initial_metadata, server_trailing_metadata;
+ std::multimap<grpc::string, grpc::string> client_metadata;
+ std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
+ server_trailing_metadata;
client_metadata.insert(std::pair<grpc::string, grpc::string>("key1", "val1"));
Status s2 = CliCall::Call(channel_, kMethod, request_bin, &response_bin,
client_metadata, &server_initial_metadata,
diff --git a/test/cpp/util/grpc_cli.cc b/test/cpp/util/grpc_cli.cc
index 746d67deeb..a4888efebe 100644
--- a/test/cpp/util/grpc_cli.cc
+++ b/test/cpp/util/grpc_cli.cc
@@ -68,8 +68,10 @@
#include <grpc++/channel.h>
#include <grpc++/create_channel.h>
#include <grpc++/credentials.h>
+#include <grpc++/support/string_ref.h>
#include "test/cpp/util/cli_call.h"
+#include "test/cpp/util/string_ref_helper.h"
#include "test/cpp/util/test_config.h"
DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls.");
@@ -104,16 +106,19 @@ void ParseMetadataFlag(
}
}
-void PrintMetadata(const std::multimap<grpc::string, grpc::string>& m,
- const grpc::string& message) {
+template <typename T>
+void PrintMetadata(const T& m, const grpc::string& message) {
if (m.empty()) {
return;
}
std::cout << message << std::endl;
- for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
- m.begin();
- iter != m.end(); ++iter) {
- std::cout << iter->first << " : " << iter->second << std::endl;
+ grpc::string pair;
+ for (typename T::const_iterator iter = m.begin(); iter != m.end(); ++iter) {
+ pair.clear();
+ pair.append(iter->first.data(), iter->first.size());
+ pair.append(" : ");
+ pair.append(iter->second.data(), iter->second.size());
+ std::cout << pair << std::endl;
}
}
@@ -157,8 +162,9 @@ int main(int argc, char** argv) {
grpc::CreateChannel(server_address, creds, grpc::ChannelArguments());
grpc::string response;
- std::multimap<grpc::string, grpc::string> client_metadata,
- server_initial_metadata, server_trailing_metadata;
+ std::multimap<grpc::string, grpc::string> client_metadata;
+ std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
+ server_trailing_metadata;
ParseMetadataFlag(&client_metadata);
PrintMetadata(client_metadata, "Sending client initial metadata:");
grpc::Status s = grpc::testing::CliCall::Call(
diff --git a/test/cpp/util/string_ref_helper.cc b/test/cpp/util/string_ref_helper.cc
new file mode 100644
index 0000000000..4eb4fe0357
--- /dev/null
+++ b/test/cpp/util/string_ref_helper.cc
@@ -0,0 +1,44 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "test/cpp/util/string_ref_helper.h"
+
+namespace grpc {
+namespace testing {
+
+grpc::string ToString(const grpc::string_ref& r) {
+ return grpc::string(r.data(), r.size());
+}
+
+} // namespace testing
+} // namespace grpc
diff --git a/test/cpp/util/string_ref_helper.h b/test/cpp/util/string_ref_helper.h
new file mode 100644
index 0000000000..ac94bcd018
--- /dev/null
+++ b/test/cpp/util/string_ref_helper.h
@@ -0,0 +1,47 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_TEST_CPP_UTIL_STRING_REF_HELPER_H
+#define GRPC_TEST_CPP_UTIL_STRING_REF_HELPER_H
+
+#include <grpc++/support/string_ref.h>
+
+namespace grpc {
+namespace testing {
+
+grpc::string ToString(const grpc::string_ref& r);
+
+} // namespace testing
+} // namespace grpc
+
+#endif // GRPC_TEST_CPP_UTIL_STRING_REF_HELPER_H