001: /*
002: * $Header$
003: * $Revision: 515317 $
004: * $Date: 2007-03-06 22:41:04 +0100 (Tue, 06 Mar 2007) $
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: */
029:
030: package org.apache.commons.httpclient.params;
031:
032: import java.io.IOException;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.apache.commons.httpclient.FeedbackService;
038: import org.apache.commons.httpclient.Header;
039: import org.apache.commons.httpclient.HttpClientTestBase;
040: import org.apache.commons.httpclient.HttpStatus;
041: import org.apache.commons.httpclient.HttpVersion;
042: import org.apache.commons.httpclient.methods.GetMethod;
043: import org.apache.commons.httpclient.server.HttpRequestHandler;
044: import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
045: import org.apache.commons.httpclient.server.SimpleRequest;
046: import org.apache.commons.httpclient.server.SimpleResponse;
047:
048: /**
049: * Tunnelling proxy configuration.
050: *
051: * @author Oleg Kalnichevski
052: *
053: * @version $Id: TestSSLTunnelParams.java 515317 2007-03-06 21:41:04Z sebb $
054: */
055: public class TestSSLTunnelParams extends HttpClientTestBase {
056:
057: // ------------------------------------------------------------ Constructor
058: public TestSSLTunnelParams(final String testName)
059: throws IOException {
060: super (testName);
061: setUseProxy(true);
062: setUseSSL(true);
063: }
064:
065: // ------------------------------------------------------------------- Main
066: public static void main(String args[]) {
067: String[] testCaseName = { TestSSLTunnelParams.class.getName() };
068: junit.textui.TestRunner.main(testCaseName);
069: }
070:
071: // ------------------------------------------------------- TestCase Methods
072:
073: public static Test suite() {
074: TestSuite suite = new TestSuite(TestSSLTunnelParams.class);
075: return suite;
076: }
077:
078: static class HttpVersionHandler implements HttpRequestHandler {
079:
080: public HttpVersionHandler() {
081: super ();
082: }
083:
084: public boolean processRequest(
085: final SimpleHttpServerConnection conn,
086: final SimpleRequest request) throws IOException {
087: HttpVersion ver = request.getRequestLine().getHttpVersion();
088: if (ver.equals(HttpVersion.HTTP_1_0)) {
089: return false;
090: } else {
091: SimpleResponse response = new SimpleResponse();
092: response.setStatusLine(ver,
093: HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
094: response.addHeader(new Header("Proxy-Connection",
095: "close"));
096: conn.setKeepAlive(false);
097: // Make sure the request body is fully consumed
098: request.getBodyBytes();
099: conn.writeResponse(response);
100: return true;
101: }
102: }
103:
104: }
105:
106: /**
107: * Tests correct proparation of HTTP params from the client to
108: * CONNECT method.
109: */
110: public void testTunnellingParamsAgentLevel() throws IOException {
111: this .proxy.addHandler(new HttpVersionHandler());
112: this .server.setHttpService(new FeedbackService());
113:
114: this .client.getParams().setVersion(HttpVersion.HTTP_1_1);
115: GetMethod httpget = new GetMethod("/test/");
116: try {
117: this .client.executeMethod(httpget);
118: assertNotNull(httpget.getStatusLine());
119: assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
120: httpget.getStatusLine().getStatusCode());
121: } finally {
122: httpget.releaseConnection();
123: }
124:
125: this .client.getParams().setVersion(HttpVersion.HTTP_1_0);
126: httpget = new GetMethod("/test/");
127: try {
128: this .client.executeMethod(httpget);
129: assertNotNull(httpget.getStatusLine());
130: assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
131: .getStatusCode());
132: } finally {
133: httpget.releaseConnection();
134: }
135: }
136:
137: /**
138: * Tests correct proparation of HTTP params from the host config to
139: * CONNECT method.
140: */
141: public void testTunnellingParamsHostLevel() throws IOException {
142: this .proxy.addHandler(new HttpVersionHandler());
143: this .server.setHttpService(new FeedbackService());
144:
145: this .client.getHostConfiguration().getParams()
146: .setParameter(HttpMethodParams.PROTOCOL_VERSION,
147: HttpVersion.HTTP_1_1);
148: GetMethod httpget = new GetMethod("/test/");
149: try {
150: this .client.executeMethod(httpget);
151: assertNotNull(httpget.getStatusLine());
152: assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
153: httpget.getStatusLine().getStatusCode());
154: } finally {
155: httpget.releaseConnection();
156: }
157:
158: this .client.getHostConfiguration().getParams()
159: .setParameter(HttpMethodParams.PROTOCOL_VERSION,
160: HttpVersion.HTTP_1_0);
161: httpget = new GetMethod("/test/");
162: try {
163: this .client.executeMethod(httpget);
164: assertNotNull(httpget.getStatusLine());
165: assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
166: .getStatusCode());
167: } finally {
168: httpget.releaseConnection();
169: }
170: }
171:
172: /**
173: * Tests ability to use HTTP/1.0 to execute CONNECT method and HTTP/1.1 to
174: * execute methods once the tunnel is established.
175: */
176: public void testTunnellingParamsHostHTTP10AndMethodHTTP11()
177: throws IOException {
178: this .proxy.addHandler(new HttpVersionHandler());
179: this .server.setHttpService(new FeedbackService());
180:
181: this .client.getHostConfiguration().getParams()
182: .setParameter(HttpMethodParams.PROTOCOL_VERSION,
183: HttpVersion.HTTP_1_0);
184: GetMethod httpget = new GetMethod("/test/");
185: httpget.getParams().setVersion(HttpVersion.HTTP_1_1);
186: try {
187: this.client.executeMethod(httpget);
188: assertNotNull(httpget.getStatusLine());
189: assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
190: .getStatusCode());
191: assertEquals(HttpVersion.HTTP_1_1, httpget
192: .getEffectiveVersion());
193: } finally {
194: httpget.releaseConnection();
195: }
196: }
197: }
|