-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Honor system proxy settings #510
Open
drewhamilton
wants to merge
5
commits into
jfrog:master
Choose a base branch
from
drewhamilton:drew/system-proxy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
242 changes: 242 additions & 0 deletions
242
...or-gradle/src/test/java/org/jfrog/gradle/plugin/artifactory/task/DeployTaskProxyTest.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,242 @@ | ||
package org.jfrog.gradle.plugin.artifactory.task; | ||
|
||
import org.jfrog.build.api.util.Log; | ||
import org.jfrog.build.api.util.NullLog; | ||
import org.jfrog.build.client.ProxyConfiguration; | ||
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration; | ||
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager; | ||
import org.testng.annotations.*; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNull; | ||
|
||
@Test | ||
public class DeployTaskProxyTest { | ||
|
||
private static final String HTTPS_PROXY_HOST_SETTING = "https.proxyHost"; | ||
private static final String HTTPS_PROXY_PORT_SETTING = "https.proxyPort"; | ||
private static final String HTTP_PROXY_HOST_SETTING = "http.proxyHost"; | ||
private static final String HTTP_PROXY_PORT_SETTING = "http.proxyPort"; | ||
private static final String HTTP_NON_PROXY_HOSTS_SETTING = "http.nonProxyHosts"; | ||
|
||
private final Log log = new NullLog(); | ||
|
||
private String systemHttpsProxyHost = null; | ||
private String systemHttpsProxyPort = null; | ||
private String systemHttpProxyHost = null; | ||
private String systemHttpProxyPort = null; | ||
private String systemHttpNonProxyHosts = null; | ||
|
||
@BeforeMethod | ||
public void copySystemProperties() { | ||
systemHttpsProxyHost = System.getProperty(HTTPS_PROXY_HOST_SETTING); | ||
systemHttpsProxyPort = System.getProperty(HTTPS_PROXY_PORT_SETTING); | ||
systemHttpProxyHost = System.getProperty(HTTP_PROXY_HOST_SETTING); | ||
systemHttpProxyPort = System.getProperty(HTTP_PROXY_PORT_SETTING); | ||
systemHttpNonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS_SETTING); | ||
} | ||
|
||
@AfterMethod | ||
public void restoreSystemProperties() { | ||
setOrClearSystemProperty(HTTPS_PROXY_HOST_SETTING, systemHttpsProxyHost); | ||
setOrClearSystemProperty(HTTPS_PROXY_PORT_SETTING, systemHttpsProxyPort); | ||
setOrClearSystemProperty(HTTP_PROXY_HOST_SETTING, systemHttpProxyHost); | ||
setOrClearSystemProperty(HTTP_PROXY_PORT_SETTING, systemHttpProxyPort); | ||
setOrClearSystemProperty(HTTP_NON_PROXY_HOSTS_SETTING, systemHttpNonProxyHosts); | ||
} | ||
|
||
private static void setOrClearSystemProperty(String key, @Nullable String value) { | ||
if (value == null) | ||
System.clearProperty(key); | ||
else | ||
System.setProperty(key, value); | ||
} | ||
|
||
@Test | ||
public void givenExplicitProxyHostAndPort_usesExplicitProxyHostAndPort() { | ||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
clientConfig.proxy.setHost("proxy-host"); | ||
clientConfig.proxy.setPort(9999); | ||
|
||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("", clientConfig, artifactoryManager); | ||
|
||
ProxyConfiguration result = artifactoryManager.getProxyConfiguration(); | ||
assertEquals(result.host, "proxy-host"); | ||
assertEquals(result.port, 9999); | ||
} | ||
|
||
@Test | ||
public void givenExplicitProxyHostButNoPort_doesNotUseProxy() { | ||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
clientConfig.proxy.setHost("proxy-host"); | ||
|
||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenExplicitProxyPortButNoHost_doesNotUseProxy() { | ||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
clientConfig.proxy.setPort(9999); | ||
|
||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenExplicitAndSystemProxyHostAndPort_usesExplicitProxyHostAndPort() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "wrong-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(4444)); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
clientConfig.proxy.setHost("proxy-host"); | ||
clientConfig.proxy.setPort(9999); | ||
|
||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("", clientConfig, artifactoryManager); | ||
|
||
ProxyConfiguration result = artifactoryManager.getProxyConfiguration(); | ||
assertEquals(result.host, "proxy-host"); | ||
assertEquals(result.port, 9999); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxyHostAndPort_usesSystemProxyHostAndPort() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(9999)); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("", clientConfig, artifactoryManager); | ||
|
||
ProxyConfiguration result = artifactoryManager.getProxyConfiguration(); | ||
assertEquals(result.host, "proxy-host"); | ||
assertEquals(result.port, 9999); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxyHostAndPort_whenContextUrlIsHttp_usesSystemHttpProxyHostAndPort() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above line are duplicated many times in the code. I suggest we move them into a method and reuse it. |
||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("http://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
ProxyConfiguration result = artifactoryManager.getProxyConfiguration(); | ||
assertEquals(result.host, "http-proxy-host"); | ||
assertEquals(result.port, 2222); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxyHostAndPort_whenContextUrlIsHttps_usesSystemHttpsProxyHostAndPort() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
ProxyConfiguration result = artifactoryManager.getProxyConfiguration(); | ||
assertEquals(result.host, "https-proxy-host"); | ||
assertEquals(result.port, 1111); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsExactly_doesNotUseProxy() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "example.com|other"); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsWithDot_doesNotUseProxy() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, ".example.com|other"); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsByLeadingWildcard_doesNotUseProxy() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|*.example.com"); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsByCentralWildcard_doesNotUseProxy() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|ex*ple.com"); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
|
||
@Test | ||
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsWithSlash_doesNotUseProxy() { | ||
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host"); | ||
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111)); | ||
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host"); | ||
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222)); | ||
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|example.com/artifacts"); | ||
|
||
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log); | ||
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log); | ||
|
||
DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager); | ||
|
||
assertNull(artifactoryManager.getProxyConfiguration()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "else" clause is redundant, because of the above "return".