Debian Codegraph @

src/test/java/com/github/markusbernhardt/proxy/selector/pac/PacProxySelectorTest.java

raw symbols SCIP
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 * Tests for the Pac script parser and proxy selector.
26 *
27 * @author Markus Bernhardt, Copyright 2016
28 * @author Bernd Rosstauscher, Copyright 2009
29 ****************************************************************************/
30
31 public class PacProxySelectorTest {
32
33 /*************************************************************************
34 * Test method
35 *
36 * @throws ProxyException
37 * on proxy detection error.
38 * @throws MalformedURLException
39 * on URL erros
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 * Test method
51 *
52 * @throws ProxyException
53 * on proxy detection error.
54 * @throws MalformedURLException
55 * on URL erros
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 * Test download fix to prevent infinite loop.
69 *
70 * @throws ProxyException
71 * on proxy detection error.
72 * @throws MalformedURLException
73 * on URL erros
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 // Not used
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 * Test method
101 *
102 * @throws ProxyException
103 * on proxy detection error.
104 * @throws MalformedURLException
105 * on URL erros
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 * Test method for the override local IP feature.
118 *
119 * @throws ProxyException
120 * on proxy detection error.
121 * @throws MalformedURLException
122 * on URL erros
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 * Helper method to build the url to the given test file
140 *
141 * @param testFile
142 * the name of the test file.
143 * @return the URL.
144 * @throws MalformedURLException
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 }