client_helper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. *
  3. * Copyright 2015 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 GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H
  19. #define GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H
  20. #include <functional>
  21. #include <memory>
  22. #include <unordered_map>
  23. #include <grpcpp/channel.h>
  24. #include <grpcpp/client_context.h>
  25. #include "src/core/lib/surface/call_test_only.h"
  26. namespace grpc {
  27. namespace testing {
  28. grpc::string GetServiceAccountJsonKey();
  29. grpc::string GetOauth2AccessToken();
  30. void UpdateActions(
  31. std::unordered_map<grpc::string, std::function<bool()>>* actions);
  32. std::shared_ptr<Channel> CreateChannelForTestCase(
  33. const grpc::string& test_case,
  34. std::vector<
  35. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  36. interceptor_creators = {});
  37. // Parse the contents of FLAGS_additional_metadata into a map. Allow
  38. // alphanumeric characters and dashes in keys, and any character but semicolons
  39. // in values.
  40. std::multimap<grpc::string, grpc::string> ParseAdditionalMetadataFlag(
  41. const grpc::string& flag);
  42. class InteropClientContextInspector {
  43. public:
  44. InteropClientContextInspector(const ::grpc::ClientContext& context)
  45. : context_(context) {}
  46. // Inspector methods, able to peek inside ClientContext, follow.
  47. grpc_compression_algorithm GetCallCompressionAlgorithm() const {
  48. return grpc_call_test_only_get_compression_algorithm(context_.call_);
  49. }
  50. uint32_t GetMessageFlags() const {
  51. return grpc_call_test_only_get_message_flags(context_.call_);
  52. }
  53. private:
  54. const ::grpc::ClientContext& context_;
  55. };
  56. class AdditionalMetadataInterceptor : public experimental::Interceptor {
  57. public:
  58. AdditionalMetadataInterceptor(
  59. std::multimap<grpc::string, grpc::string> additional_metadata)
  60. : additional_metadata_(std::move(additional_metadata)) {}
  61. void Intercept(experimental::InterceptorBatchMethods* methods) override;
  62. private:
  63. const std::multimap<grpc::string, grpc::string> additional_metadata_;
  64. };
  65. class AdditionalMetadataInterceptorFactory
  66. : public experimental::ClientInterceptorFactoryInterface {
  67. public:
  68. AdditionalMetadataInterceptorFactory(
  69. std::multimap<grpc::string, grpc::string> additional_metadata)
  70. : additional_metadata_(std::move(additional_metadata)) {}
  71. experimental::Interceptor* CreateClientInterceptor(
  72. experimental::ClientRpcInfo* info) override {
  73. return new AdditionalMetadataInterceptor(additional_metadata_);
  74. }
  75. const std::multimap<grpc::string, grpc::string> additional_metadata_;
  76. };
  77. } // namespace testing
  78. } // namespace grpc
  79. #endif // GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H