1 //
2 //
3 // Copyright 2016 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/server_context_test_spouse.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
ClientMetadataContains(const ServerContext & context,const grpc::string_ref & key,const grpc::string_ref & value)34 bool ClientMetadataContains(const ServerContext& context,
35 const grpc::string_ref& key,
36 const grpc::string_ref& value) {
37 const auto& client_metadata = context.client_metadata();
38 for (auto iter = client_metadata.begin(); iter != client_metadata.end();
39 ++iter) {
40 if (iter->first == key && iter->second == value) {
41 return true;
42 }
43 }
44 return false;
45 }
46
TEST(ServerContextTestSpouseTest,ClientMetadataHandle)47 TEST(ServerContextTestSpouseTest, ClientMetadataHandle) {
48 ServerContext context;
49 ServerContextTestSpouse spouse(&context);
50
51 spouse.AddClientMetadata(key1, val1);
52 ASSERT_TRUE(ClientMetadataContains(context, key1, val1));
53
54 spouse.AddClientMetadata(key2, val2);
55 ASSERT_TRUE(ClientMetadataContains(context, key1, val1));
56 ASSERT_TRUE(ClientMetadataContains(context, key2, val2));
57 }
58
TEST(ServerContextTestSpouseTest,InitialMetadata)59 TEST(ServerContextTestSpouseTest, InitialMetadata) {
60 ServerContext context;
61 ServerContextTestSpouse spouse(&context);
62 std::multimap<std::string, std::string> metadata;
63
64 context.AddInitialMetadata(key1, val1);
65 metadata.insert(std::pair<std::string, std::string>(key1, val1));
66 ASSERT_EQ(metadata, spouse.GetInitialMetadata());
67
68 context.AddInitialMetadata(key2, val2);
69 metadata.insert(std::pair<std::string, std::string>(key2, val2));
70 ASSERT_EQ(metadata, spouse.GetInitialMetadata());
71 }
72
TEST(ServerContextTestSpouseTest,ServerMetadataHandle)73 TEST(ServerContextTestSpouseTest, ServerMetadataHandle) {
74 ServerContext context;
75 ServerContextTestSpouse spouse(&context);
76 std::multimap<std::string, std::string> metadata;
77
78 context.AddTrailingMetadata(key1, val1);
79 metadata.insert(std::pair<std::string, std::string>(key1, val1));
80 ASSERT_EQ(metadata, spouse.GetTrailingMetadata());
81
82 context.AddTrailingMetadata(key2, val2);
83 metadata.insert(std::pair<std::string, std::string>(key2, val2));
84 ASSERT_EQ(metadata, spouse.GetTrailingMetadata());
85 }
86
87 } // namespace testing
88 } // namespace grpc
89
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91 ::testing::InitGoogleTest(&argc, argv);
92 return RUN_ALL_TESTS();
93 }
94