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  package org.apache.commons.httpclient;
33  
34  import junit.framework.Test;
35  import junit.framework.TestCase;
36  import junit.framework.TestSuite;
37  
38  import org.apache.commons.httpclient.auth.AuthScope;
39  import org.apache.commons.httpclient.methods.GetMethod;
40  
41  /**
42   * Simple tests for HTTPS support in HttpClient.
43   *
44   * To run this test you'll need:
45   *  + a JSSE implementation installed (see README.txt)
46   *  + the java.protocol.handler.pkgs system property set
47   *    for your provider.  e.g.:
48   *     -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol
49   *    (see build.xml)
50   *
51   * @author Rodney Waldhoff
52   * @author Ortwin Gl?ck
53   * @version $Id: TestHttps.java 480424 2006-11-29 05:56:49Z bayard $
54   */
55  public class TestHttps extends TestCase {
56  
57      
58      private String _urlWithPort = null;
59      private String _urlWithoutPort = null;
60      private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
61      private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
62      private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser");
63      private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
64  
65      
66      public TestHttps(String testName) {
67          super(testName);
68      }
69  
70      
71      public static void main(String args[]) {
72          String[] testCaseName = { TestHttps.class.getName() };
73          junit.textui.TestRunner.main(testCaseName);
74      }
75  
76      
77      public static Test suite() {
78          return new TestSuite(TestHttps.class);
79      }
80  
81      public void setUp() throws Exception {
82          _urlWithPort = "https://www.verisign.com:443/";
83          _urlWithoutPort = "https://www.verisign.com/";
84      }
85  
86      public void testHttpsGet() {
87          HttpClient client = new HttpClient();
88          if (PROXY_HOST != null) {
89              if (PROXY_USER != null) {
90                  HttpState state = client.getState();
91                  state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
92                      PROXY_USER, PROXY_PASS));
93              }
94              client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
95          }
96          GetMethod method = new GetMethod(_urlWithPort);
97          
98          try {
99              client.executeMethod(method);
100         } catch (Throwable t) {
101             t.printStackTrace();
102             fail("Exception thrown during HTTPS GET: " + t.toString());
103         }
104 
105         try {
106             String data = method.getResponseBodyAsString();
107             
108             assertTrue("No data returned.", (data.length() > 0));
109         } catch (Throwable t) {
110             t.printStackTrace();
111             fail("Exception thrown while retrieving data : " + t.toString());
112         }
113     }
114 
115     public void testHttpsGetNoPort() {
116         HttpClient client = new HttpClient();
117         if (PROXY_HOST != null) {
118             if (PROXY_USER != null) {
119                 HttpState state = client.getState();
120                 state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
121                     PROXY_USER, PROXY_PASS));
122             }
123             client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
124         }
125         GetMethod method = new GetMethod(_urlWithoutPort);
126         
127         try {
128             client.executeMethod(method);
129         } catch (Throwable t) {
130             t.printStackTrace();
131             fail("Exception thrown during HTTPS GET: " + t.toString());
132         }
133 
134         try {
135             String data = method.getResponseBodyAsString();
136             
137             assertTrue("No data returned.", (data.length() > 0));
138         } catch (Throwable t) {
139             t.printStackTrace();
140             fail("Exception thrown while retrieving data : " + t.toString());
141         }
142     }
143 }