You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by og...@apache.org on 2003/12/05 22:23:15 UTC
cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server TransparentProxyRequestHandler.java ErrorResponse.java HttpRequestHandler.java SimpleHttpServerConnection.java HttpRequestHandlerChain.java ResponseWriter.java BidiStreamProxy.java GenericResponse.java StreamProxy.java SimpleProxy.java ProxyRequestHandler.java SimpleHttpServer.java ProxyAuthRequestHandler.java RequestLine.java
oglueck 2003/12/05 13:23:15
Modified: httpclient/src/test/org/apache/commons/httpclient
TestNoHost.java TestWebapp.java
Added: httpclient/src/test/org/apache/commons/httpclient
TestProxy.java TestBadContentLength.java
httpclient/src/test/org/apache/commons/httpclient/server
TransparentProxyRequestHandler.java
ErrorResponse.java HttpRequestHandler.java
SimpleHttpServerConnection.java
HttpRequestHandlerChain.java ResponseWriter.java
BidiStreamProxy.java GenericResponse.java
StreamProxy.java SimpleProxy.java
ProxyRequestHandler.java SimpleHttpServer.java
ProxyAuthRequestHandler.java RequestLine.java
Log:
Infrastructure for small local servers for testing.
Port to HEAD from 2.0 branch.
Submitted by: Christian Kohlsch�tter
Reviewed by: Ortwin Gl�ck
Revision Changes Path
1.27 +5 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
Index: TestNoHost.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- TestNoHost.java 19 Oct 2003 18:46:03 -0000 1.26
+++ TestNoHost.java 5 Dec 2003 21:23:14 -0000 1.27
@@ -108,6 +108,7 @@
suite.addTest(TestExceptions.suite());
suite.addTest(TestHttpVersion.suite());
suite.addTest(TestHttpParser.suite());
+ //TODO enable when ready: suite.addTest(TestBadContentLength.suite());
return suite;
}
1.8 +5 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java
Index: TestWebapp.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TestWebapp.java 12 Feb 2003 12:30:44 -0000 1.7
+++ TestWebapp.java 5 Dec 2003 21:23:14 -0000 1.8
@@ -102,6 +102,7 @@
suite.addTest(TestWebappPostMethod.suite());
suite.addTest(TestWebappMultiPostMethod.suite());
suite.addTest(TestWebappNoncompliant.suite());
+ suite.addTest(TestProxy.suite());
return suite;
}
1.2 +130 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java
Index: TestBadContentLength.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java,v 1.1 2003/12/05 21:23:14 oglueck Exp $
* $Revision: 1.1 $
* $Date: 2003/12/05 21:23:14 $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.server.HttpRequestHandler;
import org.apache.commons.httpclient.server.RequestLine;
import org.apache.commons.httpclient.server.ResponseWriter;
import org.apache.commons.httpclient.server.SimpleHttpServer;
import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
/**
* Tests HttpClient's behaviour when receiving more response data than expected.
* <p>
* A very simple HTTP Server will be setup on a free port during testing, which
* returns an incorrect response Content-Length, sending surplus response data,
* which may contain malicious/fake response headers.
* </p>
*
* @author Christian Kohlschuetter
* @version $Id: TestBadContentLength.java,v 1.1 2003/12/05 21:23:14 oglueck Exp $
*/
public class TestBadContentLength extends TestNoHost {
private HttpClient client = null;
private SimpleHttpServer server = null;
// ------------------------------------------------------------ Constructor
public TestBadContentLength(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestBadContentLength.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestBadContentLength.class);
}
// ----------------------------------------------------------- Test Methods
public void setUp() throws IOException {
/*
* uncomment to enable wire log
*/
/*
System.setProperty(
"org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty(
"org.apache.commons.logging.simplelog.showdatetime",
"true");
System.setProperty(
"org.apache.commons.logging.simplelog.log.httpclient.wire",
"debug");
System.setProperty(
"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
"debug");
*/
client = new HttpClient();
server = new SimpleHttpServer(); // use arbitrary port
server.setRequestHandler(new MyHttpRequestHandler());
}
public void tearDown() throws IOException {
client = null;
server.destroy();
}
/**
* HttpClient connects to the test server and performs two subsequent
* requests to the same URI in <u>lenient</u> mode.
*
* Expected behavior:
* For both requests, status code 200 and a response body of "12345"
* should be returned.
*
* @throws IOException
*/
public void test1Lenient() throws IOException {
client.getParams().makeLenient();
GetMethod m =
new GetMethod("http://localhost:" + server.getLocalPort() + "/");
client.executeMethod(m);
assertEquals(200, m.getStatusCode());
assertEquals("12345", m.getResponseBodyAsString());
m.recycle();
client.executeMethod(m);
assertEquals(200, m.getStatusCode());
assertEquals("12345", m.getResponseBodyAsString());
m.releaseConnection();
}
/**
* HttpClient connects to the test server and performs two subsequent
* requests to the same URI in <u>strict</u> mode.
* <p>
* The first response body will be read with getResponseBodyAsString(),
* which returns null if an error occured.
* </p>
* <p>
* The second response body will be read using an InputStream, which
* throws an IOException if something went wrong.
* </p>
* Expected behavior:
* For both requests, status code 200 should be returned.<br />
* For request 1, a <code>null</code> response body should be returned.<br />
* For request 2, a {@link ProtocolException} is expected.
*
* @throws IOException
*/
public void test1Strict() throws IOException {
client.getParams().makeStrict();
GetMethod m =
new GetMethod("http://localhost:" + server.getLocalPort() + "/");
client.executeMethod(m);
assertEquals(200, m.getStatusCode());
assertEquals(null, m.getResponseBodyAsString());
m.recycle();
client.executeMethod(m);
assertEquals(200, m.getStatusCode());
boolean gotException = false;
try {
InputStream in = m.getResponseBodyAsStream();
while (in.read() != -1) {
}
fail("Did not receive expected ProtocolException");
} catch (ProtocolException e) {
/* expected */
}
m.releaseConnection();
}
public void enableThisTestForDebuggingOnly()
throws InterruptedException {
while (server.isRunning()) {
Thread.sleep(100);
}
}
private class MyHttpRequestHandler implements HttpRequestHandler {
private int requestNo = 0;
public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
RequestLine requestLine = conn.getRequestLine();
ResponseWriter out = conn.getWriter();
if ("GET".equals(requestLine.getMethod())
&& "/".equals(requestLine.getUri())) {
requestNo++;
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("Content-Length: 5");
out.println("Connection: keep-alive");
out.println();
out.println("12345"); // send exactly 5 bytes
// and some more garbage!
out.println("AND SOME MORE\r\nGARBAGE!");
out.println("HTTP/1.0 404 Not Found");
out.println("Content-Type: text/plain");
out.println("");
out.println("THIS-IS-A-FAKE-RESPONSE!");
out.flush();
// process max. 2 subsequents requests per connection
if (requestNo < 2) {
conn.connectionKeepAlive();
}
}
return true;
}
}
}
1.2 +130 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java
1.2 +111 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java
1.2 +93 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java
1.2 +219 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
1.2 +108 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java
1.2 +155 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ResponseWriter.java
1.2 +124 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/BidiStreamProxy.java
1.2 +218 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/GenericResponse.java
1.2 +142 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/StreamProxy.java
1.2 +106 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java
1.2 +143 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
1.2 +249 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
1.2 +166 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java
1.2 +126 -0 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/RequestLine.java
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org