View Javadoc

1   package com.atlassian.httpclient.apache.httpcomponents.proxy;
2   
3   import com.atlassian.fugue.Option;
4   import com.google.common.collect.Iterables;
5   import org.apache.http.HttpHost;
6   import org.apache.http.auth.UsernamePasswordCredentials;
7   import org.hamcrest.Matchers;
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.contrib.java.lang.system.ClearSystemProperties;
11  
12  import static org.hamcrest.Matchers.instanceOf;
13  import static org.hamcrest.Matchers.is;
14  import static org.junit.Assert.assertThat;
15  
16  public class SystemPropertiesProxyConfigTest {
17      @Rule
18      public ClearSystemProperties clearSystemPropertiesRule =
19              new ClearSystemProperties(ProxyTestUtils.getProxyPropertiesNames());
20  
21      @Test
22      public void httpsProxyConfigured() {
23          System.setProperty("https.proxyHost", "localhost");
24          System.setProperty("https.proxyPort", "3128");
25          ProxyConfig config = new SystemPropertiesProxyConfig();
26          Option<HttpHost> proxy = config.getProxyHost();
27  
28          assertThat(proxy.isDefined(), is(true));
29          assertThat(proxy.get().getHostName(), is("localhost"));
30          assertThat(proxy.get().getPort(), is(3128));
31      }
32  
33      @Test
34      public void httpProxyConfigured() {
35          System.setProperty("http.proxyHost", "localhost");
36          System.setProperty("http.proxyPort", "3128");
37          ProxyConfig config = new SystemPropertiesProxyConfig();
38          Option<HttpHost> proxy = config.getProxyHost();
39  
40          assertThat(proxy.isDefined(), is(true));
41          assertThat(proxy.get().getHostName(), is("localhost"));
42          assertThat(proxy.get().getPort(), is(3128));
43      }
44  
45      @Test
46      public void httpProxyNotConfigured() {
47          ProxyConfig config = new SystemPropertiesProxyConfig();
48          Option<HttpHost> proxy = config.getProxyHost();
49  
50          assertThat(proxy.isEmpty(), is(true));
51      }
52  
53      @Test
54      public void proxyUserForHttpConfigured() {
55          System.setProperty("http.proxyHost", "localhost");
56          System.setProperty("http.proxyPort", "3128");
57          System.setProperty("http.proxyUser", "user");
58          System.setProperty("http.proxyPassword", "password");
59  
60          ProxyConfig config = new SystemPropertiesProxyConfig();
61          Iterable<ProxyConfig.AuthenticationInfo> authenticationInfos = config.getAuthenticationInfo();
62  
63          assertThat(authenticationInfos, Matchers.iterableWithSize(1));
64  
65          final ProxyConfig.AuthenticationInfo authenticationInfo = Iterables.getOnlyElement(authenticationInfos);
66  
67          assertThat(authenticationInfo.getCredentials().isDefined(), is(true));
68          assertThat(authenticationInfo.getCredentials().get(), instanceOf(UsernamePasswordCredentials.class));
69  
70          UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) authenticationInfo.getCredentials().get();
71  
72          assertThat(credentials.getPassword(), is("password"));
73          assertThat(credentials.getUserName(), is("user"));
74      }
75  
76      @Test
77      public void proxyUserForHttpsConfigured() {
78          System.setProperty("https.proxyHost", "localhost");
79          System.setProperty("https.proxyPort", "3128");
80          System.setProperty("https.proxyUser", "user");
81          System.setProperty("https.proxyPassword", "password");
82  
83          ProxyConfig config = new SystemPropertiesProxyConfig();
84          Iterable<ProxyConfig.AuthenticationInfo> authenticationInfos = config.getAuthenticationInfo();
85  
86          assertThat(authenticationInfos, Matchers.iterableWithSize(1));
87  
88          final ProxyConfig.AuthenticationInfo authenticationInfo = Iterables.getOnlyElement(authenticationInfos);
89  
90          assertThat(authenticationInfo.getCredentials().isDefined(), is(true));
91          assertThat(authenticationInfo.getCredentials().get(), instanceOf(UsernamePasswordCredentials.class));
92  
93          UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) authenticationInfo.getCredentials().get();
94  
95          assertThat(credentials.getPassword(), is("password"));
96          assertThat(credentials.getUserName(), is("user"));
97      }
98  
99      @Test
100     public void proxyAndProxyUserNotConfigured() {
101         ProxyConfig config = new SystemPropertiesProxyConfig();
102         final Iterable<ProxyConfig.AuthenticationInfo> authenticationInfo = config.getAuthenticationInfo();
103 
104         assertThat(authenticationInfo, Matchers.iterableWithSize(0));
105 
106         final Option<HttpHost> proxyHost = config.getProxyHost();
107 
108         assertThat(proxyHost.isDefined(), Matchers.is(false));
109     }
110 
111     @Test
112     public void proxyUserNotConfigured() {
113         System.setProperty("https.proxyHost", "localhost");
114         System.setProperty("https.proxyPort", "3128");
115 
116         ProxyConfig config = new SystemPropertiesProxyConfig();
117 
118         final Iterable<ProxyConfig.AuthenticationInfo> authenticationInfos = config.getAuthenticationInfo();
119         assertThat(authenticationInfos, Matchers.iterableWithSize(1));
120 
121         final ProxyConfig.AuthenticationInfo authenticationInfo = Iterables.getOnlyElement(authenticationInfos);
122         assertThat(authenticationInfo.getCredentials().isDefined(), Matchers.is(false));
123 
124         final Option<HttpHost> proxyHost = config.getProxyHost();
125         assertThat(proxyHost.isDefined(), Matchers.is(true));
126     }
127 
128 }