client_helper.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "src/core/lib/transport/byte_stream.h"
  27. namespace grpc {
  28. namespace testing {
  29. std::string GetServiceAccountJsonKey();
  30. std::string GetOauth2AccessToken();
  31. void UpdateActions(
  32. std::unordered_map<std::string, std::function<bool()>>* actions);
  33. std::shared_ptr<Channel> CreateChannelForTestCase(
  34. const std::string& test_case,
  35. std::vector<
  36. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  37. interceptor_creators = {});
  38. class InteropClientContextInspector {
  39. public:
  40. explicit InteropClientContextInspector(const grpc::ClientContext& context)
  41. : context_(context) {}
  42. // Inspector methods, able to peek inside ClientContext, follow.
  43. grpc_compression_algorithm GetCallCompressionAlgorithm() const {
  44. return grpc_call_test_only_get_compression_algorithm(context_.call_);
  45. }
  46. bool WasCompressed() const {
  47. return (grpc_call_test_only_get_message_flags(context_.call_) &
  48. GRPC_WRITE_INTERNAL_COMPRESS) ||
  49. (grpc_call_test_only_get_message_flags(context_.call_) &
  50. GRPC_WRITE_INTERNAL_TEST_ONLY_WAS_COMPRESSED);
  51. }
  52. private:
  53. const grpc::ClientContext& context_;
  54. };
  55. class AdditionalMetadataInterceptor : public experimental::Interceptor {
  56. public:
  57. explicit AdditionalMetadataInterceptor(
  58. std::multimap<std::string, std::string> additional_metadata)
  59. : additional_metadata_(std::move(additional_metadata)) {}
  60. void Intercept(experimental::InterceptorBatchMethods* methods) override {
  61. if (methods->QueryInterceptionHookPoint(
  62. experimental::InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) {
  63. std::multimap<std::string, std::string>* metadata =
  64. methods->GetSendInitialMetadata();
  65. for (const auto& entry : additional_metadata_) {
  66. metadata->insert(entry);
  67. }
  68. }
  69. methods->Proceed();
  70. }
  71. private:
  72. const std::multimap<std::string, std::string> additional_metadata_;
  73. };
  74. class AdditionalMetadataInterceptorFactory
  75. : public experimental::ClientInterceptorFactoryInterface {
  76. public:
  77. explicit AdditionalMetadataInterceptorFactory(
  78. std::multimap<std::string, std::string> additional_metadata)
  79. : additional_metadata_(std::move(additional_metadata)) {}
  80. experimental::Interceptor* CreateClientInterceptor(
  81. experimental::ClientRpcInfo* /*info*/) override {
  82. return new AdditionalMetadataInterceptor(additional_metadata_);
  83. }
  84. const std::multimap<std::string, std::string> additional_metadata_;
  85. };
  86. class MetadataAndStatusLoggerInterceptor : public experimental::Interceptor {
  87. public:
  88. explicit MetadataAndStatusLoggerInterceptor() {}
  89. void Intercept(experimental::InterceptorBatchMethods* methods) override;
  90. };
  91. class MetadataAndStatusLoggerInterceptorFactory
  92. : public experimental::ClientInterceptorFactoryInterface {
  93. public:
  94. explicit MetadataAndStatusLoggerInterceptorFactory() {}
  95. experimental::Interceptor* CreateClientInterceptor(
  96. experimental::ClientRpcInfo* /*info*/) override {
  97. return new MetadataAndStatusLoggerInterceptor();
  98. }
  99. };
  100. } // namespace testing
  101. } // namespace grpc
  102. #endif // GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H