1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
57 public TestSSLTunnelParams(final String testName) throws IOException {
58 super(testName);
59 setUseProxy(true);
60 setUseSSL(true);
61 }
62
63
64 public static void main(String args[]) {
65 String[] testCaseName = { TestSSLTunnelParams.class.getName() };
66 junit.textui.TestRunner.main(testCaseName);
67 }
68
69
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
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 }