From c10348a55536525dc2b0d2972208b2f746cc75ce Mon Sep 17 00:00:00 2001 From: yang-g Date: Fri, 19 Feb 2016 16:05:10 -0800 Subject: Add custom_metadata test case --- test/cpp/interop/client.cc | 6 +++- test/cpp/interop/interop_client.cc | 72 ++++++++++++++++++++++++++++++++++++++ test/cpp/interop/interop_client.h | 1 + test/cpp/interop/server.cc | 22 ++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index cb9b396beb..8e81e432e7 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -74,6 +74,7 @@ DEFINE_string(test_case, "large_unary", "oauth2_auth_token: raw oauth2 access token auth; " "per_rpc_creds: raw oauth2 access token on a single rpc; " "status_code_and_message: verify status code & message; " + "custom_metadata: server will echo custom metadata;" "all : all of above."); DEFINE_string(default_service_account, "", "Email of GCE default service account"); @@ -129,6 +130,8 @@ int main(int argc, char** argv) { client.DoPerRpcCreds(json_key); } else if (FLAGS_test_case == "status_code_and_message") { client.DoStatusWithMessage(); + } else if (FLAGS_test_case == "custom_metadata") { + client.DoCustomMetadata(); } else if (FLAGS_test_case == "all") { client.DoEmpty(); client.DoLargeUnary(); @@ -142,6 +145,7 @@ int main(int argc, char** argv) { client.DoTimeoutOnSleepingServer(); client.DoEmptyStream(); client.DoStatusWithMessage(); + client.DoCustomMetadata(); // service_account_creds and jwt_token_creds can only run with ssl. if (FLAGS_use_tls) { grpc::string json_key = GetServiceAccountJsonKey(); @@ -159,7 +163,7 @@ int main(int argc, char** argv) { "server_compressed_streaming|half_duplex|ping_pong|cancel_after_begin|" "cancel_after_first_response|timeout_on_sleeping_server|empty_stream|" "compute_engine_creds|jwt_token_creds|oauth2_auth_token|per_rpc_creds", - FLAGS_test_case.c_str()); + "status_code_and_message|custom_metadata", FLAGS_test_case.c_str()); ret = 1; } diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc index b06310781a..46f6fdac40 100644 --- a/test/cpp/interop/interop_client.cc +++ b/test/cpp/interop/interop_client.cc @@ -625,5 +625,77 @@ void InteropClient::DoStatusWithMessage() { gpr_log(GPR_DEBUG, "Done testing Status and Message"); } +void InteropClient::DoCustomMetadata() { + const grpc::string kEchoInitialMetadataKey("x-grpc-test-echo-initial"); + const grpc::string kInitialMetadataValue("test_initial_metadata_value"); + const grpc::string kEchoTrailingBinMetadataKey( + "x-grpc-test-echo-trailing-bin"); + const grpc::string kTrailingBinValue("\x0a\x0b\x0a\x0b\x0a\x0b"); + ; + + { + gpr_log(GPR_DEBUG, "Sending RPC with custom metadata"); + ClientContext context; + context.AddMetadata(kEchoInitialMetadataKey, kInitialMetadataValue); + context.AddMetadata(kEchoTrailingBinMetadataKey, kTrailingBinValue); + SimpleRequest request; + SimpleResponse response; + request.set_response_size(kLargeResponseSize); + grpc::string payload(kLargeRequestSize, '\0'); + request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); + + Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); + AssertOkOrPrintErrorStatus(s); + const auto& server_initial_metadata = context.GetServerInitialMetadata(); + auto iter = server_initial_metadata.find(kEchoInitialMetadataKey); + GPR_ASSERT(iter != server_initial_metadata.end()); + GPR_ASSERT(iter->second.data() == kInitialMetadataValue); + const auto& server_trailing_metadata = context.GetServerTrailingMetadata(); + iter = server_trailing_metadata.find(kEchoTrailingBinMetadataKey); + GPR_ASSERT(iter != server_trailing_metadata.end()); + GPR_ASSERT(grpc::string(iter->second.begin(), iter->second.end()) == + kTrailingBinValue); + + gpr_log(GPR_DEBUG, "Done testing RPC with custom metadata"); + } + + { + gpr_log(GPR_DEBUG, "Sending stream with custom metadata"); + ClientContext context; + context.AddMetadata(kEchoInitialMetadataKey, kInitialMetadataValue); + context.AddMetadata(kEchoTrailingBinMetadataKey, kTrailingBinValue); + std::unique_ptr> + stream(serviceStub_.Get()->FullDuplexCall(&context)); + + StreamingOutputCallRequest request; + request.set_response_type(PayloadType::COMPRESSABLE); + ResponseParameters* response_parameter = request.add_response_parameters(); + response_parameter->set_size(kLargeResponseSize); + grpc::string payload(kLargeRequestSize, '\0'); + request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); + StreamingOutputCallResponse response; + GPR_ASSERT(stream->Write(request)); + stream->WritesDone(); + GPR_ASSERT(stream->Read(&response)); + GPR_ASSERT(response.payload().body() == + grpc::string(kLargeResponseSize, '\0')); + GPR_ASSERT(!stream->Read(&response)); + Status s = stream->Finish(); + AssertOkOrPrintErrorStatus(s); + const auto& server_initial_metadata = context.GetServerInitialMetadata(); + auto iter = server_initial_metadata.find(kEchoInitialMetadataKey); + GPR_ASSERT(iter != server_initial_metadata.end()); + GPR_ASSERT(iter->second.data() == kInitialMetadataValue); + const auto& server_trailing_metadata = context.GetServerTrailingMetadata(); + iter = server_trailing_metadata.find(kEchoTrailingBinMetadataKey); + GPR_ASSERT(iter != server_trailing_metadata.end()); + GPR_ASSERT(grpc::string(iter->second.begin(), iter->second.end()) == + kTrailingBinValue); + + gpr_log(GPR_DEBUG, "Done testing stream with custom metadata"); + } +} + } // namespace testing } // namespace grpc diff --git a/test/cpp/interop/interop_client.h b/test/cpp/interop/interop_client.h index 3ecd380398..92f57f7bdd 100644 --- a/test/cpp/interop/interop_client.h +++ b/test/cpp/interop/interop_client.h @@ -75,6 +75,7 @@ class InteropClient { void DoTimeoutOnSleepingServer(); void DoEmptyStream(); void DoStatusWithMessage(); + void DoCustomMetadata(); // Auth tests. // username is a string containing the user email void DoJwtTokenCreds(const grpc::string& username); diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index cdca060c23..bf576322b4 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -80,6 +80,26 @@ using grpc::Status; static bool got_sigint = false; static const char* kRandomFile = "test/cpp/interop/rnd.dat"; +const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial"; +const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin"; + +void MaybeEchoMetadata(ServerContext* context) { + const auto& client_metadata = context->client_metadata(); + GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1); + GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1); + + auto iter = client_metadata.find(kEchoInitialMetadataKey); + if (iter != client_metadata.end()) { + context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data()); + } + iter = client_metadata.find(kEchoTrailingBinMetadataKey); + if (iter != client_metadata.end()) { + context->AddTrailingMetadata( + kEchoTrailingBinMetadataKey, + grpc::string(iter->second.begin(), iter->second.end())); + } +} + bool SetPayload(PayloadType type, int size, Payload* payload) { PayloadType response_type; if (type == PayloadType::RANDOM) { @@ -135,6 +155,7 @@ class TestServiceImpl : public TestService::Service { Status UnaryCall(ServerContext* context, const SimpleRequest* request, SimpleResponse* response) { + MaybeEchoMetadata(context); SetResponseCompression(context, *request); if (request->response_size() > 0) { if (!SetPayload(request->response_type(), request->response_size(), @@ -192,6 +213,7 @@ class TestServiceImpl : public TestService::Service { ServerContext* context, ServerReaderWriter* stream) { + MaybeEchoMetadata(context); StreamingOutputCallRequest request; StreamingOutputCallResponse response; bool write_success = true; -- cgit v1.2.3 From 364be644d0beba5f62bfe7f6b3a80f3836a1cd52 Mon Sep 17 00:00:00 2001 From: yang-g Date: Mon, 22 Feb 2016 10:04:43 -0800 Subject: copyright of course --- test/cpp/interop/client.cc | 2 +- test/cpp/interop/interop_client.h | 2 +- test/cpp/interop/server.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index 8e81e432e7..788adefd24 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/test/cpp/interop/interop_client.h b/test/cpp/interop/interop_client.h index 92f57f7bdd..3f57f3c733 100644 --- a/test/cpp/interop/interop_client.h +++ b/test/cpp/interop/interop_client.h @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index bf576322b4..63eca6a5ef 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without -- cgit v1.2.3