Created
January 2, 2019 17:01
-
-
Save rhowe-gds/2ddcb891b70e348112e5f8080531ec45 to your computer and use it in GitHub Desktop.
Testing how Java's proxy settings behave
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
import com.google.common.collect.ImmutableList; | |
import org.junit.Test; | |
import static org.hamcrest.core.Is.is; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertThat; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.net.ProxySelector; | |
import java.net.URISyntaxException; | |
public class ProxyTest { | |
private void proxySettings(String httpProxy, String httpsProxy, String nonProxyHosts) { | |
if (httpProxy == null) | |
System.clearProperty("http.proxyHost"); | |
else | |
System.setProperty("http.proxyHost", httpProxy); | |
if (httpsProxy == null) | |
System.clearProperty("https.proxyHost"); | |
else | |
System.setProperty("https.proxyHost", httpsProxy); | |
if (nonProxyHosts == null) | |
System.clearProperty("http.nonProxyHosts"); | |
else | |
System.setProperty("http.nonProxyHosts", nonProxyHosts); | |
} | |
@Test | |
public void withoutAProxyGoesDirect() throws URISyntaxException { | |
proxySettings(null, null, null); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
java.util.List proxies = selector.select(new java.net.URI("https://example.com")); | |
assertEquals(proxies.size(), 1); | |
assertThat(ImmutableList.of(Proxy.NO_PROXY), is(proxies)); | |
} | |
@Test | |
public void usesAHttpProxyForHttpUrlsOnly() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, null); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("https://example.com"))); | |
} | |
@Test | |
public void usesAHttpsProxyForHttpsUrlsOnly() throws URISyntaxException { | |
proxySettings(null, "proxy.example.com",null); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 443))), selector.select(new java.net.URI("https://example.com"))); | |
} | |
@Test | |
public void avoidsProxyForLocalhostByDefault() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, null); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://localhost"))); | |
} | |
@Test | |
public void canBypassProxy() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, "example.com"); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.org"))); | |
} | |
@Test | |
public void canBypassProxyForMultipleHosts() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, "example.com|example.org"); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://example.org"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://subdomain.example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.net"))); | |
} | |
@Test | |
public void canBypassProxyForMultipleHostPatterns() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, "*.example.com|*.example.org"); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.org"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://subdomain.example.com"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://subdomain.example.org"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.net"))); | |
} | |
@Test | |
public void canBypassProxyForPartialHostPatterns() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, "*-internal.example.com"); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.org"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://a-internal.example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://-internal.example.com"))); | |
} | |
@Test | |
public void canBypassProxyForMultiplePartialHostPatterns() throws URISyntaxException { | |
proxySettings("proxy.example.com",null, "*-internal.example.com|*.foo.example.com"); | |
ProxySelector selector = new sun.net.spi.DefaultProxySelector(); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://example.org"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://a-internal.example.com"))); | |
assertEquals(ImmutableList.of(Proxy.NO_PROXY), selector.select(new java.net.URI("http://bar.foo.example.com"))); | |
assertEquals(ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 80))), selector.select(new java.net.URI("http://-internal.example.com"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment