From dda3463944d9be2b97c2186ee25bab863f664c87 Mon Sep 17 00:00:00 2001 From: Arthur Passos Date: Tue, 10 Oct 2023 11:44:06 -0300 Subject: [PATCH] add some tests --- ..._proxy_configuration_resolver_provider.cpp | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/Common/tests/gtest_proxy_configuration_resolver_provider.cpp b/src/Common/tests/gtest_proxy_configuration_resolver_provider.cpp index 884040fcf52..bd6e08522c4 100644 --- a/src/Common/tests/gtest_proxy_configuration_resolver_provider.cpp +++ b/src/Common/tests/gtest_proxy_configuration_resolver_provider.cpp @@ -120,4 +120,78 @@ TEST_F(ProxyConfigurationResolverProviderTests, ListBoth) ASSERT_EQ(https_proxy_configuration.port, https_list_proxy_server.getPort()); } +TEST_F(ProxyConfigurationResolverProviderTests, RemoteResolverIsBasedOnProtocolConfigurationHTTP) +{ + /* + * Since there is no way to call `ProxyConfigurationResolver::resolve` on remote resolver, + * it is hard to verify the remote resolver was actually picked. One hackish way to assert + * the remote resolver was OR was not picked based on the configuration, is to use the + * environment resolver. Since the environment resolver is always returned as a fallback, + * we can assert the remote resolver was not picked if `ProxyConfigurationResolver::resolve` + * succeeds and returns an environment proxy configuration. + * */ + EnvironmentProxySetter setter(http_env_proxy_server, https_env_proxy_server); + + ConfigurationPtr config = Poco::AutoPtr(new Poco::Util::MapConfiguration()); + + config->setString("proxy", ""); + config->setString("proxy.https", ""); + config->setString("proxy.https.resolver", ""); + config->setString("proxy.https.resolver.endpoint", "http://resolver:8080/hostname"); + + // even tho proxy protocol / scheme is http, it should not be picked (prior to this PR, it would be picked) + config->setString("proxy.https.resolver.proxy_scheme", "http"); + config->setString("proxy.https.resolver.proxy_port", "80"); + config->setString("proxy.https.resolver.proxy_cache_time", "10"); + + context->setConfig(config); + + auto http_proxy_configuration = DB::ProxyConfigurationResolverProvider::get(DB::ProxyConfiguration::Protocol::HTTP, *config)->resolve(); + + /* + * Asserts env proxy is used and not the remote resolver. If the remote resolver is picked, it is an error because + * there is no `http` specification for remote resolver + * */ + ASSERT_EQ(http_proxy_configuration.host, http_env_proxy_server.getHost()); + ASSERT_EQ(http_proxy_configuration.port, http_env_proxy_server.getPort()); + ASSERT_EQ(http_proxy_configuration.protocol, DB::ProxyConfiguration::protocolFromString(http_env_proxy_server.getScheme())); +} + +TEST_F(ProxyConfigurationResolverProviderTests, RemoteResolverIsBasedOnProtocolConfigurationHTTPS) +{ + /* + * Since there is no way to call `ProxyConfigurationResolver::resolve` on remote resolver, + * it is hard to verify the remote resolver was actually picked. One hackish way to assert + * the remote resolver was OR was not picked based on the configuration, is to use the + * environment resolver. Since the environment resolver is always returned as a fallback, + * we can assert the remote resolver was not picked if `ProxyConfigurationResolver::resolve` + * succeeds and returns an environment proxy configuration. + * */ + EnvironmentProxySetter setter(http_env_proxy_server, https_env_proxy_server); + + ConfigurationPtr config = Poco::AutoPtr(new Poco::Util::MapConfiguration()); + + config->setString("proxy", ""); + config->setString("proxy.http", ""); + config->setString("proxy.http.resolver", ""); + config->setString("proxy.http.resolver.endpoint", "http://resolver:8080/hostname"); + + // even tho proxy protocol / scheme is https, it should not be picked (prior to this PR, it would be picked) + config->setString("proxy.http.resolver.proxy_scheme", "https"); + config->setString("proxy.http.resolver.proxy_port", "80"); + config->setString("proxy.http.resolver.proxy_cache_time", "10"); + + context->setConfig(config); + + auto http_proxy_configuration = DB::ProxyConfigurationResolverProvider::get(DB::ProxyConfiguration::Protocol::HTTPS, *config)->resolve(); + + /* + * Asserts env proxy is used and not the remote resolver. If the remote resolver is picked, it is an error because + * there is no `http` specification for remote resolver + * */ + ASSERT_EQ(http_proxy_configuration.host, https_env_proxy_server.getHost()); + ASSERT_EQ(http_proxy_configuration.port, https_env_proxy_server.getPort()); + ASSERT_EQ(http_proxy_configuration.protocol, DB::ProxyConfiguration::protocolFromString(https_env_proxy_server.getScheme())); +} + // remote resolver is tricky to be tested in unit tests