From 9f9144c4b6165a865327d0a02e97e910c5bc633f Mon Sep 17 00:00:00 2001 From: ztefanie Date: Wed, 19 Feb 2025 14:02:23 +0100 Subject: [PATCH] feat(rest): Improve http proxy --- .../client/apache/CustomApacheHttpClient.java | 10 +- .../http/base/client/apache/ProxyHandler.java | 79 ++--- .../apache/CustomApacheHttpClientTest.java | 22 +- .../base/client/apache/ProxyHandlerTest.java | 280 +++++++----------- 4 files changed, 150 insertions(+), 241 deletions(-) diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java index 1e0f01ca3a..f9bf3608c2 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClient.java @@ -24,14 +24,12 @@ import io.camunda.connector.http.base.model.HttpCommonRequest; import io.camunda.connector.http.base.model.HttpCommonResult; import java.io.IOException; -import java.net.URISyntaxException; import javax.annotation.Nullable; import org.apache.hc.client5.http.ClientProtocolException; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; -import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.util.Timeout; @@ -103,15 +101,9 @@ public HttpCommonResult execute( @Nullable ExecutionEnvironment executionEnvironment) { try { var apacheRequest = ApacheRequestFactory.get().createHttpRequest(request); - HttpHost proxy = - proxyHandler.getProxyHost( - apacheRequest.getUri().getScheme(), apacheRequest.getUri().getHost()); - var routePlanner = proxyHandler.getRoutePlanner(apacheRequest.getUri().getScheme(), proxy); var result = httpClientBuilder .setDefaultRequestConfig(getRequestConfig(request)) - .setRoutePlanner(routePlanner) - .setProxy(proxy) .setDefaultCredentialsProvider( proxyHandler.getCredentialsProvider(apacheRequest.getScheme())) .useSystemProperties() // Will fallback on system properties for unset values, @@ -132,7 +124,7 @@ public HttpCommonResult execute( String.valueOf(HttpStatus.SC_SERVER_ERROR), "An error with the HTTP protocol occurred", e); - } catch (IOException | URISyntaxException e) { + } catch (IOException e) { throw new ConnectorException( String.valueOf(HttpStatus.SC_REQUEST_TIMEOUT), "An error occurred while executing the request, or the connection was aborted", diff --git a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/ProxyHandler.java b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/ProxyHandler.java index 5b605199d5..116aa9d184 100644 --- a/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/ProxyHandler.java +++ b/connectors/http/http-base/src/main/java/io/camunda/connector/http/base/client/apache/ProxyHandler.java @@ -18,23 +18,16 @@ import io.camunda.connector.api.error.ConnectorInputException; import java.net.*; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.regex.Pattern; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; -import org.apache.hc.client5.http.impl.DefaultSchemePortResolver; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; -import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; -import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; -import org.apache.hc.client5.http.routing.HttpRoutePlanner; -import org.apache.hc.core5.http.HttpHost; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,6 +49,7 @@ record ProxyDetails( public ProxyHandler() { this.proxyConfigForProtocols = loadProxyConfig(); this.credentialsProvidersForProtocols = initializeCredentialsProviders(); + this.syncEnvVarsToSystemProperties(); } private Map loadProxyConfig() { @@ -99,7 +93,7 @@ private Optional getConfigFromEnvVars(String protocol) { System.getenv() .getOrDefault("CONNECTOR_" + protocol.toUpperCase() + "_PROXY_PASSWORD", "") .toCharArray(), - System.getenv("CONNECTOR_" + protocol.toUpperCase() + "_PROXY_NON_PROXY_HOSTS"), + System.getenv("CONNECTOR_HTTP_PROXY_NON_PROXY_HOSTS"), false)); } catch (NumberFormatException e) { throw new ConnectorInputException("Invalid proxy port in environment variables", e); @@ -135,56 +129,31 @@ public CredentialsProvider getCredentialsProvider(String protocol) { return credentialsProvidersForProtocols.getOrDefault(protocol, new BasicCredentialsProvider()); } - public HttpHost getProxyHost(String protocol, String requestUrl) { - ProxyDetails p = proxyConfigForProtocols.get(protocol); - if (p == null || doesTargetMatchNonProxy(protocol, requestUrl)) { - LOGGER.debug("No proxy used for request URL: {}", requestUrl); - return null; - } - LOGGER.debug( - "Using proxy for {} - Host: {}, Port: {}, Source: {}", - protocol, - p.host(), - p.port(), - p.sourceIsSystemProperties() ? "System Properties" : "Environment Variables"); - - return new HttpHost(p.protocol(), p.host(), p.port()); - } - - private boolean doesTargetMatchNonProxy(String protocol, String requestUri) { - ProxyDetails p = proxyConfigForProtocols.get(protocol); - if (p == null || p.nonProxyHosts() == null) { - return false; + /* + Set the system properties for the proxy settings from env vars, if needed, because + the default proxy selector does enforce a set of System Properties related to proxy settings. + https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/ProxySelector.html + */ + private void syncEnvVarsToSystemProperties() { + for (Map.Entry entry : proxyConfigForProtocols.entrySet()) { + ProxyDetails p = entry.getValue(); + if (!p.sourceIsSystemProperties()) { + setSystemPropertyIfUnset(p.protocol + ".proxyHost", p.host()); + setSystemPropertyIfUnset(p.protocol + ".proxyPort", String.valueOf(p.port())); + setSystemPropertyIfUnset(p.protocol + ".proxyUser", p.user()); + setSystemPropertyIfUnset(p.protocol + ".proxyPassword", String.valueOf(p.password())); + setSystemPropertyIfUnset( + "http.nonProxyHosts", + p.nonProxyHosts()); // The HTTPS protocol handler will use the same nonProxyHosts + // property as the HTTP protocol. See here: + // https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html#Proxies + } } - - return Arrays.stream(p.nonProxyHosts().split("\\|")) - .map( - nonProxyHost -> { - // If entry is "example.de", it should match example.de and *.example.de - if (!nonProxyHost.contains("*")) { - return "^(.*\\.)?" + Pattern.quote(nonProxyHost) + "$"; - } - - // Otherwise, process as wildcard domain - return nonProxyHost.replace(".", "\\.").replace("*", ".*"); - }) - .anyMatch(regex -> requestUri.matches(regex)); } - public HttpRoutePlanner getRoutePlanner(String protocol, HttpHost proxyHost) { - ProxyDetails p = proxyConfigForProtocols.get(protocol); - - if (p != null && p.sourceIsSystemProperties()) { - LOGGER.debug("Using system default route planner for protocol: {}", protocol); - return new SystemDefaultRoutePlanner( - DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault()); - } else if (proxyHost != null) { - LOGGER.debug( - "Using default proxy route planner for protocol: {} with proxy: {}", protocol, proxyHost); - return new DefaultProxyRoutePlanner(proxyHost); + private void setSystemPropertyIfUnset(String name, String value) { + if (System.getProperty(name) == null || System.getProperty(name).isBlank()) { + System.setProperty(name, value != null ? value : ""); } - - LOGGER.debug("No proxy route planner used for protocol: {}", protocol); - return null; } } diff --git a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java index 823ac29767..3a9bf149fe 100644 --- a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java +++ b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/CustomApacheHttpClientTest.java @@ -177,14 +177,12 @@ public static void setUp() { Testcontainers.exposeHostPorts(proxy.port()); proxyContainer.withAccessToHost(true); proxyContainer.start(); - // Set up the HttpClient to use the proxy - String proxyHost = proxyContainer.getHost(); - Integer proxyPort = proxyContainer.getMappedPort(3128); - setAllSystemProperties(proxyHost, proxyPort); proxiedApacheHttpClient = CustomApacheHttpClient.create(HttpClients.custom()); } - private static void setAllSystemProperties(String proxyHost, Integer proxyPort) { + private static void setAllSystemProperties() { + String proxyHost = proxyContainer.getHost(); + Integer proxyPort = proxyContainer.getMappedPort(3128); System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort.toString()); System.setProperty("http.nonProxyHosts", ""); @@ -208,13 +206,14 @@ private static void unsetAllSystemProperties() { @AfterAll public static void tearDown() { - proxyContainer.stop(); unsetAllSystemProperties(); + proxyContainer.stop(); proxy.stop(); } - @AfterEach + @BeforeEach public void resetProxy() { + unsetAllSystemProperties(); proxy.resetAll(); } @@ -222,6 +221,7 @@ public void resetProxy() { public void shouldReturn200_whenAuthenticationRequiredAndProvidedAsSystemProperty( WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor(get("/protected").willReturn(ok().withBody("Hello, world!"))); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); @@ -261,7 +261,6 @@ public void shouldReturn200_whenValidEnvVar( .execute( () -> { proxy.stubFor(get(path).willReturn(ok().withBody("Hello, world!"))); - unsetAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); @@ -284,6 +283,7 @@ public void shouldReturn200_whenValidEnvVar( shouldThrowException_whenAuthenticationRequiredAndNotProvidedOrInvalidAsSystemProperty( String input, WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor(get("/protected").willReturn(ok().withBody("Hello, world!"))); + setAllSystemProperties(); System.setProperty("http.proxyUser", input); System.setProperty("http.proxyPassword", input); @@ -313,7 +313,6 @@ public void shouldThrowException_whenAuthenticationRequiredAndNotProvidedAsEnvVa .execute( () -> { proxy.stubFor(get("/protected").willReturn(ok().withBody("Hello, world!"))); - unsetAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); request.setUrl(getWireMockBaseUrlWithPath(wmRuntimeInfo, "/protected")); @@ -343,6 +342,7 @@ public void shouldUseSystemProperties_WhenEnvVarAndSystemPropertiesAreProvided( .execute( () -> { proxy.stubFor(get("/protected").willReturn(ok().withBody("Hello, world!"))); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); @@ -360,6 +360,7 @@ public void shouldUseSystemProperties_WhenEnvVarAndSystemPropertiesAreProvided( @Test public void shouldReturn200_whenGetAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor(get("/path").willReturn(ok().withBody("Hello, world!"))); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.GET); @@ -376,6 +377,7 @@ public void shouldReturn200_whenGetAndProxySet(WireMockRuntimeInfo wmRuntimeInfo public void shouldReturn200_whenPostAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor( post("/path").willReturn(created().withJsonBody(new POJONode(Map.of("key1", "value1"))))); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.POST); @@ -392,6 +394,7 @@ public void shouldReturn200_whenPostAndProxySet(WireMockRuntimeInfo wmRuntimeInf public void shouldReturn200_whenPutAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor( put("/path").willReturn(ok().withJsonBody(new POJONode(Map.of("key1", "value1"))))); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.PUT); @@ -407,6 +410,7 @@ public void shouldReturn200_whenPutAndProxySet(WireMockRuntimeInfo wmRuntimeInfo @Test public void shouldReturn200_whenDeleteAndProxySet(WireMockRuntimeInfo wmRuntimeInfo) { proxy.stubFor(delete("/path").willReturn(noContent())); + setAllSystemProperties(); HttpCommonRequest request = new HttpCommonRequest(); request.setMethod(HttpMethod.DELETE); diff --git a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/ProxyHandlerTest.java b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/ProxyHandlerTest.java index 8dfb7d69a0..88c40659ad 100644 --- a/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/ProxyHandlerTest.java +++ b/connectors/http/http-base/src/test/java/io/camunda/connector/http/base/client/apache/ProxyHandlerTest.java @@ -22,19 +22,11 @@ import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables; import io.camunda.connector.api.error.ConnectorInputException; -import java.util.stream.Stream; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; -import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; -import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; -import org.apache.hc.core5.http.HttpHost; -import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; public class ProxyHandlerTest { @@ -42,184 +34,136 @@ public class ProxyHandlerTest { @BeforeEach public void setUp() { + unsetAllSystemProperties(); + proxyHandler = new ProxyHandler(); + } + + @AfterAll + public static void cleanUp() { + unsetAllSystemProperties(); + } + + public static void unsetAllSystemProperties() { System.setProperty("http.proxyHost", ""); System.setProperty("http.proxyPort", ""); + System.setProperty("http.nonProxyHosts", ""); System.setProperty("http.proxyUser", ""); System.setProperty("http.proxyPassword", ""); + System.setProperty("https.proxyHost", ""); System.setProperty("https.proxyPort", ""); System.setProperty("https.proxyUser", ""); System.setProperty("https.proxyPassword", ""); - proxyHandler = new ProxyHandler(); } - @Nested - class LoadProxyConfigTests { - @AfterEach - public void unsetAllSystemProperties() { - System.setProperty("http.proxyHost", ""); - System.setProperty("http.proxyPort", ""); - System.setProperty("http.nonProxyHosts", ""); - System.setProperty("https.proxyHost", ""); - System.setProperty("https.proxyPort", ""); - System.setProperty("https.nonProxyHosts", ""); - System.setProperty("http.proxyUser", ""); - System.setProperty("http.proxyPassword", ""); - } - - @Test - public void shouldUseCorrectProtocol_whenMultipleProtocolsSet() { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8080"); - System.setProperty("https.proxyHost", "localhost"); - System.setProperty("https.proxyPort", "8081"); - - ProxyHandler handler = new ProxyHandler(); - HttpHost httpProxyHost = handler.getProxyHost("http", "http://example.com"); - HttpHost httpsProxyHost = handler.getProxyHost("https", "https://example.com"); - - assertThat(httpProxyHost.getPort()).isEqualTo(8080); - assertThat(httpsProxyHost.getPort()).isEqualTo(8081); - } - - @Test - public void shouldThrowException_whenProxyPortInvalidInSystemProperties() { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "invalid"); - - assertThrows(ConnectorInputException.class, () -> new ProxyHandler()); - } - } + @Test + public void shouldThrowException_whenProxyPortInvalidInSystemProperties() { + System.setProperty("http.proxyHost", "localhost"); + System.setProperty("http.proxyPort", "invalid"); - @Nested - class GetProxyHostTests { - - @Test - public void shouldReturnNull_whenNoProxyConfiguredForProtocol() { - System.setProperty("https.proxyHost", "localhost"); - System.setProperty("https.proxyPort", "8080"); - HttpHost proxyHost = proxyHandler.getProxyHost("http", "http://example.com"); - - assertThat(proxyHost).isNull(); - } - - @Test - public void shouldReturnProxyHost_whenConfigured() { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8080"); - - ProxyHandler handler = new ProxyHandler(); - HttpHost proxyHost = handler.getProxyHost("http", "http://example.com"); - - assertThat(proxyHost).isNotNull(); - assertThat(proxyHost.getHostName()).isEqualTo("localhost"); - assertThat(proxyHost.getPort()).isEqualTo(8080); - } - - private static Stream provideNonProxyHostTestData() { - return Stream.of( - Arguments.of("www.example.de", "www.example.de", true), - Arguments.of("www.camunda.de", "www.camunda.de", true), - Arguments.of("www.example.de", "www.camunda.de", false), - Arguments.of("www.example.de|www.camunda.de", "www.camunda.io", false), - Arguments.of("example.de", "www.example.de", true), - Arguments.of("example.de", "api.example.de", true), - Arguments.of("example.de", "another.example.de", true), - Arguments.of("example.de", "example.com", false), - Arguments.of("example.de|camunda.de", "www.example.de", true), - Arguments.of("example.de|camunda.de", "www.camunda.de", true), - Arguments.of("example.de|camunda.de", "www.google.de", false), - Arguments.of("*.example.de", "api.example.de", true), - Arguments.of("*.example.de", "www.example.de", true), - Arguments.of("*.example.de", "example.de", false), - Arguments.of("*.example.de", "example.com", false), - Arguments.of("*.example.de|*.camunda.io", "sub.example.de", true), - Arguments.of("*.example.de|*.camunda.io", "api.camunda.io", true), - Arguments.of("*.example.de|*.camunda.io", "www.google.com", false)); - } - - @ParameterizedTest - @MethodSource("provideNonProxyHostTestData") - public void shouldReturnExpected_whenNonProxyHostsSet( - String nonProxyHosts, String requestUrl, Boolean isNull) { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8080"); - System.setProperty("http.nonProxyHosts", nonProxyHosts); - - ProxyHandler handler = new ProxyHandler(); - HttpHost proxyHost = handler.getProxyHost("http", requestUrl); - - assertThat(proxyHost).isEqualTo(isNull ? null : new HttpHost("http", "localhost", 8080)); - } + assertThrows(ConnectorInputException.class, () -> new ProxyHandler()); } - @Nested - class GetRoutePlannerTests { - - @Test - public void shouldReturnSystemDefaultRoutePlanner_whenConfiguredFromSystemProperties() { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8080"); - - ProxyHandler handler = new ProxyHandler(); - HttpHost proxyHost = handler.getProxyHost("http", "http://example.com"); - var routePlanner = handler.getRoutePlanner("http", proxyHost); - - assertThat(routePlanner).isInstanceOf(SystemDefaultRoutePlanner.class); - } - - @Test - public void shouldReturnDefaultProxyRoutePlanner_whenProxyHostProvidedFromEnvVars() - throws Exception { - restoreSystemProperties( - () -> { - withEnvironmentVariables( - "CONNECTOR_HTTP_PROXY_HOST", - "localhost", - "CONNECTOR_HTTP_PROXY_PORT", - "8080", - "CONNECTOR_HTTP_PROXY_USER", - "user", - "CONNECTOR_HTTP_PROXY_PASSWORD", - "password") - .execute( - () -> { - HttpHost proxyHost = new HttpHost("http", "localhost", 8080); - var routePlanner = proxyHandler.getRoutePlanner("http", proxyHost); - - assertThat(routePlanner).isInstanceOf(DefaultProxyRoutePlanner.class); - }); - }); - } - - @Test - public void shouldReturnNullWhenNoProxyConfigured() { - var routePlanner = proxyHandler.getRoutePlanner("http", null); - assertThat(routePlanner).isNull(); - } + @Test + public void shouldThrowException_whenProxyPortInvalidInEnvVars() throws Exception { + restoreSystemProperties( + () -> { + withEnvironmentVariables( + "CONNECTOR_HTTP_PROXY_HOST", "localhost", "CONNECTOR_HTTP_PROXY_PORT", "invalid") + .execute( + () -> { + assertThrows(ConnectorInputException.class, () -> new ProxyHandler()); + }); + }); } - @Nested - class GetCredentialsProviderTests { + @Test + public void shouldReturnCredentialsProvider_whenConfigured() { + System.setProperty("http.proxyHost", "localhost"); + System.setProperty("http.proxyPort", "8080"); + System.setProperty("http.proxyUser", "user"); + System.setProperty("http.proxyPassword", "password"); + + ProxyHandler handler = new ProxyHandler(); + CredentialsProvider provider = handler.getCredentialsProvider("http"); - @Test - public void shouldReturnCredentialsProvider_whenConfigured() { - System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8080"); - System.setProperty("http.proxyUser", "user"); - System.setProperty("http.proxyPassword", "password"); + assertThat(provider).isInstanceOf(CredentialsProvider.class); + } - ProxyHandler handler = new ProxyHandler(); - CredentialsProvider provider = handler.getCredentialsProvider("http"); + @Test + public void shouldReturnDefaultCredentialsProvider_whenNotConfigured() { + CredentialsProvider provider = proxyHandler.getCredentialsProvider("http"); - assertThat(provider).isInstanceOf(CredentialsProvider.class); - } + assertThat(provider).isInstanceOf(BasicCredentialsProvider.class); + } - @Test - public void shouldReturnDefaultCredentialsProvider_whenNotConfigured() { - CredentialsProvider provider = proxyHandler.getCredentialsProvider("http"); + @Test + public void shouldSetSystemProperties_whenProxySettingsEnvVars() throws Exception { + restoreSystemProperties( + () -> { + withEnvironmentVariables( + "CONNECTOR_HTTP_PROXY_HOST", + "localhost", + "CONNECTOR_HTTP_PROXY_PORT", + "3128", + "CONNECTOR_HTTP_PROXY_USER", + "my-user", + "CONNECTOR_HTTP_PROXY_PASSWORD", + "demo", + "CONNECTOR_HTTP_PROXY_NON_PROXY_HOSTS", + "www.test.de") + .execute( + () -> { + assertThat(System.getProperty("http.proxyHost")).isEmpty(); + assertThat(System.getProperty("http.proxyPort")).isEmpty(); + assertThat(System.getProperty("http.proxyUser")).isEmpty(); + assertThat(System.getProperty("http.proxyPassword")).isEmpty(); + assertThat(System.getProperty("http.nonProxyHosts")).isEmpty(); + + new ProxyHandler(); + + assertThat(System.getProperty("http.proxyHost")).isEqualTo("localhost"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("3128"); + assertThat(System.getProperty("http.proxyUser")).isEqualTo("my-user"); + assertThat(System.getProperty("http.proxyPassword")).isEqualTo("demo"); + assertThat(System.getProperty("http.nonProxyHosts")).isEqualTo("www.test.de"); + }); + }); + } - assertThat(provider).isInstanceOf(BasicCredentialsProvider.class); - } + @Test + public void shouldNotOverwriteSystemProperties_whenProxySettingsEnvVarsAndSystemProperties() + throws Exception { + restoreSystemProperties( + () -> { + withEnvironmentVariables( + "CONNECTOR_HTTP_PROXY_HOST", + "localhost", + "CONNECTOR_HTTP_PROXY_PORT", + "3128", + "CONNECTOR_HTTP_PROXY_USER", + "my-user", + "CONNECTOR_HTTP_PROXY_PASSWORD", + "demo", + "CONNECTOR_HTTP_PROXY_NON_PROXY_HOSTS", + "www.env-var.de") + .execute( + () -> { + System.setProperty("http.proxyHost", "localhost"); + System.setProperty("http.proxyPort", "8080"); + System.setProperty("http.proxyUser", "user"); + System.setProperty("http.proxyPassword", "password"); + System.setProperty("http.nonProxyHosts", "www.system-property.de"); + + new ProxyHandler(); + + assertThat(System.getProperty("http.proxyHost")).isEqualTo("localhost"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("8080"); + assertThat(System.getProperty("http.proxyUser")).isEqualTo("user"); + assertThat(System.getProperty("http.proxyPassword")).isEqualTo("password"); + assertThat(System.getProperty("http.nonProxyHosts")) + .isEqualTo("www.system-property.de"); + }); + }); } }