4
* $Date: 2007-03-06 22:41:04 +0100 (Tue, 06 Mar 2007) $
5
* ====================================================================
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
* ====================================================================
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/>.
30
package org.apache.commons.httpclient.params;
32
import java.io.IOException;
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
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;
49
* Tunnelling proxy configuration.
51
* @author Oleg Kalnichevski
53
* @version $Id: TestSSLTunnelParams.java 515317 2007-03-06 21:41:04Z sebb $
55
public class TestSSLTunnelParams extends HttpClientTestBase {
57
// ------------------------------------------------------------ Constructor
58
public TestSSLTunnelParams(final String testName) throws IOException {
64
// ------------------------------------------------------------------- Main
65
public static void main(String args[]) {
66
String[] testCaseName = { TestSSLTunnelParams.class.getName() };
67
junit.textui.TestRunner.main(testCaseName);
70
// ------------------------------------------------------- TestCase Methods
72
public static Test suite() {
73
TestSuite suite = new TestSuite(TestSSLTunnelParams.class);
78
static class HttpVersionHandler implements HttpRequestHandler {
80
public HttpVersionHandler() {
84
public boolean processRequest(
85
final SimpleHttpServerConnection conn,
86
final SimpleRequest request) throws IOException
88
HttpVersion ver = request.getRequestLine().getHttpVersion();
89
if (ver.equals(HttpVersion.HTTP_1_0)) {
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);
106
* Tests correct proparation of HTTP params from the client to
109
public void testTunnellingParamsAgentLevel() throws IOException {
110
this.proxy.addHandler(new HttpVersionHandler());
111
this.server.setHttpService(new FeedbackService());
113
this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
114
GetMethod httpget = new GetMethod("/test/");
116
this.client.executeMethod(httpget);
117
assertNotNull(httpget.getStatusLine());
118
assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
119
httpget.getStatusLine().getStatusCode());
121
httpget.releaseConnection();
124
this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
125
httpget = new GetMethod("/test/");
127
this.client.executeMethod(httpget);
128
assertNotNull(httpget.getStatusLine());
129
assertEquals(HttpStatus.SC_OK,
130
httpget.getStatusLine().getStatusCode());
132
httpget.releaseConnection();
137
* Tests correct proparation of HTTP params from the host config to
140
public void testTunnellingParamsHostLevel() throws IOException {
141
this.proxy.addHandler(new HttpVersionHandler());
142
this.server.setHttpService(new FeedbackService());
144
this.client.getHostConfiguration().getParams().setParameter(
145
HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
146
GetMethod httpget = new GetMethod("/test/");
148
this.client.executeMethod(httpget);
149
assertNotNull(httpget.getStatusLine());
150
assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
151
httpget.getStatusLine().getStatusCode());
153
httpget.releaseConnection();
156
this.client.getHostConfiguration().getParams().setParameter(
157
HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
158
httpget = new GetMethod("/test/");
160
this.client.executeMethod(httpget);
161
assertNotNull(httpget.getStatusLine());
162
assertEquals(HttpStatus.SC_OK,
163
httpget.getStatusLine().getStatusCode());
165
httpget.releaseConnection();
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.
173
public void testTunnellingParamsHostHTTP10AndMethodHTTP11() throws IOException {
174
this.proxy.addHandler(new HttpVersionHandler());
175
this.server.setHttpService(new FeedbackService());
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);
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());
189
httpget.releaseConnection();