mbecke 2004/10/31 18:21:15
Modified: httpclient/src/test/org/apache/commons/httpclient
TestHttpMethodFundamentals.java TestRedirects.java
TestPostMethod.java TestMultipartPost.java
HttpClientTestBase.java
httpclient/src/test/org/apache/commons/httpclient/server
ProxyRequestHandler.java
httpclient/src/test/org/apache/commons/httpclient/auth
TestBasicAuth.java
Added: httpclient/src/test/org/apache/commons/httpclient
ProxyTestDecorator.java
Log:
Enhanced the ProxyRequestHandler to support more methods and chunking.
Added ProxyTestDecorator to support reusing existing test cases via a proxy.
Revision Changes Path
1.4 +7 -5
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java
Index: TestHttpMethodFundamentals.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestHttpMethodFundamentals.java 31 Oct 2004 14:42:59 -0000 1.3
+++ TestHttpMethodFundamentals.java 1 Nov 2004 02:21:15 -0000 1.4
@@ -56,7 +56,9 @@
}
public static Test suite() {
- return new TestSuite(TestHttpMethodFundamentals.class);
+ TestSuite suite = new TestSuite(TestHttpMethodFundamentals.class);
+ ProxyTestDecorator.addTests(suite);
+ return suite;
}
public static void main(String args[]) {
1.6 +8 -7
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java
Index: TestRedirects.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestRedirects.java 31 Oct 2004 14:42:59 -0000 1.5
+++ TestRedirects.java 1 Nov 2004 02:21:15 -0000 1.6
@@ -70,7 +70,9 @@
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
- return new TestSuite(TestRedirects.class);
+ TestSuite suite = new TestSuite(TestRedirects.class);
+ ProxyTestDecorator.addTests(suite);
+ return suite;
}
private class BasicRedirectService implements HttpService {
@@ -480,8 +482,7 @@
}
Protocol.unregisterProtocol("test");
- }
-
+ }
public void testRedirectWithCookie() throws IOException {
1.3 +10 -8
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java
Index: TestPostMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPostMethod.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestPostMethod.java 31 Oct 2004 14:42:59 -0000 1.2
+++ TestPostMethod.java 1 Nov 2004 02:21:15 -0000 1.3
@@ -33,13 +33,13 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
/**
* Tests basic method functionality.
*
@@ -65,7 +65,9 @@
}
public static Test suite() {
- return new TestSuite(TestPostMethod.class);
+ TestSuite suite = new TestSuite(TestPostMethod.class);
+ ProxyTestDecorator.addTests(suite);
+ return suite;
}
public static void main(String args[]) {
1.3 +4 -3
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMultipartPost.java
Index: TestMultipartPost.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMultipartPost.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestMultipartPost.java 31 Oct 2004 14:42:59 -0000 1.2
+++ TestMultipartPost.java 1 Nov 2004 02:21:15 -0000 1.3
@@ -56,6 +56,7 @@
public static Test suite() {
TestSuite suite = new TestSuite(TestMultipartPost.class);
+ ProxyTestDecorator.addTests(suite);
return suite;
}
1.5 +23 -6
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java
Index: HttpClientTestBase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- HttpClientTestBase.java 31 Oct 2004 18:02:02 -0000 1.4
+++ HttpClientTestBase.java 1 Nov 2004 02:21:15 -0000 1.5
@@ -38,6 +38,7 @@
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.server.SimpleHttpServer;
+import org.apache.commons.httpclient.server.SimpleProxy;
/**
* Base class for test cases using
@@ -53,6 +54,9 @@
protected HttpClient client = null;
protected SimpleHttpServer server = null;
+ protected SimpleProxy proxy = null;
+ private boolean useProxy = true;
+
// ------------------------------------------------------------ Constructor
public HttpClientTestBase(final String testName) throws IOException {
super(testName);
@@ -70,6 +74,10 @@
return new TestSuite(HttpClientTestBase.class);
}
+ public void setUseProxy(boolean useProxy) {
+ this.useProxy = useProxy;
+ }
+
// ------------------------------------------------- TestCase setup/shutdown
public void setUp() throws IOException {
@@ -80,12 +88,21 @@
this.server.getLocalAddress(),
this.server.getLocalPort(),
Protocol.getProtocol("http"));
-
+ if (useProxy) {
+ this.proxy = new SimpleProxy();
+ client.getHostConfiguration().setProxy(
+ proxy.getLocalAddress(),
+ proxy.getLocalPort());
+ }
}
public void tearDown() throws IOException {
this.client = null;
this.server.destroy();
this.server = null;
- }
+ if (proxy != null) {
+ proxy.destroy();
+ proxy = null;
+ }
+ }
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ProxyTestDecorator.java
Index: ProxyTestDecorator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ProxyTestDecorator.java,v
1.1 2004/11/01 02:21:15 mbecke Exp $
* $Revision: 1.1 $
* $Date: 2004/11/01 02:21:15 $
*
* ====================================================================
*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient;
import java.util.Enumeration;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* A TestDecorator that configures instances of HttpClientTestBase to use
* a proxy server.
*/
public class ProxyTestDecorator extends TestSetup {
/**
* Iterates through all test cases included in the suite and adds
* copies of them modified to use a proxy server.
* @param suite
*/
public static void addTests(TestSuite suite) {
TestSuite ts2 = new TestSuite();
addTest(ts2, suite);
suite.addTest(ts2);
}
private static void addTest(TestSuite suite, Test t) {
if (t instanceof HttpClientTestBase) {
suite.addTest(new ProxyTestDecorator((HttpClientTestBase) t));
} else if (t instanceof TestSuite) {
Enumeration en = ((TestSuite) t).tests();
while (en.hasMoreElements()) {
addTest(suite, (Test) en.nextElement());
}
}
}
public ProxyTestDecorator(HttpClientTestBase test) {
super(test);
}
protected void setUp() throws Exception {
HttpClientTestBase base = (HttpClientTestBase) fTest;
base.setUseProxy(true);
}
}
1.6 +53 -35
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
Index: ProxyRequestHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ProxyRequestHandler.java 12 May 2004 20:43:54 -0000 1.5
+++ ProxyRequestHandler.java 1 Nov 2004 02:21:15 -0000 1.6
@@ -33,21 +33,22 @@
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.commons.httpclient.ChunkedOutputStream;
import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.URI;
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
/**
- * This request handler can handle GET and POST requests. It does
+ * This request handler can handle GET, POST, PUT, and HEAD requests. It does
* nothing for all other request
* @author Ortwin Glueck
*/
@@ -61,11 +62,22 @@
final SimpleRequest request) throws IOException
{
RequestLine line = request.getRequestLine();
- String method = line.getMethod();
- //TODO add POST method handling
- if (!"GET".equalsIgnoreCase(method)) return false;
- httpProxy(conn, request);
- return true;
+ URI url = new URI(request.getRequestLine().getUri(), false);
+ String methodName = line.getMethod();
+ HttpMethod method = null;
+ if ("GET".equalsIgnoreCase(methodName)) {
+ method = new GetMethod(url.getEscapedURI());
+ } else if ("POST".equalsIgnoreCase(methodName)) {
+ method = new PostMethod(url.getEscapedURI());
+ } else if ("PUT".equalsIgnoreCase(methodName)) {
+ method = new PutMethod(url.getEscapedURI());
+ } else if ("HEAD".equalsIgnoreCase(methodName)) {
+ method = new HeadMethod(url.getEscapedURI());
+ } else {
+ return false;
+ }
+ httpProxy(conn, request, method);
+ return true;
}
/**
@@ -73,30 +85,22 @@
*/
private void httpProxy(
final SimpleHttpServerConnection conn,
- final SimpleRequest request) throws IOException
+ final SimpleRequest request,
+ final HttpMethod method) throws IOException
{
- Log wireLog = LogFactory.getLog("httpclient.wire");
-
- URI url = new HttpURL(request.getRequestLine().getUri());
-
+ // TODO add support for Proxy-Connection headers
+
HttpClient client = new HttpClient();
- HostConfiguration hc = new HostConfiguration();
- hc.setHost(url);
- client.setHostConfiguration(hc);
-
- //TODO support other methods
- HttpMethod method = new GetMethod(url.getPathQuery());
+ method.setFollowRedirects(false);
Header[] headers = request.getHeaders();
for (int i=0; i<headers.length; i++) {
method.addRequestHeader(headers[i]);
}
if (method instanceof EntityEnclosingMethod) {
EntityEnclosingMethod emethod = (EntityEnclosingMethod) method;
- emethod.setRequestEntity(
- new InputStreamRequestEntity(conn.getInputStream()));
+ emethod.setRequestEntity(new
ByteArrayRequestEntity(request.getBody()));
}
client.executeMethod(method);
-
Header[] rheaders = method.getResponseHeaders();
InputStream targetIn = method.getResponseBodyAsStream();
@@ -108,13 +112,27 @@
if (rheaders.length > 0) out.println();
out.flush();
out = null;
- StreamProxy sp = new StreamProxy(targetIn, conn.getOutputStream());
- sp.start();
- try {
- sp.block();
- } catch (InterruptedException e) {
- throw new IOException(e.toString());
- }
+
+ // handle content, if present
+ if (targetIn != null) {
+ // if the server used chunking, so will we
+ Header teHeader = method.getResponseHeader("Transfer-Encoding");
+ ChunkedOutputStream cos = null;
+ OutputStream os = conn.getOutputStream();
+ if (teHeader != null &&
teHeader.getValue().toLowerCase().indexOf("chunked") >= 0) {
+ cos = new ChunkedOutputStream(os);
+ os = cos;
+ }
+ byte[] buffer = new byte[4096];
+ int length = 0;
+ while ((length = targetIn.read(buffer)) > 0) {
+ os.write(buffer, 0, length);
+ }
+ if (cos != null) {
+ cos.finish();
+ cos.flush();
+ }
+ }
}
}
1.6 +8 -5
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java
Index: TestBasicAuth.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestBasicAuth.java 31 Oct 2004 14:42:59 -0000 1.5
+++ TestBasicAuth.java 1 Nov 2004 02:21:15 -0000 1.6
@@ -41,6 +41,7 @@
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.ProxyTestDecorator;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.server.HttpService;
@@ -72,7 +73,9 @@
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
- return new TestSuite(TestBasicAuth.class);
+ TestSuite suite = new TestSuite(TestBasicAuth.class);
+ ProxyTestDecorator.addTests(suite);
+ return suite;
}
private class BasicAuthService implements HttpService {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]