ProxyAddressSelector { public static String getProxyForUrl(String url) throws IllegalArgumentException { Proxy proxy = null; if (url != null && url.length() > 0 && url.charAt(0) != '/') { try { proxy = filter(PROXY_SELECTOR.select(new URI(normalizeUrl(url)))); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot convert URL to URI: " + url, e); } } return proxy != null && proxy.address() != null ? proxy.address().toString() : null; } private  ProxyAddressSelector(); static String getProxyForUrl(String url);  }
@Test public void testBothProxiesDefined() throws HttpException { System.setProperty("http.proxyHost", "httpProxy"); System.setProperty("http.proxyPort", "9090"); System.setProperty("https.proxyHost", "httpsProxy"); System.setProperty("https.proxyPort", "9443"); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("http: assertEquals("httpsProxy:9443", ProxyAddressSelector.getProxyForUrl("https: assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("http: assertEquals("httpsProxy:9443", ProxyAddressSelector.getProxyForUrl("https: assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("www.jahia.com:9090")); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("localhost:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("/aaa.html")); }
@Test public void testHttpProxyDefined() throws HttpException { System.setProperty("http.proxyHost", "httpProxy"); System.setProperty("http.proxyPort", "9090"); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("www.jahia.com:9090")); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("localhost:9090")); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("localhost:9090/aaa/bb/test.jsp?a=aaa&b=bbb")); assertNull(ProxyAddressSelector.getProxyForUrl("/aaa.html")); System.setProperty("http.proxyPort", ""); assertEquals("httpProxy:80", ProxyAddressSelector.getProxyForUrl("http: }
@Test public void testHttpsProxyDefined() throws HttpException { System.setProperty("https.proxyHost", "httpsProxy"); System.setProperty("https.proxyPort", "9443"); assertNull(ProxyAddressSelector.getProxyForUrl("http: assertEquals("httpsProxy:9443", ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("http: assertEquals("httpsProxy:9443", ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("www.jahia.com:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("localhost:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("/aaa.html")); System.setProperty("https.proxyPort", ""); assertEquals("httpsProxy:443", ProxyAddressSelector.getProxyForUrl("https: }
@Test public void testNonProxyHostsDefined() throws HttpException { System.setProperty("http.proxyHost", "httpProxy"); System.setProperty("http.proxyPort", "9090"); System.setProperty("https.proxyHost", "httpsProxy"); System.setProperty("https.proxyPort", "9443"); System.setProperty("http.nonProxyHosts", "localhost|*.jahia.com"); assertNull(ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("www.jahia.com:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("localhost:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("/aaa.html")); assertEquals("httpProxy:9090", ProxyAddressSelector.getProxyForUrl("http: assertEquals("httpsProxy:9443", ProxyAddressSelector.getProxyForUrl("https: }
@Test public void testNoProxySettingsDefined() throws HttpException { assertNull(ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("http: assertNull(ProxyAddressSelector.getProxyForUrl("https: assertNull(ProxyAddressSelector.getProxyForUrl("www.jahia.com:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("localhost:9090")); assertNull(ProxyAddressSelector.getProxyForUrl("/aaa.html")); }