KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestHttps


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttps.java,v 1.9.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.9.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
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.methods.GetMethod;
38
39 /**
40  * Simple tests for HTTPS support in HttpClient.
41  *
42  * To run this test you'll need:
43  * + a JSSE implementation installed (see README.txt)
44  * + the java.protocol.handler.pkgs system property set
45  * for your provider. e.g.:
46  * -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol
47  * (see build.xml)
48  *
49  * @author Rodney Waldhoff
50  * @author Ortwin Glück
51  * @version $Id: TestHttps.java,v 1.9.2.1 2004/02/22 18:21:16 olegk Exp $
52  */

53 public class TestHttps extends TestCase {
54
55     // ---------------------------------------------------------------- Members
56
private String JavaDoc _urlWithPort = null;
57     private String JavaDoc _urlWithoutPort = null;
58     private final String JavaDoc PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
59     private final String JavaDoc PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
60     private final String JavaDoc PROXY_USER = System.getProperty("httpclient.test.proxyUser");
61     private final String JavaDoc PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
62
63     // ------------------------------------------------------------ Constructor
64
public TestHttps(String JavaDoc testName) {
65         super(testName);
66     }
67
68     // ------------------------------------------------------------------- Main
69
public static void main(String JavaDoc args[]) {
70         String JavaDoc[] testCaseName = { TestHttps.class.getName() };
71         junit.textui.TestRunner.main(testCaseName);
72     }
73
74     // ------------------------------------------------------- TestCase Methods
75
public static Test suite() {
76         return new TestSuite(TestHttps.class);
77     }
78
79     public void setUp() throws Exception JavaDoc {
80         _urlWithPort = "https://www.verisign.com:443/";
81         _urlWithoutPort = "https://www.verisign.com/";
82     }
83
84     public void testHttpsGet() {
85         HttpClient client = new HttpClient();
86         if (PROXY_HOST != null) {
87             if (PROXY_USER != null) {
88                 HttpState state = client.getState();
89                 state.setProxyCredentials(null, new UsernamePasswordCredentials(
90                     PROXY_USER, PROXY_PASS));
91             }
92             client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
93         }
94         GetMethod method = new GetMethod(_urlWithPort);
95         
96         try {
97             client.executeMethod(method);
98         } catch (Throwable JavaDoc t) {
99             t.printStackTrace();
100             fail("Exception thrown during HTTPS GET: " + t.toString());
101         }
102
103         try {
104             String JavaDoc data = method.getResponseBodyAsString();
105             // This enumeration musn't be empty
106
assertTrue("No data returned.", (data.length() > 0));
107         } catch (Throwable JavaDoc t) {
108             t.printStackTrace();
109             fail("Exception thrown while retrieving data : " + t.toString());
110         }
111     }
112
113     public void testHttpsGetNoPort() {
114         HttpClient client = new HttpClient();
115         if (PROXY_HOST != null) {
116             if (PROXY_USER != null) {
117                 HttpState state = client.getState();
118                 state.setProxyCredentials(null, new UsernamePasswordCredentials(
119                     PROXY_USER, PROXY_PASS));
120             }
121             client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
122         }
123         GetMethod method = new GetMethod(_urlWithoutPort);
124         
125         try {
126             client.executeMethod(method);
127         } catch (Throwable JavaDoc t) {
128             t.printStackTrace();
129             fail("Exception thrown during HTTPS GET: " + t.toString());
130         }
131
132         try {
133             String JavaDoc data = method.getResponseBodyAsString();
134             // This enumeration musn't be empty
135
assertTrue("No data returned.", (data.length() > 0));
136         } catch (Throwable JavaDoc t) {
137             t.printStackTrace();
138             fail("Exception thrown while retrieving data : " + t.toString());
139         }
140     }
141 }
142
Popular Tags