mbecke 2004/10/16 15:40:08
Modified: httpclient/src/test/org/apache/commons/httpclient
TestResponseHeaders.java
httpclient/src/test/org/apache/commons/httpclient/server
SimpleHttpServer.java
httpclient/src/java/org/apache/commons/httpclient
SimpleHttpConnectionManager.java
Added: httpclient/src/test/org/apache/commons/httpclient
AccessibleHttpConnectionManager.java
Log:
More test refactorings.
PR: 27298
Submitted by: Michael Becke
Revision Changes Path
1.15 +222 -129
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java
Index: TestResponseHeaders.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- TestResponseHeaders.java 26 Feb 2004 20:25:55 -0000 1.14
+++ TestResponseHeaders.java 16 Oct 2004 22:40:08 -0000 1.15
@@ -30,10 +30,18 @@
package org.apache.commons.httpclient;
+import java.io.IOException;
+
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.HttpService;
+import org.apache.commons.httpclient.server.ResponseWriter;
+import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
+import org.apache.commons.httpclient.server.SimpleRequest;
+import org.apache.commons.httpclient.server.SimpleResponse;
/**
* Tests for reading response headers.
@@ -43,12 +51,20 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Sutton</a>
* @version $Id$
*/
-public class TestResponseHeaders extends TestNoHostBase {
+public class TestResponseHeaders extends HttpClientTestBase {
+ private AccessibleHttpConnectionManager connectionManager;
+
// ------------------------------------------------------------ Constructor
public TestResponseHeaders(String testName) {
super(testName);
}
+
+ public void setUp() throws IOException {
+ super.setUp();
+ this.connectionManager = new AccessibleHttpConnectionManager();
+ this.client.setHttpConnectionManager(connectionManager);
+ }
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
@@ -63,16 +79,22 @@
// ----------------------------------------------------------- Test Methods
public void testHeaders() throws Exception {
- String body = "XXX\r\nYYY\r\nZZZ";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Connection: close\r\n" +
- "Content-Length: " + body.length() + "\r\n" +
- "Content-Type: text/xml; charset=utf-8\r\n" +
- "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" +
- "Server: UserLand Frontier/7.0-WinNT\r\n";
- HttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
+ final String body = "XXX\r\nYYY\r\nZZZ";
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Connection", "close"));
+ response.addHeader(new Header("Content-Length",
Integer.toString(body.length())));
+ response.addHeader(new Header("Content-Type", "text/xml;
charset=utf-8"));
+ response.addHeader(new Header("Date", "Wed, 28 Mar 2001 05:05:04
GMT"));
+ response.addHeader(new Header("Server", "UserLand
Frontier/7.0-WinNT"));
+ response.setBodyString(body);
+ return true;
+ }
+ });
+
+ HttpMethod method = new GetMethod();
client.executeMethod(method);
assertEquals("close", method.getResponseHeader("Connection").getValue());
assertEquals(body.length(),
Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
@@ -86,132 +108,164 @@
*/
public void testDuplicateContentLength() throws Exception {
- String body = "XXX\r\nYYY\r\nZZZ";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Content-Length: " + body.length() + "\r\n" +
- "Content-Length: " + body.length() + "\r\n";
- HttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
+ final String body = "XXX\r\nYYY\r\nZZZ";
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Content-Length",
Integer.toString(body.length())));
+ response.addHeader(new Header("Content-Length",
Integer.toString(body.length())));
+ response.setBodyString(body);
+ return true;
+ }
+ });
+ HttpMethod method = new GetMethod();
client.executeMethod(method);
assertNotNull( "Response body is null.", method.getResponseBodyAsStream() );
-
}
public void testDuplicateProxyConnection() throws Exception {
- client.getHostConfiguration().setProxy("proxy", 1);
-
- String headers =
- "HTTP/1.1 200 OK\r\n"
- + "proxy-connection: close\r\n"
- + "proxy-connection: close\r\n"
- + "Content-Length: 0\r\n"
- + "\r\n";
-
- conn.addResponse(headers, "");
+ client.getHostConfiguration().setProxy(server.getLocalAddress(),
server.getLocalPort());
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("proxy-connection", "close"));
+ response.addHeader(new Header("proxy-connection", "close"));
+ return true;
+ }
+ });
GetMethod method = new GetMethod("/");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
- headers =
- "HTTP/1.0 200 OK\r\n"
- + "proxy-connection: keep-alive\r\n"
- + "proxy-connection: keep-alive\r\n"
- + "Content-Length: 2\r\n"
- + "\r\n";
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(HttpVersion.HTTP_1_0, 200);
+ response.addHeader(new Header("proxy-connection", "keep-alive"));
+ response.addHeader(new Header("proxy-connection", "keep-alive"));
+ response.setBodyString("aa");
+ return true;
+ }
+ });
- conn.addResponse(headers, "");
method = new GetMethod("/");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertTrue(conn.isOpen());
+ assertTrue(connectionManager.getConection().isOpen());
}
public void testDuplicateConnection() throws Exception {
- String headers =
- "HTTP/1.1 200 OK\r\n"
- + "Connection: close\r\n"
- + "Connection: close\r\n"
- + "Content-Length: 0\r\n"
- + "\r\n";
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Connection", "close"));
+ response.addHeader(new Header("Connection", "close"));
+ return true;
+ }
+ });
GetMethod method = new GetMethod("/");
- conn.addResponse(headers, "");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
- headers =
- "HTTP/1.0 200 OK\r\n"
- +"Connection: keep-alive\r\n"
- +"Connection: keep-alive\r\n"
- + "Content-Length: 2\r\n"
- +"\r\n";
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(HttpVersion.HTTP_1_0, 200);
+ response.addHeader(new Header("Connection", "keep-alive"));
+ response.addHeader(new Header("Connection", "keep-alive"));
+ response.setBodyString("aa");
+ return true;
+ }
+ });
method = new GetMethod("/");
- conn.addResponse(headers, "");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertTrue(conn.isOpen());
+ assertTrue(connectionManager.getConection().isOpen());
}
public void testNoContentLength() throws Exception {
// test with connection header
- String headers =
- "HTTP/1.1 200 OK\r\n"
- + "Connection: keep-alive\r\n"
- + "\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println("Connection: keep-alive");
+ out.println();
+ out.println("12345");
+ out.flush();
+ return true;
+ }
+ });
GetMethod method = new GetMethod("/");
- conn.addResponse(headers, "12345");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
// test without connection header
- headers = "HTTP/1.1 200 OK\r\n\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println();
+ out.println("12345");
+ out.flush();
+ return true;
+ }
+ });
// test with connection header
method = new GetMethod("/");
- conn.addResponse(headers, "12345");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
}
public void testInvalidContentLength1() throws Exception {
- // test with connection header
- String headers = "HTTP/1.1 200 OK\r\n"
- + "Content-Length: 5\r\n"
- + "Content-Length: stuff\r\n"
- + "\r\n";
-
- // test with connection header
- conn.addResponse(headers, "12345");
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Content-Length", "5"));
+ response.addHeader(new Header("Content-Length", "stuff"));
+ response.setBodyString("12345");
+ return true;
+ }
+ });
GetMethod method = new GetMethod("/");
client.executeMethod(method);
assertEquals(5, method.getResponseContentLength());
}
public void testInvalidContentLength2() throws Exception {
- // test with connection header
- String headers = "HTTP/1.1 200 OK\r\n"
- + "Content-Length: stuff\r\n"
- + "Content-Length: 5\r\n"
- + "\r\n";
-
- // test with connection header
- conn.addResponse(headers, "12345");
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Content-Length", "stuff"));
+ response.addHeader(new Header("Content-Length", "5"));
+ response.setBodyString("12345");
+ return true;
+ }
+ });
GetMethod method = new GetMethod("/");
client.executeMethod(method);
assertEquals(5, method.getResponseContentLength());
@@ -219,58 +273,83 @@
public void testProxyNoContentLength() throws Exception {
// test with proxy-connection header
- String headers =
- "HTTP/1.1 200 OK\r\n"
- + "proxy-connection: keep-alive\r\n"
- + "\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println("proxy-connection: keep-alive");
+ out.println();
+ out.println("12345");
+ out.flush();
+ return true;
+ }
+ });
- conn.setProxyHost("proxy");
- conn.setProxyPort(1);
+ client.getHostConfiguration().setProxy(server.getLocalAddress(),
server.getLocalPort());
GetMethod method = new GetMethod("/");
- conn.addResponse(headers, "12345");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
// test without proxy-connection header
- headers = "HTTP/1.1 200 OK\r\n\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println();
+ out.println("12345");
+ out.flush();
+ return true;
+ }
+ });
- conn.setProxyHost("proxy");
- conn.setProxyPort(1);
method = new GetMethod("/");
- conn.addResponse(headers, "12345");
client.executeMethod(method);
method.getResponseBodyAsString();
- assertFalse(conn.isOpen());
+ assertFalse(connectionManager.getConection().isOpen());
}
public void testNullHeaders() throws Exception {
- String body = "XXX\r\nYYY\r\nZZZ";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Content-Length: " + body.length() + "\r\n";
- HttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
+ this.server.setHttpService(new HttpService() {
+ public boolean process(SimpleRequest request,
+ SimpleResponse response) throws IOException {
+ response.setStatusLine(request.getRequestLine().getHttpVersion(),
200);
+ response.addHeader(new Header("Connection", "close"));
+ response.setBodyString("XXX\r\nYYY\r\nZZZ");
+ return true;
+ }
+ });
+ HttpMethod method = new GetMethod("/");
client.executeMethod(method);
assertEquals(null, method.getResponseHeader(null));
assertEquals(null, method.getResponseHeader("bogus"));
}
public void testFoldedHeaders() throws Exception {
- String body = "XXX\r\nYYY\r\nZZZ";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Connection: close\r\n" +
- "Content-Length: " + body.length() + "\r\n" +
- "Content-Type: text/xml; charset=utf-8\r\n" +
- "\tboundary=XXXX\r\n" +
- "Date: Wed, 28 Mar 2001\r\n" +
- " 05:05:04 GMT\r\n" +
- "Server: UserLand Frontier/7.0-WinNT\r\n";
- HttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
+ final String body = "XXX\r\nYYY\r\nZZZ";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println("Connection: close");
+ out.println("Content-Length: " + body.length());
+ out.println("Content-Type: text/xml; charset=utf-8");
+ out.println("\tboundary=XXXX");
+ out.println("Date: Wed, 28 Mar 2001");
+ out.println(" 05:05:04 GMT");
+ out.println("Server: UserLand Frontier/7.0-WinNT");
+ out.println();
+ out.println(body);
+ out.flush();
+ return true;
+ }
+ });
+ HttpMethod method = new GetMethod("/");
client.executeMethod(method);
assertEquals("close", method.getResponseHeader("Connection").getValue());
assertEquals(body.length(),
Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
@@ -282,29 +361,43 @@
public void testForceCloseConnection() throws Exception {
- String body = "stuff";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Content-Type: garbage\r\n" +
- "\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: garbage");
+ out.println();
+ out.println("stuff");
+ out.flush();
+ return true;
+ }
+ });
SimpleHttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
client.executeMethod(method);
- assertTrue("Connection should be closed",
method.shouldCloseConnection(conn));
+ assertTrue("Connection should be closed",
+ method.shouldCloseConnection(connectionManager.getConection()));
assertTrue("Connection should be force-closed",
method.isConnectionCloseForced());
}
public void testForceCloseConnection2() throws Exception {
- String body = "stuff";
- String headers =
- "HTTP/1.1 200 OK\r\n" +
- "Content-Type: garbage\r\n" +
- "Connection: close\r\n" +
- "\r\n";
+ this.server.setRequestHandler(new HttpRequestHandler() {
+ public boolean processRequest(SimpleHttpServerConnection conn,
+ SimpleRequest request) throws IOException {
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: garbage");
+ out.println("Connection: close");
+ out.println();
+ out.println("stuff");
+ out.flush();
+ return true;
+ }
+ });
SimpleHttpMethod method = new SimpleHttpMethod();
- conn.addResponse(headers, body);
client.executeMethod(method);
- assertTrue("Connection should be closed",
method.shouldCloseConnection(conn));
+ assertTrue("Connection should be closed",
+ method.shouldCloseConnection(connectionManager.getConection()));
assertFalse("Connection should NOT be closed",
method.isConnectionCloseForced());
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java
Index: AccessibleHttpConnectionManager.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java,v
1.1 2004/10/16 22:40:08 mbecke Exp $
* $Revision: 1.1 $
* $Date: 2004/10/16 22:40:08 $
*
* ====================================================================
*
* 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;
/**
* A simple connection manager that provides access to the connection used.
*/
public class AccessibleHttpConnectionManager extends SimpleHttpConnectionManager {
public AccessibleHttpConnectionManager() {
}
public HttpConnection getConection() {
return httpConnection;
}
}
1.8 +4 -4
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
Index: SimpleHttpServer.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SimpleHttpServer.java 7 Oct 2004 16:14:16 -0000 1.7
+++ SimpleHttpServer.java 16 Oct 2004 22:40:08 -0000 1.8
@@ -243,7 +243,7 @@
new Header("Content-Type", buffer.toString(), true));
}
// @TODO implement HTTP/1.1 persistent connections
- if (!conn.isKeepAlive()) {
+ if (!conn.isKeepAlive() && !response.containsHeader("Connection")) {
response.setHeader(
new Header("Connection", "close", true));
}
1.23 +4 -4
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java
Index: SimpleHttpConnectionManager.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- SimpleHttpConnectionManager.java 6 Oct 2004 17:32:04 -0000 1.22
+++ SimpleHttpConnectionManager.java 16 Oct 2004 22:40:08 -0000 1.23
@@ -69,7 +69,7 @@
}
/** The http connection */
- private HttpConnection httpConnection;
+ protected HttpConnection httpConnection;
/**
* Collection of parameters associated with this connection manager.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]