Debian Codegraph @

test/cpp/interop/client_helper.h

raw
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/transport.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:
50 explicit InteropClientContextInspector(const grpc::ClientContext& context)
51 : context_(context) {}
52
53 // Inspector methods, able to peek inside ClientContext, follow.
54 grpc_compression_algorithm GetCallCompressionAlgorithm() const {
55 return grpc_call_test_only_get_compression_algorithm(context_.call_);
56 }
57
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:
71 explicit AdditionalMetadataInterceptor(
72 std::multimap<std::string, std::string> additional_metadata)
73 : additional_metadata_(std::move(additional_metadata)) {}
74
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:
94 explicit AdditionalMetadataInterceptorFactory(
95 std::multimap<std::string, std::string> additional_metadata)
96 : additional_metadata_(std::move(additional_metadata)) {}
97
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 class MetadataAndStatusLoggerInterceptor : public experimental::Interceptor {
107 public:
108 explicit MetadataAndStatusLoggerInterceptor() {}
109
110 void Intercept(experimental::InterceptorBatchMethods* methods) override;
111 };
112
113 class MetadataAndStatusLoggerInterceptorFactory
114 : public experimental::ClientInterceptorFactoryInterface {
115 public:
116 explicit MetadataAndStatusLoggerInterceptorFactory() {}
117
118 experimental::Interceptor* CreateClientInterceptor(
119 experimental::ClientRpcInfo* /*info*/) override {
120 return new MetadataAndStatusLoggerInterceptor();
121 }
122 };
123
124 } // namespace testing
125 } // namespace grpc
126
127 #endif // GRPC_TEST_CPP_INTEROP_CLIENT_HELPER_H