1 // 2 // 3 // Copyright 2021 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H 20 #define GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H 21 22 #include <map> 23 24 #include <grpcpp/client_context.h> 25 26 namespace grpc { 27 namespace testing { 28 29 /// A test-only class to access private members and methods of ClientContext. 30 class ClientContextTestPeer { 31 public: ClientContextTestPeer(ClientContext * const ctx)32 explicit ClientContextTestPeer(ClientContext* const ctx) : ctx_(ctx) {} 33 34 /// Inject metadata to the ClientContext for the test. The test peer 35 /// must be alive when a ClientContext::GetServerInitialMetadata is called. AddServerInitialMetadata(const std::string & key,const std::string & value)36 void AddServerInitialMetadata(const std::string& key, 37 const std::string& value) { 38 server_initial_metadata_storage_.insert( 39 std::pair<std::string, std::string>(key, value)); 40 ctx_->initial_metadata_received_ = true; 41 ctx_->recv_initial_metadata_.map()->clear(); 42 for (const auto& item : server_initial_metadata_storage_) { 43 ctx_->recv_initial_metadata_.map()->insert( 44 std::pair<grpc::string_ref, grpc::string_ref>( 45 item.first.c_str(), 46 grpc::string_ref(item.second.data(), item.second.size()))); 47 } 48 } 49 GetSendInitialMetadata()50 std::multimap<std::string, std::string> GetSendInitialMetadata() const { 51 return ctx_->send_initial_metadata_; 52 } 53 54 private: 55 ClientContext* const ctx_; // not owned 56 std::multimap<std::string, std::string> server_initial_metadata_storage_; 57 }; 58 59 } // namespace testing 60 } // namespace grpc 61 62 #endif // GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H 63