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