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