1 /* 2 * 3 * Copyright 2016 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_SERVER_CONTEXT_TEST_SPOUSE_H 20 #define GRPCPP_TEST_SERVER_CONTEXT_TEST_SPOUSE_H 21 22 #include <map> 23 24 #include <grpcpp/server_context.h> 25 26 namespace grpc { 27 namespace testing { 28 29 /// A test-only class to access private members and methods of ServerContext. 30 class ServerContextTestSpouse { 31 public: ServerContextTestSpouse(ServerContext * ctx)32 explicit ServerContextTestSpouse(ServerContext* ctx) : ctx_(ctx) {} 33 34 /// Inject client metadata to the ServerContext for the test. The test spouse 35 /// must be alive when \a ServerContext::client_metadata is called. AddClientMetadata(const grpc::string & key,const grpc::string & value)36 void AddClientMetadata(const grpc::string& key, const grpc::string& value) { 37 client_metadata_storage_.insert( 38 std::pair<grpc::string, grpc::string>(key, value)); 39 ctx_->client_metadata_.map()->clear(); 40 for (auto iter = client_metadata_storage_.begin(); 41 iter != client_metadata_storage_.end(); ++iter) { 42 ctx_->client_metadata_.map()->insert( 43 std::pair<grpc::string_ref, grpc::string_ref>( 44 iter->first.c_str(), 45 grpc::string_ref(iter->second.data(), iter->second.size()))); 46 } 47 } 48 GetInitialMetadata()49 std::multimap<grpc::string, grpc::string> GetInitialMetadata() const { 50 return ctx_->initial_metadata_; 51 } 52 GetTrailingMetadata()53 std::multimap<grpc::string, grpc::string> GetTrailingMetadata() const { 54 return ctx_->trailing_metadata_; 55 } 56 57 private: 58 ServerContext* ctx_; // not owned 59 std::multimap<grpc::string, grpc::string> client_metadata_storage_; 60 }; 61 62 } // namespace testing 63 } // namespace grpc 64 65 #endif // GRPCPP_TEST_SERVER_CONTEXT_TEST_SPOUSE_H 66