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