| 1 |
package com.github.markusbernhardt.proxy.selector.pac; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
|
| 5 |
import java.io.File; |
| 6 |
import java.io.IOException; |
| 7 |
import java.net.InetSocketAddress; |
| 8 |
import java.net.MalformedURLException; |
| 9 |
import java.net.Proxy; |
| 10 |
import java.net.Proxy.Type; |
| 11 |
import java.net.ProxySelector; |
| 12 |
import java.net.SocketAddress; |
| 13 |
import java.net.URI; |
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
|
| 18 |
import com.github.markusbernhardt.proxy.TestUtil; |
| 19 |
import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector; |
| 20 |
import com.github.markusbernhardt.proxy.selector.pac.PacScriptMethods; |
| 21 |
import com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource; |
| 22 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class PacProxySelectorTest { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Test |
| 42 |
public void testScriptExecution() throws ProxyException, MalformedURLException { |
| 43 |
List<Proxy> result = new PacProxySelector(new UrlPacScriptSource(toUrl("test1.pac"))) |
| 44 |
.select(TestUtil.HTTP_TEST_URI); |
| 45 |
|
| 46 |
assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0)); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@Test |
| 58 |
public void testScriptExecution2() throws ProxyException, MalformedURLException { |
| 59 |
PacProxySelector pacProxySelector = new PacProxySelector(new UrlPacScriptSource(toUrl("test2.pac"))); |
| 60 |
List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI); |
| 61 |
assertEquals(Proxy.NO_PROXY, result.get(0)); |
| 62 |
|
| 63 |
result = pacProxySelector.select(TestUtil.HTTPS_TEST_URI); |
| 64 |
assertEquals(Proxy.NO_PROXY, result.get(0)); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Test |
| 76 |
public void pacDownloadFromURLShouldNotUseProxy() throws ProxyException, MalformedURLException { |
| 77 |
ProxySelector oldOne = ProxySelector.getDefault(); |
| 78 |
try { |
| 79 |
ProxySelector.setDefault(new ProxySelector() { |
| 80 |
@Override |
| 81 |
public List<Proxy> select(URI uri) { |
| 82 |
throw new IllegalStateException("Should not download via proxy"); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 87 |
|
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
PacProxySelector pacProxySelector = new PacProxySelector( |
| 92 |
new UrlPacScriptSource("http://www.test.invalid/wpad.pac")); |
| 93 |
pacProxySelector.select(TestUtil.HTTPS_TEST_URI); |
| 94 |
} finally { |
| 95 |
ProxySelector.setDefault(oldOne); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Test |
| 108 |
public void testScriptMuliProxy() throws ProxyException, MalformedURLException { |
| 109 |
PacProxySelector pacProxySelector = new PacProxySelector(new UrlPacScriptSource(toUrl("testMultiProxy.pac"))); |
| 110 |
List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI); |
| 111 |
assertEquals(2, result.size()); |
| 112 |
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy.com", 80)), result.get(0)); |
| 113 |
assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy2.com", 8080)), result.get(1)); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Test |
| 125 |
public void testLocalIPOverride() throws ProxyException, MalformedURLException { |
| 126 |
System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "123.123.123.123"); |
| 127 |
try { |
| 128 |
PacProxySelector pacProxySelector = new PacProxySelector(new UrlPacScriptSource(toUrl("testLocalIP.pac"))); |
| 129 |
List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI); |
| 130 |
assertEquals(result.get(0), |
| 131 |
new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("123.123.123.123", 8080))); |
| 132 |
} finally { |
| 133 |
System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, ""); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private String toUrl(String testFile) throws MalformedURLException { |
| 148 |
return new File(TestUtil.TEST_DATA_FOLDER + "pac", testFile).toURI().toURL().toString(); |
| 149 |
} |
| 150 |
|
| 151 |
} |