001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: import org.apache.commons.httpclient.auth.AuthScope;
039: import org.apache.commons.httpclient.methods.GetMethod;
040:
041: /**
042: * Simple tests for HTTPS support in HttpClient.
043: *
044: * To run this test you'll need:
045: * + a JSSE implementation installed (see README.txt)
046: * + the java.protocol.handler.pkgs system property set
047: * for your provider. e.g.:
048: * -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol
049: * (see build.xml)
050: *
051: * @author Rodney Waldhoff
052: * @author Ortwin Glück
053: * @version $Id: TestHttps.java 480424 2006-11-29 05:56:49Z bayard $
054: */
055: public class TestHttps extends TestCase {
056:
057: // ---------------------------------------------------------------- Members
058: private String _urlWithPort = null;
059: private String _urlWithoutPort = null;
060: private final String PROXY_HOST = System
061: .getProperty("httpclient.test.proxyHost");
062: private final String PROXY_PORT = System
063: .getProperty("httpclient.test.proxyPort");
064: private final String PROXY_USER = System
065: .getProperty("httpclient.test.proxyUser");
066: private final String PROXY_PASS = System
067: .getProperty("httpclient.test.proxyPass");
068:
069: // ------------------------------------------------------------ Constructor
070: public TestHttps(String testName) {
071: super (testName);
072: }
073:
074: // ------------------------------------------------------------------- Main
075: public static void main(String args[]) {
076: String[] testCaseName = { TestHttps.class.getName() };
077: junit.textui.TestRunner.main(testCaseName);
078: }
079:
080: // ------------------------------------------------------- TestCase Methods
081: public static Test suite() {
082: return new TestSuite(TestHttps.class);
083: }
084:
085: public void setUp() throws Exception {
086: _urlWithPort = "https://www.verisign.com:443/";
087: _urlWithoutPort = "https://www.verisign.com/";
088: }
089:
090: public void testHttpsGet() {
091: HttpClient client = new HttpClient();
092: if (PROXY_HOST != null) {
093: if (PROXY_USER != null) {
094: HttpState state = client.getState();
095: state.setProxyCredentials(AuthScope.ANY,
096: new UsernamePasswordCredentials(PROXY_USER,
097: PROXY_PASS));
098: }
099: client.getHostConfiguration().setProxy(PROXY_HOST,
100: Integer.parseInt(PROXY_PORT));
101: }
102: GetMethod method = new GetMethod(_urlWithPort);
103:
104: try {
105: client.executeMethod(method);
106: } catch (Throwable t) {
107: t.printStackTrace();
108: fail("Exception thrown during HTTPS GET: " + t.toString());
109: }
110:
111: try {
112: String data = method.getResponseBodyAsString();
113: // This enumeration musn't be empty
114: assertTrue("No data returned.", (data.length() > 0));
115: } catch (Throwable t) {
116: t.printStackTrace();
117: fail("Exception thrown while retrieving data : "
118: + t.toString());
119: }
120: }
121:
122: public void testHttpsGetNoPort() {
123: HttpClient client = new HttpClient();
124: if (PROXY_HOST != null) {
125: if (PROXY_USER != null) {
126: HttpState state = client.getState();
127: state.setProxyCredentials(AuthScope.ANY,
128: new UsernamePasswordCredentials(PROXY_USER,
129: PROXY_PASS));
130: }
131: client.getHostConfiguration().setProxy(PROXY_HOST,
132: Integer.parseInt(PROXY_PORT));
133: }
134: GetMethod method = new GetMethod(_urlWithoutPort);
135:
136: try {
137: client.executeMethod(method);
138: } catch (Throwable t) {
139: t.printStackTrace();
140: fail("Exception thrown during HTTPS GET: " + t.toString());
141: }
142:
143: try {
144: String data = method.getResponseBodyAsString();
145: // This enumeration musn't be empty
146: assertTrue("No data returned.", (data.length() > 0));
147: } catch (Throwable t) {
148: t.printStackTrace();
149: fail("Exception thrown while retrieving data : "
150: + t.toString());
151: }
152: }
153: }
|