Skip to content

Commit

Permalink
Added generic test cases to test combination of settings
Browse files Browse the repository at this point in the history
  • Loading branch information
joviegas committed Sep 29, 2023
1 parent d502ccd commit de6e4dc
Show file tree
Hide file tree
Showing 19 changed files with 1,215 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@

package software.amazon.awssdk.crtcore;

import static software.amazon.awssdk.utils.ProxyConfigProvider.getProxyConfig;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.LocalProxyConfiguration;
import software.amazon.awssdk.utils.ProxyConfigProvider;
import software.amazon.awssdk.utils.ProxySystemSetting;
import software.amazon.awssdk.utils.StringUtils;

Expand All @@ -41,15 +46,14 @@ protected CrtProxyConfiguration(DefaultBuilder<?> builder) {
this.useEnvironmentVariableValues = builder.useEnvironmentVariableValues;
this.scheme = builder.scheme;

LocalProxyConfiguration localProxyConfiguration = useSystemPropertyValues ?
LocalProxyConfiguration.fromSystemPropertySettings(builder.scheme) : null;

this.host = builder.host != null || localProxyConfiguration == null ? builder.host : localProxyConfiguration.host();
this.port = builder.port != 0 || localProxyConfiguration == null ? builder.port : localProxyConfiguration.port();
this.username = ! StringUtils.isEmpty(builder.username) || localProxyConfiguration == null ? builder.username :
localProxyConfiguration.userName().orElseGet(() -> builder.username);
this.password = ! StringUtils.isEmpty(builder.password) || localProxyConfiguration == null ? builder.password :
localProxyConfiguration.password().orElseGet(() -> builder.password);
ProxyConfigProvider proxyConfigProvider = getProxyConfig(builder.useSystemPropertyValues,
builder.useEnvironmentVariableValues ,builder.scheme);
this.host = builder.host != null || proxyConfigProvider == null ? builder.host : proxyConfigProvider.host();
this.port = builder.port != 0 || proxyConfigProvider == null ? builder.port : proxyConfigProvider.port();
this.username = ! StringUtils.isEmpty(builder.username) || proxyConfigProvider == null ? builder.username :
proxyConfigProvider.userName().orElseGet(() -> builder.username);
this.password = ! StringUtils.isEmpty(builder.password) || proxyConfigProvider == null ? builder.password :
proxyConfigProvider.password().orElseGet(() -> builder.password);
}

/**
Expand Down Expand Up @@ -80,10 +84,7 @@ public final int port() {
* property, based on the scheme used, if {@link Builder#useSystemPropertyValues(Boolean)} is set to true
* */
public final String username() {
if (Objects.equals(scheme(), HTTPS)) {
return resolveValue(username, ProxySystemSetting.HTTPS_PROXY_USERNAME);
}
return resolveValue(username, ProxySystemSetting.PROXY_USERNAME);
return username;
}

/**
Expand All @@ -92,10 +93,7 @@ public final String username() {
* to true
* */
public final String password() {
if (Objects.equals(scheme(), HTTPS)) {
return resolveValue(password, ProxySystemSetting.HTTPS_PROXY_PASSWORD);
}
return resolveValue(password, ProxySystemSetting.PROXY_PASSWORD);
return password;
}

@Override
Expand Down Expand Up @@ -210,15 +208,6 @@ public interface Builder {
}



/**
* Uses the configuration options, system setting property and returns the final value of the given member.
*/
private String resolveValue(String value, ProxySystemSetting systemSetting) {
return value == null && Boolean.TRUE.equals(useSystemPropertyValues) ?
systemSetting.getStringValue().orElse(null) : value;
}

protected abstract static class DefaultBuilder<B extends Builder> implements Builder {
private String scheme;
private String host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

package software.amazon.awssdk.http.apache;

import static software.amazon.awssdk.utils.ProxyConfigProvider.getProxyConfig;
import static software.amazon.awssdk.utils.StringUtils.isEmpty;

import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.LocalProxyConfiguration;
import software.amazon.awssdk.utils.ProxyConfigProvider;
import software.amazon.awssdk.utils.ProxySystemSetting;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.ToString;
Expand Down Expand Up @@ -56,9 +56,9 @@ public final class ProxyConfiguration implements ToCopyableBuilder<ProxyConfigur
*/
private ProxyConfiguration(DefaultClientProxyConfigurationBuilder builder) {
this.endpoint = builder.endpoint;
this.scheme = resolveScheme();

LocalProxyConfiguration proxyConfiguration = getProxyConfig(builder);
String resolvedScheme = resolveScheme(builder);
this.scheme = resolvedScheme;
ProxyConfigProvider proxyConfiguration = getProxyConfig(builder.useSystemPropertyValues, builder.useEnvironmentVariableValues ,resolvedScheme);
this.username = !StringUtils.isEmpty(builder.username) || proxyConfiguration == null ? builder.username :
proxyConfiguration.userName().orElseGet(() -> builder.username);
this.password = !StringUtils.isEmpty(builder.password) || proxyConfiguration == null ? builder.password :
Expand All @@ -75,15 +75,6 @@ private ProxyConfiguration(DefaultClientProxyConfigurationBuilder builder) {
this.port = resolvePort(proxyConfiguration);
}

private LocalProxyConfiguration getProxyConfig(DefaultClientProxyConfigurationBuilder builder) {
if (builder.useSystemPropertyValues) {
return LocalProxyConfiguration.fromSystemPropertySettings(scheme());
} else if (builder.useEnvironmentVariableValues) {
return LocalProxyConfiguration.fromEnvironmentSettings(scheme());
} else {
return null;
}
}


/**
Expand Down Expand Up @@ -116,10 +107,7 @@ public String scheme() {
* @see Builder#password(String)
*/
public String username() {
if (Objects.equals(scheme(), HTTPS)) {
return resolveValue(username, ProxySystemSetting.HTTPS_PROXY_USERNAME);
}
return resolveValue(username, ProxySystemSetting.PROXY_USERNAME);
return username;
}

/**
Expand All @@ -129,10 +117,7 @@ public String username() {
*/
public String password() {

if (Objects.equals(scheme(), HTTPS)) {
return resolveValue(password, ProxySystemSetting.HTTPS_PROXY_PASSWORD);
}
return resolveValue(password, ProxySystemSetting.PROXY_PASSWORD);
return password;
}

/**
Expand Down Expand Up @@ -183,7 +168,8 @@ public Builder toBuilder() {
.ntlmWorkstation(ntlmWorkstation)
.nonProxyHosts(nonProxyHosts)
.preemptiveBasicAuthenticationEnabled(preemptiveBasicAuthenticationEnabled)
.useSystemPropertyValues(useSystemPropertyValues);
.useSystemPropertyValues(useSystemPropertyValues)
.useEnvironmentVariableValues(useEnvironmentVariablesValues);
}

/**
Expand All @@ -202,34 +188,32 @@ public String toString() {
.add("ntlmWorkstation", ntlmWorkstation)
.add("nonProxyHosts", nonProxyHosts)
.add("preemptiveBasicAuthenticationEnabled", preemptiveBasicAuthenticationEnabled)
.add("useSystemPropertyValues", useSystemPropertyValues)
.add("useEnvironmentVariablesValues", useEnvironmentVariablesValues)
.build();
}


private String resolveHost(String scheme, LocalProxyConfiguration localProxyConfiguration) {
private String resolveHost(String scheme, ProxyConfigProvider proxyConfigProvider) {
if (endpoint != null) {
return endpoint.getHost();
}

if (Objects.equals(scheme, HTTPS)) {
return resolveValue(null, ProxySystemSetting.HTTPS_PROXY_HOST);
}
return localProxyConfiguration != null ? localProxyConfiguration.host() : null;
return proxyConfigProvider.host();
}

private int resolvePort(LocalProxyConfiguration localProxyConfiguration) {
private int resolvePort(ProxyConfigProvider proxyConfigProvider) {
int port = 0;

if (endpoint != null) {
port = endpoint.getPort();
} else if(localProxyConfiguration != null){
return localProxyConfiguration.port();
} else if(proxyConfigProvider != null){
return proxyConfigProvider.port();
}
return port;
}

public String resolveScheme() {
return endpoint != null ? endpoint.getScheme() : null;
public String resolveScheme(DefaultClientProxyConfigurationBuilder builder) {
return endpoint != null ? endpoint.getScheme() : builder.scheme;
}

/**
Expand Down Expand Up @@ -299,6 +283,11 @@ public interface Builder extends CopyableBuilder<Builder, ProxyConfiguration> {
*/
Builder useSystemPropertyValues(Boolean useSystemPropertyValues);

Builder useEnvironmentVariableValues(Boolean useEnvironmentVariableValues);


Builder scheme(String scheme);

}

/**
Expand All @@ -315,6 +304,7 @@ private static final class DefaultClientProxyConfigurationBuilder implements Bui
private Boolean preemptiveBasicAuthenticationEnabled;
private Boolean useSystemPropertyValues = Boolean.TRUE;
private Boolean useEnvironmentVariableValues = Boolean.TRUE;
private String scheme ;

@Override
public Builder endpoint(URI endpoint) {
Expand Down Expand Up @@ -412,6 +402,25 @@ public void setUseSystemPropertyValues(Boolean useSystemPropertyValues) {
useSystemPropertyValues(useSystemPropertyValues);
}


@Override
public Builder useEnvironmentVariableValues(Boolean useEnvironmentVariableValues) {
this.useEnvironmentVariableValues = useEnvironmentVariableValues;

return this;
}

@Override
public Builder scheme(String scheme) {
this.scheme = scheme;
return this;
}

public void setUseEnvironmentVariableValues(Boolean useEnvironmentVariableValues) {
useEnvironmentVariableValues(useEnvironmentVariableValues);
}


@Override
public ProxyConfiguration build() {
return new ProxyConfiguration(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.http.apache;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import software.amazon.awssdk.http.HttpProxyTestSuite;
import software.amazon.awssdk.http.proxy.TestProxySetting;

public class ApacheHttpProxyTest extends HttpProxyTestSuite {
@Override
protected void assertProxyConfiguration(TestProxySetting userSetProxySettings,
TestProxySetting expectedProxySettings,
Boolean useSystemProperty,
Boolean useEnvironmentVariable,
String protocol) {
ProxyConfiguration.Builder builder = ProxyConfiguration.builder();

if (userSetProxySettings != null) {
String hostName = userSetProxySettings.getHost();
Integer portNumber = userSetProxySettings.getPort();
String userName = userSetProxySettings.getUserName();
String password = userSetProxySettings.getPassword();
Set<String> nonProxyHosts = userSetProxySettings.getNonProxyHosts();

if (hostName != null && portNumber != null) {
try {
builder.endpoint(new URI(String.format("%s://%s:%d", protocol, hostName, portNumber)));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
if (userName != null) {
builder.username(userName);
}
if (password != null) {
builder.password(password);
}
if (nonProxyHosts != null && !nonProxyHosts.isEmpty()) {
builder.nonProxyHosts(nonProxyHosts);
}
}
if(!"http".equals(protocol)){
builder.scheme(protocol);
}
if (useSystemProperty != null) {
builder.useSystemPropertyValues(useSystemProperty);
}
if (useEnvironmentVariable != null) {
builder.useEnvironmentVariableValues(useEnvironmentVariable);
}
ProxyConfiguration proxyConfiguration = builder.build();
assertThat(proxyConfiguration.host()).isEqualTo(expectedProxySettings.getHost());
assertThat(proxyConfiguration.port()).isEqualTo(expectedProxySettings.getPort());
assertThat(proxyConfiguration.username()).isEqualTo(expectedProxySettings.getUserName());
assertThat(proxyConfiguration.password()).isEqualTo(expectedProxySettings.getPassword());
assertThat(proxyConfiguration.nonProxyHosts()).isEqualTo(expectedProxySettings.getNonProxyHosts());
}

}
Loading

0 comments on commit de6e4dc

Please sign in to comment.