Index: src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v
retrieving revision 1.22
diff -u -r1.22 SimpleHttpConnectionManager.java
--- src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java 6 Oct 2004 17:32:04 -0000 1.22
+++ src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java 13 Oct 2004 03:00:01 -0000
@@ -69,7 +69,7 @@
}
/** The http connection */
- private HttpConnection httpConnection;
+ protected HttpConnection httpConnection;
/**
* Collection of parameters associated with this connection manager.
Index: src/test/org/apache/commons/httpclient/TestResponseHeaders.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v
retrieving revision 1.14
diff -u -r1.14 TestResponseHeaders.java
--- src/test/org/apache/commons/httpclient/TestResponseHeaders.java 26 Feb 2004 20:25:55 -0000 1.14
+++ src/test/org/apache/commons/httpclient/TestResponseHeaders.java 13 Oct 2004 03:00:03 -0000
@@ -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 Adrian Sutton
* @version $Id: TestResponseHeaders.java,v 1.14 2004/02/26 20:25:55 olegk Exp $
*/
-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());
}
}
Index: src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
retrieving revision 1.7
diff -u -r1.7 SimpleHttpServer.java
--- src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java 7 Oct 2004 16:14:16 -0000 1.7
+++ src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java 13 Oct 2004 03:00:04 -0000
@@ -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));
}
Index: src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java
===================================================================
RCS file: src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java
diff -N src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/test/org/apache/commons/httpclient/AccessibleHttpConnectionManager.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,45 @@
+/*
+ * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.22 2004/10/06 17:32:04 olegk Exp $
+ * $Revision: 1.22 $
+ * $Date: 2004/10/06 17:32:04 $
+ *
+ * ====================================================================
+ *
+ * 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
+ * .
+ *
+ */
+
+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;
+ }
+
+}