-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
221 additions
and
29 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
utils/src/main/java/software/amazon/awssdk/utils/internal/proxy/Dassd.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
utils/src/test/java/software/amazon/awssdk/utils/proxy/ProxyConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.utils.proxy; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.awssdk.testutils.EnvironmentVariableHelper; | ||
import software.amazon.awssdk.utils.LocalProxyConfiguration; | ||
import software.amazon.awssdk.utils.Pair; | ||
|
||
|
||
public class ProxyConfigurationTest { | ||
|
||
private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper(); | ||
|
||
public static Stream<Arguments> stubResponses() { | ||
return Stream.of( | ||
Arguments.of(Arrays.asList( | ||
Pair.of("%s.proxyHost", "foo.com"), | ||
Pair.of("%s.proxyPort", "555"), | ||
Pair.of("http.nonProxyHosts", "bar.com"), | ||
Pair.of("%s.proxyUser", "UserOne"), | ||
Pair.of("%s.proxyPassword", "passwordSecret")), | ||
Arrays.asList( | ||
Pair.of("%s_proxy", "http://UserOne:passwordSecret@foo.com:555/"), | ||
Pair.of("no_proxy", "bar.com") | ||
), | ||
new ExpectedProxySetting().host("foo.com").port(555).userName("UserOne").password("passwordSecret").nonProxyHost("bar.com"), | ||
"All Proxy Parameters are Set"), | ||
|
||
Arguments.of(Arrays.asList( | ||
Pair.of("%s.proxyHost", "foo.com"), | ||
Pair.of("%s.proxyPort", "555")), | ||
Collections.singletonList( | ||
Pair.of("%s_proxy", "http://foo.com:555/") | ||
), | ||
new ExpectedProxySetting().host("foo.com").port(555), | ||
"Optional Parameters are not set"), | ||
|
||
|
||
Arguments.of(Arrays.asList( | ||
Pair.of("%s.proxyHost", ""), | ||
Pair.of("%s.proxyPort", ""), | ||
Pair.of("%s.nonProxyHosts", ""), | ||
Pair.of("%s.proxyUser", ""), | ||
Pair.of("%s.nonProxyHosts", ""), | ||
Pair.of("%s.proxyPassword", "")), | ||
Arrays.asList( | ||
Pair.of("%s_proxy", ""), | ||
Pair.of("no_proxy", "") | ||
|
||
), | ||
new ExpectedProxySetting().port(0), | ||
"All parameters are Blank"), | ||
|
||
|
||
Arguments.of(Collections.singletonList( | ||
Pair.of("%s.nonProxyHosts", "")), | ||
Collections.singletonList( | ||
Pair.of("no_proxy", "") | ||
), | ||
new ExpectedProxySetting().port(0), | ||
"Only Non Proxy Hosts are set with multiple value"), | ||
|
||
|
||
Arguments.of(Arrays.asList( | ||
Pair.of("%s.proxyVaildHost", "foo.com"), | ||
Pair.of("%s.proxyPorts", "555")), | ||
Collections.singletonList( | ||
Pair.of("%s_proxy", "http://foo:com:Incorrects:555/") | ||
), | ||
new ExpectedProxySetting().port(0), | ||
"Incorrect local Setting") | ||
); | ||
} | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
Stream.of("http", "https").forEach(protocol -> | ||
Stream.of("%s.proxyHost", "%s.proxyPort", "%s.nonProxyHosts", "%s.proxyUser", | ||
"%s.proxyPassword") | ||
.forEach(property -> System.clearProperty(String.format(property, protocol))) | ||
); | ||
|
||
ENVIRONMENT_VARIABLE_HELPER.reset(); | ||
|
||
} | ||
|
||
@ParameterizedTest(name = "{index} - {3}.") | ||
@MethodSource("stubResponses") | ||
void given_LocalSetting_when_httpProtocol_then_correctProxyConfiguration(List<Pair<String, String>> systemSettingsPair, | ||
List<Pair<String, String>> envSystemSetting, | ||
ExpectedProxySetting expectedProxySetting, | ||
String testCaseName) { | ||
|
||
setSystemProperties(systemSettingsPair, "http"); | ||
setEnvironmentProperties(envSystemSetting, "http"); | ||
|
||
assertProxyEquals(LocalProxyConfiguration.fromSystemPropertySettings("http"), expectedProxySetting); | ||
assertProxyEquals(LocalProxyConfiguration.fromEnvironmentSettings("http"), expectedProxySetting); | ||
|
||
} | ||
|
||
@ParameterizedTest(name = "{index} - {3}.") | ||
@MethodSource("stubResponses") | ||
void given_LocalSetting_when_httpsProtocol_then_correctProxyConfiguration(List<Pair<String, String>> systemSettingsPair, | ||
List<Pair<String, String>> envSystemSetting, | ||
ExpectedProxySetting expectedProxySetting, | ||
String testCaseName) { | ||
|
||
setSystemProperties(systemSettingsPair, "https"); | ||
setEnvironmentProperties(envSystemSetting, "https"); | ||
|
||
assertProxyEquals(LocalProxyConfiguration.fromSystemPropertySettings("https"), expectedProxySetting); | ||
assertProxyEquals(LocalProxyConfiguration.fromEnvironmentSettings("https"), expectedProxySetting); | ||
|
||
} | ||
|
||
private static void assertProxyEquals(LocalProxyConfiguration actualConfiguration, | ||
ExpectedProxySetting expectedProxySetting) { | ||
assertThat(actualConfiguration.port()).isEqualTo(expectedProxySetting.port); | ||
assertThat(actualConfiguration.host()).isEqualTo(expectedProxySetting.host); | ||
assertThat(actualConfiguration.nonProxyHosts()).isEqualTo(expectedProxySetting.nonProxyHosts); | ||
|
||
assertThat(actualConfiguration.userName().orElse(null)).isEqualTo(expectedProxySetting.userName); | ||
assertThat(actualConfiguration.password().orElse(null)).isEqualTo(expectedProxySetting.password); | ||
} | ||
|
||
static void setSystemProperties(List<Pair<String, String>> settingsPairs, String protocol) { | ||
settingsPairs.forEach(settingsPair -> System.setProperty(String.format(settingsPair.left(), protocol), | ||
settingsPair.right())); | ||
} | ||
|
||
static void setEnvironmentProperties(List<Pair<String, String>> settingsPairs, String protocol) { | ||
|
||
|
||
settingsPairs.forEach(settingsPair -> ENVIRONMENT_VARIABLE_HELPER.set(String.format(settingsPair.left(), protocol), | ||
settingsPair.right())); | ||
} | ||
|
||
static void clearProperties(List<Pair<String, String>> settingsPairs, String protocol) { | ||
settingsPairs.forEach(settingsPair -> System.setProperty(String.format(settingsPair.left(), protocol), | ||
settingsPair.right())); | ||
} | ||
|
||
|
||
private static class ExpectedProxySetting { | ||
private int port; | ||
private String host; | ||
private String userName; | ||
private String password; | ||
private Set<String> nonProxyHosts = new HashSet<>(); | ||
|
||
|
||
public ExpectedProxySetting port(int port) { | ||
this.port = port; | ||
return this; | ||
} | ||
|
||
public ExpectedProxySetting host(String host) { | ||
this.host = host; | ||
return this; | ||
} | ||
|
||
public ExpectedProxySetting userName(String userName) { | ||
this.userName = userName; | ||
return this; | ||
} | ||
|
||
public ExpectedProxySetting password(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public ExpectedProxySetting nonProxyHost(String... nonProxyHosts) { | ||
this.nonProxyHosts = nonProxyHosts != null ? Arrays.stream(nonProxyHosts) | ||
.collect(Collectors.toSet()) : new HashSet<>(); | ||
return this; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |