1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.net.InetAddress ; 37 import java.net.Socket ; 38 import java.net.UnknownHostException ; 39 40 import junit.framework.Test; 41 import junit.framework.TestSuite; 42 43 import org.apache.commons.httpclient.protocol.Protocol; 44 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 45 46 55 public class TestHttpConnection extends TestLocalHostBase { 56 57 public TestHttpConnection(String testName) { 59 super(testName); 60 } 61 62 public static void main(String args[]) { 64 String [] testCaseName = { TestHttpConnection.class.getName() }; 65 junit.textui.TestRunner.main(testCaseName); 66 } 67 68 70 public static Test suite() { 71 return new TestSuite(TestHttpConnection.class); 72 } 73 74 75 77 public void testConstructThenClose() { 78 HttpConnection conn = new HttpConnection(getHost(), getPort()); 79 conn.close(); 80 assertTrue( ! conn.isOpen() ); 81 } 82 83 public void testConnTimeout() { 84 85 Protocol testProtocol = new Protocol( 87 "timeout", 88 new DelayedProtocolSocketFactory( 89 500, 90 Protocol.getProtocol("http").getSocketFactory() 91 ), 92 getPort() 93 ); 94 95 HttpConnection conn = new HttpConnection(getHost(), getPort(), testProtocol); 96 conn.setConnectionTimeout(1); 98 try { 99 conn.open(); 100 fail("Should have timed out"); 101 } catch(IOException e) { 102 assertTrue(e instanceof HttpConnection.ConnectionTimeoutException); 103 104 } 105 } 106 107 public void testForIllegalStateExceptions() { 108 HttpConnection conn = new HttpConnection(getHost(), getPort()); 109 110 try { 111 OutputStream out = conn.getRequestOutputStream(); 112 fail("getRequestOutputStream did not throw the expected exception"); 113 } 114 catch (IllegalStateException expected) { 115 } 117 catch (IOException ex) { 118 fail("getRequestOutputStream did not throw the expected exception"); 119 } 120 121 try { 122 OutputStream out = new ChunkedOutputStream(conn.getRequestOutputStream()); 123 fail("getRequestOutputStream(true) did not throw the expected exception"); 124 } 125 catch (IllegalStateException expected) { 126 } 128 catch (IOException ex) { 129 fail("getRequestOutputStream(true) did not throw the expected exception"); 130 } 131 132 try { 133 InputStream in = conn.getResponseInputStream(); 134 fail("getResponseInputStream() did not throw the expected exception"); 135 } 136 catch (IllegalStateException expected) { 137 } 139 catch (IOException ex) { 140 fail("getResponseInputStream() did not throw the expected exception"); 141 } 142 143 } 144 145 148 class DelayedProtocolSocketFactory implements ProtocolSocketFactory { 149 150 private int delay; 151 private ProtocolSocketFactory realFactory; 152 153 public DelayedProtocolSocketFactory(int delay, ProtocolSocketFactory realFactory) { 154 this.delay = delay; 155 this.realFactory = realFactory; 156 } 157 158 public Socket createSocket( 159 String host, 160 int port, 161 InetAddress clientHost, 162 int clientPort 163 ) throws IOException , UnknownHostException { 164 165 synchronized (this) { 166 try { 167 this.wait(delay); 168 } catch (InterruptedException e) {} 169 } 170 return realFactory.createSocket(host, port, clientHost, clientPort); 171 } 172 173 public Socket createSocket(String host, int port) 174 throws IOException , UnknownHostException { 175 synchronized (this) { 176 try { 177 this.wait(delay); 178 } catch (InterruptedException e) {} 179 } 180 return realFactory.createSocket(host, port); 181 } 182 183 } 184 185 } 186 187 | Popular Tags |