server_context_test_spouse.h 2.0 KB

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