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