Index: TestAbortHandling.java =================================================================== --- TestAbortHandling.java (revision 1177034) +++ TestAbortHandling.java (working copy) @@ -27,6 +27,9 @@ package org.apache.http.impl.client; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; import java.util.concurrent.CountDownLatch; import org.apache.http.HttpException; @@ -103,6 +106,92 @@ Assert.assertNotNull("Request should exist",reqWrapper); Assert.assertEquals(1,((RequestWrapper) reqWrapper).getExecCount()); } + + @Test + public void testConnectionTimeoutAbort() throws IOException { + final ServerSocket server = new ServerSocket(0); + server.setSoTimeout(60000); // 1 minute timeout + Thread serverThread = new Thread() { + public void run() { + + Socket socket = null; + try { + socket = server.accept(); + } catch (java.io.InterruptedIOException e) { + Thread.currentThread().interrupt(); + } catch (Exception ignore) { + + } finally { + + if (socket != null) { + try { + socket.close(); + } catch (Exception ignore) { + + } + } + try { + server.close(); + } catch (Exception ignore) { + + } + } + } + }; + + serverThread.start(); + + int port = server.getLocalPort(); + + String s = "http://localhost:" + port + "/path"; + final HttpGet httpget = new HttpGet(s); + + Thread t = new Thread() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + httpget.abort(); + } + }; + + t.start(); + + HttpContext context = new BasicHttpContext(); + try { + this.httpclient.execute(getServerHttp((InetSocketAddress)server.getLocalSocketAddress()), httpget, context); + } catch (IOException e) { + e.printStackTrace(); + } + + HttpRequest reqWrapper = (HttpRequest) context + .getAttribute(ExecutionContext.HTTP_REQUEST); + Assert.assertNotNull("Request should exist", reqWrapper); + Assert.assertEquals(1, ((RequestWrapper) reqWrapper).getExecCount()); + } + + @Test + public void testRequestAlreadyAborted(){ + String s = "http://localhost/path"; + final HttpGet httpget = new HttpGet(s); + httpget.abort(); + + HttpContext context = new BasicHttpContext(); + try { + this.httpclient.execute(httpget, context); + } catch (IOException e) { + e.printStackTrace(); + } + + HttpRequest reqWrapper = (HttpRequest) context + .getAttribute(ExecutionContext.HTTP_REQUEST); + Assert.assertNotNull("Request should exist", reqWrapper); + Assert.assertEquals(1, ((RequestWrapper) reqWrapper).getExecCount()); + } + // TODO add similar test for connection abort }