summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/btr/proxy/selector/pac/PacProxySelectorTest.java
blob: a08f9c172d2ccfeee5055eb67a91440f4a8d6cfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.btr.proxy.selector.pac;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;

import org.junit.Test;

import com.btr.proxy.TestUtil;
import com.btr.proxy.util.ProxyException;


/*****************************************************************************
 * Tests for the Pac script parser and proxy selector. 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class PacProxySelectorTest {

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testScriptExecution() throws ProxyException, MalformedURLException {
		List<Proxy> result = new PacProxySelector(
				new UrlPacScriptSource(toUrl("test1.pac"))).select(TestUtil.HTTP_TEST_URI);
		
		assertEquals(TestUtil.HTTP_TEST_PROXY, result.get(0));
	}

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testScriptExecution2() throws ProxyException, MalformedURLException {
		PacProxySelector pacProxySelector = new PacProxySelector(
				new UrlPacScriptSource(toUrl("test2.pac")));
		List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
		assertEquals(Proxy.NO_PROXY, result.get(0));
		
		result = pacProxySelector.select(TestUtil.HTTPS_TEST_URI);
		assertEquals(Proxy.NO_PROXY, result.get(0));
	}
	
	/*************************************************************************
	 * Test download fix to prevent infinite loop.
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void pacDownloadFromURLShouldNotUseProxy() throws ProxyException, MalformedURLException {
		ProxySelector oldOne = ProxySelector.getDefault();
		try {
			ProxySelector.setDefault(new ProxySelector() {
				@Override
				public List<Proxy> select(URI uri) {
					throw new IllegalStateException("Should not download via proxy");
				}
				
				@Override
				public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
					// Not used
				}
			});

			PacProxySelector pacProxySelector = new PacProxySelector(
					new UrlPacScriptSource("http://www.test.invalid/wpad.pac"));
			pacProxySelector.select(TestUtil.HTTPS_TEST_URI);
		} finally {
			ProxySelector.setDefault(oldOne);
		}
	}

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testScriptMuliProxy() throws ProxyException, MalformedURLException {
		PacProxySelector pacProxySelector = new PacProxySelector(
				new UrlPacScriptSource(toUrl("testMultiProxy.pac")));
		List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
		assertEquals(2, result.size());
		assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy.com", 80)), result.get(0));
		assertEquals(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("my-proxy2.com", 8080)), result.get(1));
	}
	
	/*************************************************************************
	 * Test method for the override local IP feature.
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testLocalIPOverride() throws ProxyException, MalformedURLException {
		System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "123.123.123.123");
		try {
			PacProxySelector pacProxySelector = new PacProxySelector(
					new UrlPacScriptSource(toUrl("testLocalIP.pac")));
			List<Proxy> result = pacProxySelector.select(TestUtil.HTTP_TEST_URI);
			assertEquals(result.get(0), new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("123.123.123.123", 8080)));
		} finally {
			System.setProperty(PacScriptMethods.OVERRIDE_LOCAL_IP, "");
		}
		
	}
	
	/*************************************************************************
	 * Helper method to build the url to the given test file
	 * @param testFile the name of the test file.
	 * @return the URL. 
	 * @throws MalformedURLException
	 ************************************************************************/
	
	private String toUrl(String testFile) throws MalformedURLException {
        return PacProxySelectorTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
	}


}