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