Debian Codegraph @

test/core/client_channel/http_proxy_mapper_test.cc

raw
1 //
2 //
3 // Copyright 2022 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 <string>
20
21 #include "absl/types/optional.h"
22 #include "gtest/gtest.h"
23
24 #include <grpc/grpc.h>
25
26 #include "src/core/ext/filters/client_channel/http_proxy.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/env.h"
29 #include "src/core/lib/transport/http_connect_handshaker.h"
30 #include "test/core/util/test_config.h"
31
32 namespace grpc_core {
33 namespace testing {
34 namespace {
35
36 class ScopedSetEnv {
37 public:
38 explicit ScopedSetEnv(const char* value) { SetEnv("no_proxy", value); }
39 ScopedSetEnv(const ScopedSetEnv&) = delete;
40 ScopedSetEnv& operator=(const ScopedSetEnv&) = delete;
41 ~ScopedSetEnv() { UnsetEnv("no_proxy"); }
42 };
43
44 // Test that an empty no_proxy works as expected, i.e., proxy is used.
45 TEST(NoProxyTest, EmptyList) {
46 ScopedSetEnv no_proxy("");
47 auto args = ChannelArgs().Set(GRPC_ARG_HTTP_PROXY, "http://proxy.google.com");
48 EXPECT_EQ(HttpProxyMapper().MapName("dns:///test.google.com:443", &args),
49 "proxy.google.com");
50 EXPECT_EQ(args.GetString(GRPC_ARG_HTTP_CONNECT_SERVER),
51 "test.google.com:443");
52 }
53
54 // Test basic usage of 'no_proxy' to avoid using proxy for certain domain names.
55 TEST(NoProxyTest, Basic) {
56 ScopedSetEnv no_proxy("google.com");
57 auto args = ChannelArgs().Set(GRPC_ARG_HTTP_PROXY, "http://proxy.google.com");
58 EXPECT_EQ(HttpProxyMapper().MapName("dns:///test.google.com:443", &args),
59 absl::nullopt);
60 EXPECT_EQ(args.GetString(GRPC_ARG_HTTP_CONNECT_SERVER), absl::nullopt);
61 }
62
63 // Test empty entries in 'no_proxy' list.
64 TEST(NoProxyTest, EmptyEntries) {
65 ScopedSetEnv no_proxy("foo.com,,google.com,,");
66 auto args = ChannelArgs().Set(GRPC_ARG_HTTP_PROXY, "http://proxy.google.com");
67 absl::optional<std::string> name_to_resolve;
68 EXPECT_EQ(HttpProxyMapper().MapName("dns:///test.google.com:443", &args),
69 absl::nullopt);
70 EXPECT_EQ(args.GetString(GRPC_ARG_HTTP_CONNECT_SERVER), absl::nullopt);
71 }
72
73 } // namespace
74 } // namespace testing
75 } // namespace grpc_core
76
77 int main(int argc, char** argv) {
78 ::testing::InitGoogleTest(&argc, argv);
79 grpc::testing::TestEnvironment env(&argc, argv);
80 auto result = RUN_ALL_TESTS();
81 return result;
82 }