Index: java/org/apache/commons/httpclient/HttpMethodDirector.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v retrieving revision 1.3 diff -u -r1.3 HttpMethodDirector.java --- java/org/apache/commons/httpclient/HttpMethodDirector.java 11 Sep 2003 20:08:33 -0000 1.3 +++ java/org/apache/commons/httpclient/HttpMethodDirector.java 13 Sep 2003 17:52:49 -0000 @@ -166,10 +166,10 @@ // responseStream == null), as subclasses, might reset the stream, // for example, reading the entire response into a file and then // setting the file as the stream. - if (method.getResponseBodyAsStream() == null) { - method.releaseConnection(); - } else if (releaseConnection && connection != null) { + if (releaseConnection && connection != null) { connection.releaseConnection(); + } else if (method.getResponseBodyAsStream() == null) { + method.releaseConnection(); } } Index: test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java,v retrieving revision 1.2 diff -u -r1.2 NoHostHttpConnectionManager.java --- test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java 4 Sep 2003 02:12:13 -0000 1.2 +++ test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java 13 Sep 2003 17:52:51 -0000 @@ -10,20 +10,25 @@ import java.io.InputStream; /** - * @author mbecke - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ public class NoHostHttpConnectionManager implements HttpConnectionManager { private HttpConnection connection; + + private boolean connectionReleased = false; public NoHostHttpConnectionManager() { setConnection(new SimpleHttpConnection()); } /** + * @return + */ + public boolean isConnectionReleased() { + return connectionReleased; + } + + /** * @param connection */ public void setConnection(HttpConnection connection) { @@ -54,6 +59,7 @@ finishLastResponse(connection); } + connectionReleased = false; return connection; } @@ -73,7 +79,8 @@ if (conn != connection) { throw new IllegalStateException("Unexpected close on a different connection."); } - + + connectionReleased = true; finishLastResponse(connection); } Index: test/org/apache/commons/httpclient/TestHttpConnection.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java,v retrieving revision 1.9 diff -u -r1.9 TestHttpConnection.java --- test/org/apache/commons/httpclient/TestHttpConnection.java 11 Sep 2003 20:08:33 -0000 1.9 +++ test/org/apache/commons/httpclient/TestHttpConnection.java 13 Sep 2003 17:52:51 -0000 @@ -72,6 +72,7 @@ import junit.framework.Test; import junit.framework.TestSuite; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; @@ -111,6 +112,36 @@ conn.close(); assertTrue( ! conn.isOpen() ); } + + public void testConnTimeoutRelease() { + + // create a custom protocol that will delay for 500 milliseconds + Protocol testProtocol = new Protocol( + "timeout", + new DelayedProtocolSocketFactory( + 500, + Protocol.getProtocol("http").getSocketFactory() + ), + getPort() + ); + + NoHostHttpConnectionManager connectionManager = new NoHostHttpConnectionManager(); + connectionManager.setConnection(new HttpConnection(getHost(), getPort(), testProtocol)); + HttpClient client = createHttpClient(connectionManager); + client.getHostConfiguration().setHost(getHost(), getPort(), testProtocol); + client.setConnectionTimeout(1); + + try { + GetMethod get = new GetMethod(); + client.executeMethod(get); + fail("Should have timed out"); + } catch(IOException e) { + /* should fail */ + assertTrue(e instanceof HttpConnection.ConnectionTimeoutException); + assertTrue(connectionManager.isConnectionReleased()); + } + } + public void testConnTimeout() {