1 31 32 package org.apache.commons.httpclient; 33 import java.io.BufferedReader ; 34 import java.io.IOException ; 35 import java.io.InputStreamReader ; 36 37 import junit.framework.Test; 38 import junit.framework.TestSuite; 39 40 import org.apache.commons.httpclient.methods.GetMethod; 41 import org.apache.commons.httpclient.server.HttpRequestHandler; 42 import org.apache.commons.httpclient.server.ResponseWriter; 43 import org.apache.commons.httpclient.server.SimpleHttpServerConnection; 44 import org.apache.commons.httpclient.server.SimpleRequest; 45 46 53 public class TestMethodAbort extends HttpClientTestBase { 54 55 public TestMethodAbort(final String testName) throws IOException { 57 super(testName); 58 } 59 60 public static void main(String args[]) { 62 String [] testCaseName = { TestMethodAbort.class.getName() }; 63 junit.textui.TestRunner.main(testCaseName); 64 } 65 66 68 public static Test suite() { 69 return new TestSuite(TestMethodAbort.class); 70 } 71 72 private class ProduceGarbageHandler implements HttpRequestHandler { 73 74 public ProduceGarbageHandler() { 75 super(); 76 } 77 78 public boolean processRequest( 79 final SimpleHttpServerConnection conn, 80 final SimpleRequest request) throws IOException 81 { 82 83 final String garbage = "garbage!\r\n"; 84 final long count = 1000000000; 85 86 HttpVersion httpversion = request.getRequestLine().getHttpVersion(); 87 ResponseWriter out = conn.getWriter(); 88 out.println(httpversion + " 200 OK"); 89 out.println("Content-Type: text/plain"); 90 out.println("Content-Length: " + count * garbage.length()) ; 91 out.println("Connection: close"); 92 out.println(); 93 for (int i = 0; i < count; i++) { 94 out.print(garbage); 95 } 96 return true; 97 } 98 } 99 100 public void testAbortMethod() throws IOException { 101 this.server.setRequestHandler(new ProduceGarbageHandler()); 102 final GetMethod httpget = new GetMethod("/test/"); 103 104 Thread thread = new Thread (new Runnable () { 105 public void run() { 106 try { 107 Thread.sleep(500); 108 } catch (InterruptedException e) { 109 } 110 httpget.abort(); 111 } 112 113 }); 114 thread.setDaemon(true); 115 thread.start(); 116 117 try { 118 this.client.executeMethod(httpget); 119 BufferedReader in = new BufferedReader (new InputStreamReader ( 120 httpget.getResponseBodyAsStream())); 121 String line = null; 122 while ((line = in.readLine()) != null) { 123 } 124 fail("IOException must have been thrown"); 125 } catch (IOException e) { 126 } finally { 128 httpget.releaseConnection(); 129 } 130 assertTrue(httpget.isAborted()); 131 } 132 133 public void testAbortedMethodExecute() throws IOException { 134 final GetMethod httpget = new GetMethod("/test/"); 135 136 try { 137 httpget.abort(); 138 try { 139 this.client.executeMethod(httpget); 140 fail("IllegalStateException must have been thrown"); 141 } catch (IllegalStateException e) { 142 } 143 } finally { 144 httpget.releaseConnection(); 145 } 146 assertTrue(httpget.isAborted()); 147 } 148 } 149 | Popular Tags |