1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttps.java,v 1.12 2004/06/13 12:13:08 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.httpclient.auth.AuthScope;
38  import org.apache.commons.httpclient.methods.GetMethod;
39  
40  /***
41   * Simple tests for HTTPS support in HttpClient.
42   *
43   * To run this test you'll need:
44   *  + a JSSE implementation installed (see README.txt)
45   *  + the java.protocol.handler.pkgs system property set
46   *    for your provider.  e.g.:
47   *     -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol
48   *    (see build.xml)
49   *
50   * @author Rodney Waldhoff
51   * @author Ortwin Glück
52   * @version $Id: TestHttps.java 155418 2005-02-26 13:01:52Z dirkv $
53   */
54  public class TestHttps extends TestCase {
55  
56      // ---------------------------------------------------------------- Members
57      private String _urlWithPort = null;
58      private String _urlWithoutPort = null;
59      private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
60      private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
61      private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser");
62      private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
63  
64      // ------------------------------------------------------------ Constructor
65      public TestHttps(String testName) {
66          super(testName);
67      }
68  
69      // ------------------------------------------------------------------- Main
70      public static void main(String args[]) {
71          String[] testCaseName = { TestHttps.class.getName() };
72          junit.textui.TestRunner.main(testCaseName);
73      }
74  
75      // ------------------------------------------------------- TestCase Methods
76      public static Test suite() {
77          return new TestSuite(TestHttps.class);
78      }
79  
80      public void setUp() throws Exception {
81          _urlWithPort = "https://www.verisign.com:443/";
82          _urlWithoutPort = "https://www.verisign.com/";
83      }
84  
85      public void testHttpsGet() {
86          HttpClient client = new HttpClient();
87          if (PROXY_HOST != null) {
88              if (PROXY_USER != null) {
89                  HttpState state = client.getState();
90                  state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
91                      PROXY_USER, PROXY_PASS));
92              }
93              client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
94          }
95          GetMethod method = new GetMethod(_urlWithPort);
96          
97          try {
98              client.executeMethod(method);
99          } catch (Throwable t) {
100             t.printStackTrace();
101             fail("Exception thrown during HTTPS GET: " + t.toString());
102         }
103 
104         try {
105             String data = method.getResponseBodyAsString();
106             // This enumeration musn't be empty
107             assertTrue("No data returned.", (data.length() > 0));
108         } catch (Throwable t) {
109             t.printStackTrace();
110             fail("Exception thrown while retrieving data : " + t.toString());
111         }
112     }
113 
114     public void testHttpsGetNoPort() {
115         HttpClient client = new HttpClient();
116         if (PROXY_HOST != null) {
117             if (PROXY_USER != null) {
118                 HttpState state = client.getState();
119                 state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
120                     PROXY_USER, PROXY_PASS));
121             }
122             client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
123         }
124         GetMethod method = new GetMethod(_urlWithoutPort);
125         
126         try {
127             client.executeMethod(method);
128         } catch (Throwable t) {
129             t.printStackTrace();
130             fail("Exception thrown during HTTPS GET: " + t.toString());
131         }
132 
133         try {
134             String data = method.getResponseBodyAsString();
135             // This enumeration musn't be empty
136             assertTrue("No data returned.", (data.length() > 0));
137         } catch (Throwable t) {
138             t.printStackTrace();
139             fail("Exception thrown while retrieving data : " + t.toString());
140         }
141     }
142 }