Debian Codegraph @

src/test/java/capsule/SystemProxySelectorTest.java

raw symbols SCIP
1 package capsule;
2
3 import static org.junit.Assert.assertArrayEquals;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Properties;
11
12 import org.eclipse.aether.repository.Proxy;
13 import org.eclipse.aether.repository.RemoteRepository;
14 import org.junit.Test;
15
16 /**
17 * @author Paolo Di Tommaso <[email protected]>
18 * @author [email protected]
19 */
20 public class SystemProxySelectorTest {
21
22 @Test
23 public void testProxyWithEnvProperties() {
24 Map<String, String> env = new HashMap<>();
25 env.put("http_proxy", "my.proxy.com:8080");
26 env.put("https_proxy", "secure.proxy.com:8888");
27
28 SystemProxySelector selector = new SystemProxySelector(env, new Properties(), 2);
29 assertEquals(2, selector.getCount());
30
31 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
32 Proxy proxy = selector.getProxy(repo);
33 assertEquals("https", proxy.getType());
34 assertEquals("secure.proxy.com", proxy.getHost());
35 assertEquals(8888, proxy.getPort());
36
37 repo = new RemoteRepository.Builder("bar", null, "http://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
38 proxy = selector.getProxy(repo);
39 assertEquals("http", proxy.getType());
40 assertEquals("my.proxy.com", proxy.getHost());
41 assertEquals(8080, proxy.getPort());
42 }
43
44 @Test
45 public void testProxyWithEnvPropertiesAndDefaultPorts() {
46 Map<String, String> env = new HashMap<>();
47 env.put("http_proxy", "http://my.proxy.com");
48 env.put("https_proxy", "https://secure.proxy.com");
49
50 SystemProxySelector selector = new SystemProxySelector(env, new Properties(), 0);
51 assertEquals(2, selector.getCount());
52
53 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
54 Proxy proxy = selector.getProxy(repo);
55 assertEquals("https", proxy.getType());
56 assertEquals("secure.proxy.com", proxy.getHost());
57 assertEquals(443, proxy.getPort());
58
59 repo = new RemoteRepository.Builder("bar", null, "http://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
60 proxy = selector.getProxy(repo);
61 assertEquals("http", proxy.getType());
62 assertEquals("my.proxy.com", proxy.getHost());
63 assertEquals(80, proxy.getPort());
64 }
65
66 @Test
67 public void testProxyWithEnvPropertiesAndNoProxyHosts() {
68 Map<String, String> env = new HashMap<>();
69 env.put("http_proxy", "http://my.proxy.com");
70 env.put("https_proxy", "https://secure.proxy.com");
71 env.put("no_proxy", "*.foo.com,localhost");
72
73 SystemProxySelector selector = new SystemProxySelector(env, new Properties(), 2);
74 assertEquals(2, selector.getCount());
75
76 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "http://localhost/service/foo-bar-0.14.3.jar").build();
77 Proxy proxy = selector.getProxy(repo);
78 assertNull(proxy);
79
80 RemoteRepository repo2 = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
81 Proxy proxy2 = selector.getProxy(repo2);
82 assertEquals("https", proxy2.getType());
83 assertEquals("secure.proxy.com", proxy2.getHost());
84 assertEquals(443, proxy2.getPort());
85 }
86
87 @Test
88 public void testProxyWithEnvPropertiesAndCredentials() {
89 Map<String, String> env = new HashMap<>();
90 env.put("https_proxy", "https://user:[email protected]");
91
92 SystemProxySelector selector = new SystemProxySelector(env, new Properties(), 2);
93 assertEquals(1, selector.getCount());
94
95 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
96 Proxy proxy = selector.getProxy(repo);
97 assertEquals("https", proxy.getType());
98 assertEquals("secure.proxy.com", proxy.getHost());
99 assertEquals(443, proxy.getPort());
100 assertNotNull(proxy.getAuthentication());
101 assertEquals("username=user, password=***", proxy.getAuthentication().toString()); // no way to retrieve password
102
103 }
104
105 @Test
106 public void testProxyWithSystemPropertiesAndCredentials() {
107 Properties props = new Properties();
108 props.setProperty("https.proxyHost", "secure.host.name");
109 props.setProperty("https.proxyUser", "user");
110 props.setProperty("https.proxyPassword", "password");
111
112 SystemProxySelector selector = new SystemProxySelector(new HashMap(), props, 0);
113 assertEquals(1, selector.getCount());
114
115 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
116 Proxy proxy = selector.getProxy(repo);
117 assertEquals("https", proxy.getType());
118 assertEquals("secure.host.name", proxy.getHost());
119 assertEquals(443, proxy.getPort());
120 assertNotNull(proxy.getAuthentication());
121 assertEquals("username=user, password=***", proxy.getAuthentication().toString()); // no way to retrieve password
122 }
123
124 @Test
125 public void testProxyWithSystemPropertiesAndDefaultPorts() {
126 Properties props = new Properties();
127 props.setProperty("http.proxyHost", "foo.host.name");
128 props.setProperty("https.proxyHost", "secure.host.name");
129
130 SystemProxySelector selector = new SystemProxySelector(new HashMap(), props, 0);
131 assertEquals(2, selector.getCount());
132
133 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
134 Proxy proxy = selector.getProxy(repo);
135 assertEquals("https", proxy.getType());
136 assertEquals("secure.host.name", proxy.getHost());
137 assertEquals(443, proxy.getPort());
138
139 repo = new RemoteRepository.Builder("bar", null, "http://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
140 proxy = selector.getProxy(repo);
141 assertEquals("http", proxy.getType());
142 assertEquals("foo.host.name", proxy.getHost());
143 assertEquals(80, proxy.getPort());
144 }
145
146 @Test
147 public void testProxyWithSystemPropertiesAndCustomPorts() {
148 // environment conf
149 Map<String, String> env = new HashMap<>();
150 env.put("http_proxy", "env1.proxy.com:8080");
151 env.put("https_proxy", "env2.secure.com:8888");
152
153 // system properties
154 Properties props = new Properties();
155 props.setProperty("http.proxyHost", "prop1.host.name");
156 props.setProperty("http.proxyPort", "8081");
157 props.setProperty("https.proxyHost", "prop2.secure.name");
158 props.setProperty("https.proxyPort", "9091");
159
160 // it must use the proxy defined in the system properties
161 // because it is supposed to override the one defined with environment variables
162 SystemProxySelector selector = new SystemProxySelector(env, props, 2);
163 assertEquals(4, selector.getCount());
164
165 RemoteRepository repo = new RemoteRepository.Builder("bar", null, "https://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
166 Proxy proxy = selector.getProxy(repo);
167 assertEquals("https", proxy.getType());
168 assertEquals("prop2.secure.name", proxy.getHost());
169 assertEquals(9091, proxy.getPort());
170
171 repo = new RemoteRepository.Builder("bar", null, "http://oss.sonatype.org/service/foo-bar-0.14.3.jar").build();
172 proxy = selector.getProxy(repo);
173 assertEquals("http", proxy.getType());
174 assertEquals("prop1.host.name", proxy.getHost());
175 assertEquals(8081, proxy.getPort());
176 }
177
178 @Test
179 public void testParseProxy() {
180 assertArrayEquals(new String[]{"foo.bar.org", "81"}, SystemProxySelector.parseProxy("foo.bar.org", "81"));
181 assertArrayEquals(new String[]{"foo.bar.org", "8080"}, SystemProxySelector.parseProxy("foo.bar.org:8080", "91"));
182 assertArrayEquals(new String[]{"foo.com", "81"}, SystemProxySelector.parseProxy("http://foo.com", "81"));
183 assertArrayEquals(new String[] {"foo.com", "81"}, SystemProxySelector.parseProxy("http://foo.com/", "81"));
184 assertArrayEquals(new String[]{"foo.com", "81"}, SystemProxySelector.parseProxy("http://foo.com/hello", "81"));
185 assertArrayEquals(new String[]{"foo.com", "8080"}, SystemProxySelector.parseProxy("http://foo.com:8080", "81"));
186 assertArrayEquals(new String[]{"foo.com", "8080"}, SystemProxySelector.parseProxy("http://foo.com:8080/", "81"));
187 assertArrayEquals(new String[]{"foo.com", "8080"}, SystemProxySelector.parseProxy("http://foo.com:8080/hello", "81"));
188 assertArrayEquals(new String[]{"foo.com", "81"}, SystemProxySelector.parseProxy("http://user:[email protected]", "81"));
189 assertArrayEquals(new String[]{"foo.com", "8080"}, SystemProxySelector.parseProxy("http://user:[email protected]:8080", "81"));
190 }
191
192 @Test
193 public void testParseCredentials() {
194 assertNull(SystemProxySelector.parseCredentials("foo.bar.org"));
195 assertNull(SystemProxySelector.parseCredentials("foo.bar.org:8080"));
196 assertNull(SystemProxySelector.parseCredentials("http://foo.com"));
197 assertNull(SystemProxySelector.parseCredentials("http://foo.com:8080"));
198 assertArrayEquals(new String[]{"user", "password"}, SystemProxySelector.parseCredentials("user:[email protected]"));
199 assertArrayEquals(new String[]{"user", "password"}, SystemProxySelector.parseCredentials("user:[email protected]:8080"));
200 assertArrayEquals(new String[]{"user", "password"}, SystemProxySelector.parseCredentials("http://user:[email protected]"));
201 assertArrayEquals(new String[]{"user", "password"}, SystemProxySelector.parseCredentials("http://user:[email protected]:8080"));
202 assertArrayEquals(new String[]{"user", null}, SystemProxySelector.parseCredentials("http://[email protected]:8080"));
203 assertArrayEquals(new String[]{"user", "password"}, SystemProxySelector.parseCredentials("http://user:[email protected]:8080/"));
204 assertArrayEquals(new String[]{"user", null}, SystemProxySelector.parseCredentials("http://[email protected]:8080/"));
205 }
206 }