http_proxy_mapper_test.cc
Go to the documentation of this file.
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 <gmock/gmock.h>
20 
23 #include "src/core/lib/gpr/env.h"
26 
27 namespace grpc_core {
28 namespace testing {
29 namespace {
30 
31 // Test that an empty no_proxy works as expected, i.e., proxy is used.
32 TEST(NoProxyTest, EmptyList) {
33  gpr_setenv("no_proxy", "");
35  const_cast<char*>(GRPC_ARG_HTTP_PROXY),
36  const_cast<char*>("http://proxy.google.com"));
37  grpc_channel_args args = {1, &proxy_arg};
38  grpc_channel_args* new_args = nullptr;
39  char* name_to_resolve = nullptr;
40  EXPECT_TRUE(HttpProxyMapper().MapName("dns:///test.google.com:443", &args,
41  &name_to_resolve, &new_args));
42  EXPECT_STREQ(name_to_resolve, "proxy.google.com");
44  new_args, const_cast<char*>(GRPC_ARG_HTTP_CONNECT_SERVER)),
45  "test.google.com:443");
46  gpr_free(name_to_resolve);
47  grpc_channel_args_destroy(new_args);
48  gpr_unsetenv("no_proxy");
49 }
50 
51 // Test basic usage of 'no_proxy' to avoid using proxy for certain domain names.
52 TEST(NoProxyTest, Basic) {
53  gpr_setenv("no_proxy", "google.com");
55  const_cast<char*>(GRPC_ARG_HTTP_PROXY),
56  const_cast<char*>("http://proxy.google.com"));
57  grpc_channel_args args = {1, &proxy_arg};
58  grpc_channel_args* new_args = nullptr;
59  char* name_to_resolve = nullptr;
60  EXPECT_FALSE(HttpProxyMapper().MapName("dns:///test.google.com:443", &args,
61  &name_to_resolve, &new_args));
62  EXPECT_EQ(name_to_resolve, nullptr);
64  new_args, const_cast<char*>(GRPC_ARG_HTTP_CONNECT_SERVER)),
65  nullptr);
66  gpr_free(name_to_resolve);
67  grpc_channel_args_destroy(new_args);
68  gpr_unsetenv("no_proxy");
69 }
70 
71 // Test empty entries in 'no_proxy' list.
72 TEST(NoProxyTest, EmptyEntries) {
73  gpr_setenv("no_proxy", "foo.com,,google.com,,");
75  const_cast<char*>(GRPC_ARG_HTTP_PROXY),
76  const_cast<char*>("http://proxy.google.com"));
77  grpc_channel_args args = {1, &proxy_arg};
78  grpc_channel_args* new_args = nullptr;
79  char* name_to_resolve = nullptr;
80  EXPECT_FALSE(HttpProxyMapper().MapName("dns:///test.google.com:443", &args,
81  &name_to_resolve, &new_args));
82  EXPECT_EQ(name_to_resolve, nullptr);
84  new_args, const_cast<char*>(GRPC_ARG_HTTP_CONNECT_SERVER)),
85  nullptr);
86  gpr_free(name_to_resolve);
87  grpc_channel_args_destroy(new_args);
88  gpr_unsetenv("no_proxy");
89 }
90 
91 } // namespace
92 } // namespace testing
93 } // namespace grpc_core
94 
95 int main(int argc, char** argv) {
96  ::testing::InitGoogleTest(&argc, argv);
98  auto result = RUN_ALL_TESTS();
99  return result;
100 }
grpc_arg
Definition: grpc_types.h:103
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
grpc_channel_args_find_string
char * grpc_channel_args_find_string(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:441
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
main
int main(int argc, char **argv)
Definition: http_proxy_mapper_test.cc:95
generate.env
env
Definition: generate.py:37
http_proxy.h
grpc_core
Definition: call_metric_recorder.h:31
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
env.h
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
grpc_channel_args
Definition: grpc_types.h:132
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GRPC_ARG_HTTP_PROXY
#define GRPC_ARG_HTTP_PROXY
Definition: grpc_types.h:418
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
http_connect_handshaker.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
channel_args.h
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
gpr_unsetenv
void gpr_unsetenv(const char *name)
gpr_setenv
void gpr_setenv(const char *name, const char *value)
GRPC_ARG_HTTP_CONNECT_SERVER
#define GRPC_ARG_HTTP_CONNECT_SERVER
Definition: http_connect_handshaker.h:28


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:02