Java Source Code: org.apache.commons.httpclient.params.TestSSLTunnelParams


   1: /*
   2:  * $Header$
   3:  * $Revision: 515317 $
   4:  * $Date: 2007-03-06 22:41:04 +0100 (Tue, 06 Mar 2007) $
   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:  */
  29: 
  30: package org.apache.commons.httpclient.params;
  31: 
  32: import java.io.IOException;
  33: 
  34: import junit.framework.Test;
  35: import junit.framework.TestSuite;
  36: 
  37: import org.apache.commons.httpclient.FeedbackService;
  38: import org.apache.commons.httpclient.Header;
  39: import org.apache.commons.httpclient.HttpClientTestBase;
  40: import org.apache.commons.httpclient.HttpStatus;
  41: import org.apache.commons.httpclient.HttpVersion;
  42: import org.apache.commons.httpclient.methods.GetMethod;
  43: import org.apache.commons.httpclient.server.HttpRequestHandler;
  44: import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
  45: import org.apache.commons.httpclient.server.SimpleRequest;
  46: import org.apache.commons.httpclient.server.SimpleResponse;
  47: 
  48: /**
  49:  * Tunnelling proxy configuration.
  50:  *
  51:  * @author Oleg Kalnichevski
  52:  * 
  53:  * @version $Id: TestSSLTunnelParams.java 515317 2007-03-06 21:41:04Z sebb $
  54:  */
  55:	  public class TestSSLTunnelParams extends HttpClientTestBase {
  56:
  57:    // ------------------------------------------------------------ Constructor
  58:	      public TestSSLTunnelParams(final String testName) throws IOException {
  59:        super(testName);
  60:        setUseProxy(true);
  61:        setUseSSL(true);
  62:    }
  63:
  64:    // ------------------------------------------------------------------- Main
  65:	      public static void main(String args[]) {
  66:        String[] testCaseName = { TestSSLTunnelParams.class.getName() };
  67:        junit.textui.TestRunner.main(testCaseName);
  68:    }
  69:
  70:    // ------------------------------------------------------- TestCase Methods
  71:
  72:	      public static Test suite() {
  73:        TestSuite suite = new TestSuite(TestSSLTunnelParams.class);
  74:        return suite;
  75:    }
  76:
  77:    
  78:	      static class HttpVersionHandler implements HttpRequestHandler {
  79:        
  80:	          public HttpVersionHandler() {
  81:            super();
  82:        }
  83:
  84:        public boolean processRequest(
  85:                final SimpleHttpServerConnection conn,
  86:                final SimpleRequest request) throws IOException
  87:	              {
  88:                HttpVersion ver = request.getRequestLine().getHttpVersion();
  89:	                  if (ver.equals(HttpVersion.HTTP_1_0)) {
  90:                    return false;
  91:                } else {
  92:                    SimpleResponse response = new SimpleResponse();
  93:                    response.setStatusLine(ver, HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
  94:                    response.addHeader(new Header("Proxy-Connection", "close"));
  95:                    conn.setKeepAlive(false);
  96:                    // Make sure the request body is fully consumed
  97:                    request.getBodyBytes();
  98:                    conn.writeResponse(response);
  99:                    return true;
 100:                }
 101:            }
 102:        
 103:    }
 104:   
 105:    /**
 106:     * Tests correct proparation of HTTP params from the client to
 107:     * CONNECT method.
 108:     */
 109:	      public void testTunnellingParamsAgentLevel() throws IOException {
 110:        this.proxy.addHandler(new HttpVersionHandler());
 111:        this.server.setHttpService(new FeedbackService());
 112:
 113:        this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
 114:        GetMethod httpget = new GetMethod("/test/");
 115:	          try {
 116:            this.client.executeMethod(httpget);
 117:            assertNotNull(httpget.getStatusLine());
 118:            assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, 
 119:                    httpget.getStatusLine().getStatusCode());
 120:        } finally {
 121:            httpget.releaseConnection();
 122:        }
 123:
 124:        this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
 125:        httpget = new GetMethod("/test/");
 126:	          try {
 127:            this.client.executeMethod(httpget);
 128:            assertNotNull(httpget.getStatusLine());
 129:            assertEquals(HttpStatus.SC_OK, 
 130:                    httpget.getStatusLine().getStatusCode());
 131:        } finally {
 132:            httpget.releaseConnection();
 133:        }
 134:    }
 135:
 136:    /**
 137:     * Tests correct proparation of HTTP params from the host config to
 138:     * CONNECT method.
 139:     */
 140:	      public void testTunnellingParamsHostLevel() throws IOException {
 141:        this.proxy.addHandler(new HttpVersionHandler());
 142:        this.server.setHttpService(new FeedbackService());
 143:
 144:        this.client.getHostConfiguration().getParams().setParameter(
 145:                HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
 146:        GetMethod httpget = new GetMethod("/test/");
 147:	          try {
 148:            this.client.executeMethod(httpget);
 149:            assertNotNull(httpget.getStatusLine());
 150:            assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, 
 151:                    httpget.getStatusLine().getStatusCode());
 152:        } finally {
 153:            httpget.releaseConnection();
 154:        }
 155:
 156:        this.client.getHostConfiguration().getParams().setParameter(
 157:                HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
 158:        httpget = new GetMethod("/test/");
 159:	          try {
 160:            this.client.executeMethod(httpget);
 161:            assertNotNull(httpget.getStatusLine());
 162:            assertEquals(HttpStatus.SC_OK, 
 163:                    httpget.getStatusLine().getStatusCode());
 164:        } finally {
 165:            httpget.releaseConnection();
 166:        }
 167:    }
 168:
 169:    /**
 170:     * Tests ability to use HTTP/1.0 to execute CONNECT method and HTTP/1.1 to
 171:     * execute methods once the tunnel is established.
 172:     */
 173:	      public void testTunnellingParamsHostHTTP10AndMethodHTTP11() throws IOException {
 174:        this.proxy.addHandler(new HttpVersionHandler());
 175:        this.server.setHttpService(new FeedbackService());
 176:
 177:        this.client.getHostConfiguration().getParams().setParameter(
 178:                HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
 179:        GetMethod httpget = new GetMethod("/test/");
 180:        httpget.getParams().setVersion(HttpVersion.HTTP_1_1);
 181:	          try {
 182:            this.client.executeMethod(httpget);
 183:            assertNotNull(httpget.getStatusLine());
 184:            assertEquals(HttpStatus.SC_OK, 
 185:                    httpget.getStatusLine().getStatusCode());
 186:            assertEquals(HttpVersion.HTTP_1_1, 
 187:                    httpget.getEffectiveVersion());
 188:        } finally {
 189:            httpget.releaseConnection();
 190:        }
 191:    }
 192:}