• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2024 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "src/core/client_channel/lb_metadata.h"
18 
19 #include <string>
20 
21 #include "absl/strings/string_view.h"
22 #include "gtest/gtest.h"
23 #include "src/core/lib/slice/slice.h"
24 #include "src/core/lib/transport/metadata_batch.h"
25 #include "test/core/test_util/test_config.h"
26 
27 namespace grpc_core {
28 namespace testing {
29 namespace {
30 
TEST(LbMetadataMutation,SetsUnknownHeader)31 TEST(LbMetadataMutation, SetsUnknownHeader) {
32   grpc_metadata_batch metadata;
33   LoadBalancingPolicy::MetadataMutations mutations;
34   mutations.Set("key", "value");
35   MetadataMutationHandler::Apply(mutations, &metadata);
36   std::string buffer;
37   EXPECT_EQ(metadata.GetStringValue("key", &buffer), "value");
38 }
39 
TEST(LbMetadataMutation,SetsTraitHeader)40 TEST(LbMetadataMutation, SetsTraitHeader) {
41   grpc_metadata_batch metadata;
42   LoadBalancingPolicy::MetadataMutations mutations;
43   mutations.Set("user-agent", "value");
44   MetadataMutationHandler::Apply(mutations, &metadata);
45   std::string buffer;
46   EXPECT_EQ(metadata.GetStringValue("user-agent", &buffer), "value");
47 }
48 
TEST(LbMetadataMutation,OverwritesExistingHeader)49 TEST(LbMetadataMutation, OverwritesExistingHeader) {
50   grpc_metadata_batch metadata;
51   metadata.Append("key", Slice::FromCopiedString("value1"),
52                   [&](absl::string_view error, const Slice& value) {
53                     FAIL() << error << " value:" << value.as_string_view();
54                   });
55   metadata.Append("key", Slice::FromCopiedString("value2"),
56                   [&](absl::string_view error, const Slice& value) {
57                     FAIL() << error << " value:" << value.as_string_view();
58                   });
59   std::string buffer;
60   EXPECT_EQ(metadata.GetStringValue("key", &buffer), "value1,value2");
61   LoadBalancingPolicy::MetadataMutations mutations;
62   mutations.Set("key", "value3");
63   MetadataMutationHandler::Apply(mutations, &metadata);
64   EXPECT_EQ(metadata.GetStringValue("key", &buffer), "value3");
65 }
66 
TEST(LbMetadataMutation,OverwritesTraitHeader)67 TEST(LbMetadataMutation, OverwritesTraitHeader) {
68   grpc_metadata_batch metadata;
69   metadata.Append("user-agent", Slice::FromCopiedString("value1"),
70                   [&](absl::string_view error, const Slice& value) {
71                     FAIL() << error << " value:" << value.as_string_view();
72                   });
73   std::string buffer;
74   EXPECT_EQ(metadata.GetStringValue("user-agent", &buffer), "value1");
75   LoadBalancingPolicy::MetadataMutations mutations;
76   mutations.Set("user-agent", "value2");
77   MetadataMutationHandler::Apply(mutations, &metadata);
78   EXPECT_EQ(metadata.GetStringValue("user-agent", &buffer), "value2");
79 }
80 
81 }  // namespace
82 }  // namespace testing
83 }  // namespace grpc_core
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   ::testing::InitGoogleTest(&argc, argv);
87   grpc::testing::TestEnvironment env(&argc, argv);
88   auto result = RUN_ALL_TESTS();
89   return result;
90 }
91