This source file includes following definitions.
- setUp
- toString
- toString
- checkMapping
- Feature
- testNoProxy
- Feature
- testHttpProxyHostAndPort
- Feature
- testHttpProxyHostOnly
- Feature
- testHttpProxyPortOnly
- Feature
- testHttpNonProxyHosts1
- Feature
- testHttpNonProxyHosts2
- Feature
- testHttpNonProxyHosts3
- Feature
- testFtpNonProxyHosts
- Feature
- testFtpProxyHostAndPort
- Feature
- testFtpProxyHostOnly
- Feature
- testHttpsProxyHostAndPort
- Feature
- testDefaultProxyExplictPort
- Feature
- testFallbackToSocks
- Feature
- testSocksExplicitPort
- Feature
- testHttpProxySupercedesSocks
package org.chromium.net;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Properties;
public class AndroidProxySelectorTest extends InstrumentationTestCase {
Properties mProperties;
public AndroidProxySelectorTest() {
mProperties = new Properties();
}
@Override
public void setUp() {
System.setProperties(mProperties);
}
static String toString(Proxy proxy) {
if (proxy == Proxy.NO_PROXY)
return "DIRECT";
Proxy.Type type = proxy.type();
switch (type) {
case HTTP:
return "PROXY " + proxy.address().toString();
case SOCKS:
return "SOCKS5 " + proxy.address().toString();
case DIRECT:
return "DIRECT";
default:
fail("Unknown proxy type" + type);
return "unknown://";
}
}
static String toString(List<Proxy> proxies) {
StringBuilder builder = new StringBuilder();
for (Proxy proxy : proxies) {
if (builder.length() > 0)
builder.append(';');
builder.append(toString(proxy));
}
return builder.toString();
}
static void checkMapping(String url, String expected) throws URISyntaxException {
URI uri = new URI(url);
List<Proxy> proxies = ProxySelector.getDefault().select(uri);
assertEquals("Mapping", expected, toString(proxies));
}
@SmallTest
@Feature({"AndroidWebView"})
public void testNoProxy() throws Exception {
checkMapping("ftp://example.com/", "DIRECT");
checkMapping("http://example.com/", "DIRECT");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpProxyHostAndPort() throws Exception {
System.setProperty("http.proxyHost", "httpproxy.com");
System.setProperty("http.proxyPort", "8080");
checkMapping("ftp://example.com/", "DIRECT");
checkMapping("http://example.com/", "PROXY httpproxy.com:8080");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpProxyHostOnly() throws Exception {
System.setProperty("http.proxyHost", "httpproxy.com");
checkMapping("ftp://example.com/", "DIRECT");
checkMapping("http://example.com/", "PROXY httpproxy.com:80");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpProxyPortOnly() throws Exception {
System.setProperty("http.proxyPort", "8080");
checkMapping("ftp://example.com/", "DIRECT");
checkMapping("http://example.com/", "DIRECT");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpNonProxyHosts1() throws Exception {
System.setProperty("http.nonProxyHosts", "slashdot.org");
System.setProperty("http.proxyHost", "httpproxy.com");
System.setProperty("http.proxyPort", "8080");
checkMapping("http://example.com/", "PROXY httpproxy.com:8080");
checkMapping("http://slashdot.org/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpNonProxyHosts2() throws Exception {
System.setProperty("http.nonProxyHosts", "slashdot.org|freecode.net");
System.setProperty("http.proxyHost", "httpproxy.com");
System.setProperty("http.proxyPort", "8080");
checkMapping("http://example.com/", "PROXY httpproxy.com:8080");
checkMapping("http://freecode.net/", "DIRECT");
checkMapping("http://slashdot.org/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpNonProxyHosts3() throws Exception {
System.setProperty("http.nonProxyHosts", "*example.com");
System.setProperty("http.proxyHost", "httpproxy.com");
System.setProperty("http.proxyPort", "8080");
checkMapping("http://example.com/", "DIRECT");
checkMapping("http://slashdot.org/", "PROXY httpproxy.com:8080");
checkMapping("http://www.example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testFtpNonProxyHosts() throws Exception {
System.setProperty("ftp.nonProxyHosts", "slashdot.org");
System.setProperty("ftp.proxyHost", "httpproxy.com");
System.setProperty("ftp.proxyPort", "8080");
checkMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
checkMapping("http://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testFtpProxyHostAndPort() throws Exception {
System.setProperty("ftp.proxyHost", "httpproxy.com");
System.setProperty("ftp.proxyPort", "8080");
checkMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
checkMapping("http://example.com/", "DIRECT");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testFtpProxyHostOnly() throws Exception {
System.setProperty("ftp.proxyHost", "httpproxy.com");
checkMapping("ftp://example.com/", "PROXY httpproxy.com:80");
checkMapping("http://example.com/", "DIRECT");
checkMapping("https://example.com/", "DIRECT");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpsProxyHostAndPort() throws Exception {
System.setProperty("https.proxyHost", "httpproxy.com");
System.setProperty("https.proxyPort", "8080");
checkMapping("ftp://example.com/", "DIRECT");
checkMapping("http://example.com/", "DIRECT");
checkMapping("https://example.com/", "PROXY httpproxy.com:8080");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testDefaultProxyExplictPort() throws Exception {
System.setProperty("ftp.proxyHost", "httpproxy.com");
System.setProperty("ftp.proxyPort", "8080");
System.setProperty("proxyHost", "defaultproxy.com");
System.setProperty("proxyPort", "8080");
checkMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
checkMapping("http://example.com/", "PROXY defaultproxy.com:8080");
checkMapping("https://example.com/", "PROXY defaultproxy.com:8080");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testFallbackToSocks() throws Exception {
System.setProperty("http.proxyHost", "defaultproxy.com");
System.setProperty("socksProxyHost", "socksproxy.com");
checkMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080");
checkMapping("http://example.com/", "PROXY defaultproxy.com:80");
checkMapping("https://example.com/", "SOCKS5 socksproxy.com:1080");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testSocksExplicitPort() throws Exception {
System.setProperty("socksProxyHost", "socksproxy.com");
System.setProperty("socksProxyPort", "9000");
checkMapping("http://example.com/", "SOCKS5 socksproxy.com:9000");
}
@SmallTest
@Feature({"AndroidWebView"})
public void testHttpProxySupercedesSocks() throws Exception {
System.setProperty("proxyHost", "defaultproxy.com");
System.setProperty("socksProxyHost", "socksproxy.com");
System.setProperty("socksProxyPort", "9000");
checkMapping("http://example.com/", "PROXY defaultproxy.com:80");
}
}