xref: /aosp_15_r20/external/grpc-grpc/test/cpp/test/client_context_test_peer_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #include <cstring>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 
24 #include <grpcpp/impl/grpc_library.h>
25 #include <grpcpp/test/client_context_test_peer.h>
26 
27 namespace grpc {
28 namespace testing {
29 
30 const char key1[] = "metadata-key1";
31 const char key2[] = "metadata-key2";
32 const char val1[] = "metadata-val1";
33 const char val2[] = "metadata-val2";
34 
ServerInitialMetadataContains(const ClientContext & context,const grpc::string_ref & key,const grpc::string_ref & value)35 bool ServerInitialMetadataContains(const ClientContext& context,
36                                    const grpc::string_ref& key,
37                                    const grpc::string_ref& value) {
38   const auto& server_metadata = context.GetServerInitialMetadata();
39   for (auto iter = server_metadata.begin(); iter != server_metadata.end();
40        ++iter) {
41     if (iter->first == key && iter->second == value) {
42       return true;
43     }
44   }
45   return true;
46 }
47 
TEST(ClientContextTestPeerTest,AddServerInitialMetadata)48 TEST(ClientContextTestPeerTest, AddServerInitialMetadata) {
49   ClientContext context;
50   ClientContextTestPeer peer(&context);
51 
52   peer.AddServerInitialMetadata(key1, val1);
53   ASSERT_TRUE(ServerInitialMetadataContains(context, key1, val1));
54   peer.AddServerInitialMetadata(key2, val2);
55   ASSERT_TRUE(ServerInitialMetadataContains(context, key1, val1));
56   ASSERT_TRUE(ServerInitialMetadataContains(context, key2, val2));
57 }
58 
TEST(ClientContextTestPeerTest,GetSendInitialMetadata)59 TEST(ClientContextTestPeerTest, GetSendInitialMetadata) {
60   ClientContext context;
61   ClientContextTestPeer peer(&context);
62   std::multimap<std::string, std::string> metadata;
63 
64   context.AddMetadata(key1, val1);
65   metadata.insert(std::pair<std::string, std::string>(key1, val1));
66   ASSERT_EQ(metadata, peer.GetSendInitialMetadata());
67 
68   context.AddMetadata(key2, val2);
69   metadata.insert(std::pair<std::string, std::string>(key2, val2));
70   ASSERT_EQ(metadata, peer.GetSendInitialMetadata());
71 }
72 
73 }  // namespace testing
74 }  // namespace grpc
75 
main(int argc,char ** argv)76 int main(int argc, char** argv) {
77   ::testing::InitGoogleTest(&argc, argv);
78   return RUN_ALL_TESTS();
79 }
80