1 package com.atlassian.sal.core.net;
2
3 import org.junit.Test;
4
5 import static org.junit.Assert.assertFalse;
6 import static org.junit.Assert.assertTrue;
7
8 public class ProxyUtilTest {
9 @Test
10 public void requiresAuthentication() {
11 assertFalse(ProxyUtil.requiresAuthentication(getConfig(null, null, new String[0]),
12 "http://localhost:6990/bamboo"));
13 assertTrue(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[0]),
14 "http://localhost:6990/bamboo"));
15 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"localhost"}),
16 "http://localhost:6990/bamboo"));
17 assertTrue(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"localhost"}),
18 "http://bamboo.localhost:6990/bamboo"));
19 assertTrue(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"localhost"}),
20 "http://@#$@#$:6990/bamboo"));
21 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"localhost"}),
22 "http://:6990/bamboo"));
23 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"localhost", "127.0.0.1"}),
24 "http://localhost:6990/bamboo"));
25 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"127.0.0.1", "*.localhost"}),
26 "http://user.localhost:6990/bamboo"));
27 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"*localhost"}),
28 "http://user.localhost:6990/bamboo"));
29 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"127.0.*", "*.localhost"}),
30 "http://127.0.0.1:6990/bamboo"));
31 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"127.0.0.*"}),
32 "http://127.0.0.1:6990/bamboo"));
33 }
34
35 @Test
36 public void literal_IPV6_address_match() {
37 assertFalse(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"[::1]"}),
38 "http://[::1]/bamboo"));
39 assertTrue(ProxyUtil.requiresAuthentication(getConfig("admin", "admin", new String[]{"[::1]"}),
40 "http://[::1:1]/bamboo"));
41 }
42
43 private ProxyConfig getConfig(final String user, final String password, final String[] nonProxyHosts) {
44 return new ProxyConfig() {
45 @Override
46 public boolean isSet() {
47 return true;
48 }
49
50 @Override
51 public boolean requiresAuthentication() {
52 return getUser() != null;
53 }
54
55 @Override
56 public String getHost() {
57 return null;
58 }
59
60 @Override
61 public int getPort() {
62 return 0;
63 }
64
65 @Override
66 public String getUser() {
67 return user;
68 }
69
70 @Override
71 public String getPassword() {
72 return password;
73 }
74
75 @Override
76 public String[] getNonProxyHosts() {
77 return nonProxyHosts;
78 }
79 };
80 }
81 }