1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestHttps.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
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 608014 2008-01-02 05:48:53Z rolandw $
54   */
55  public class TestHttps extends TestCase {
56  
57      // ---------------------------------------------------------------- Members
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      // ------------------------------------------------------------ Constructor
66      public TestHttps(String testName) {
67          super(testName);
68      }
69  
70      // ------------------------------------------------------------------- Main
71      public static void main(String args[]) {
72          String[] testCaseName = { TestHttps.class.getName() };
73          junit.textui.TestRunner.main(testCaseName);
74      }
75  
76      // ------------------------------------------------------- TestCase Methods
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             // This enumeration musn't be empty
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             // This enumeration musn't be empty
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 }