client_context_test_peer.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H
  19. #define GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H
  20. #include <map>
  21. #include <grpcpp/client_context.h>
  22. namespace grpc {
  23. namespace testing {
  24. /// A test-only class to access private members and methods of ClientContext.
  25. class ClientContextTestPeer {
  26. public:
  27. explicit ClientContextTestPeer(ClientContext* const ctx) : ctx_(ctx) {}
  28. /// Inject metadata to the ClientContext for the test. The test peer
  29. /// must be alive when a ClientContext::GetServerInitialMetadata is called.
  30. void AddServerInitialMetadata(const TString& key,
  31. const TString& value) {
  32. server_initial_metadata_storage_.insert(
  33. std::pair<TString, TString>(key, value));
  34. ctx_->initial_metadata_received_ = true;
  35. ctx_->recv_initial_metadata_.map()->clear();
  36. for (const auto& item : server_initial_metadata_storage_) {
  37. ctx_->recv_initial_metadata_.map()->insert(
  38. std::pair<grpc::string_ref, grpc::string_ref>(
  39. item.first.c_str(),
  40. grpc::string_ref(item.second.data(), item.second.size())));
  41. }
  42. }
  43. std::multimap<TString, TString> GetSendInitialMetadata() const {
  44. return ctx_->send_initial_metadata_;
  45. }
  46. private:
  47. ClientContext* const ctx_; // not owned
  48. std::multimap<TString, TString> server_initial_metadata_storage_;
  49. };
  50. } // namespace testing
  51. } // namespace grpc
  52. #endif // GRPCPP_TEST_CLIENT_CONTEXT_TEST_PEER_H