xref: /aosp_15_r20/external/grpc-grpc/include/grpcpp/test/server_context_test_spouse.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 std::string & key,const std::string & value)36   void AddClientMetadata(const std::string& key, const std::string& value) {
37     client_metadata_storage_.insert(
38         std::pair<std::string, std::string>(key, value));
39     ctx_->client_metadata_.map()->clear();
40     for (const auto& item : client_metadata_storage_) {
41       ctx_->client_metadata_.map()->insert(
42           std::pair<grpc::string_ref, grpc::string_ref>(
43               item.first.c_str(),
44               grpc::string_ref(item.second.data(), item.second.size())));
45     }
46   }
47 
GetInitialMetadata()48   std::multimap<std::string, std::string> GetInitialMetadata() const {
49     return ctx_->initial_metadata_;
50   }
51 
GetTrailingMetadata()52   std::multimap<std::string, std::string> GetTrailingMetadata() const {
53     return ctx_->trailing_metadata_;
54   }
55 
56  private:
57   ServerContext* ctx_;  // not owned
58   std::multimap<std::string, std::string> client_metadata_storage_;
59 };
60 
61 }  // namespace testing
62 }  // namespace grpc
63 
64 #endif  // GRPCPP_TEST_SERVER_CONTEXT_TEST_SPOUSE_H
65