1 29 30 package org.apache.commons.httpclient; 31 32 import java.io.IOException ; 33 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.protocol.Protocol; 39 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 40 import org.apache.commons.httpclient.server.SimpleHttpServer; 41 import org.apache.commons.httpclient.server.SimplePlainSocketFactory; 42 import org.apache.commons.httpclient.server.SimpleProxy; 43 import org.apache.commons.httpclient.server.SimpleSocketFactory; 44 import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory; 45 import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory; 46 47 56 public class HttpClientTestBase extends TestCase { 57 58 protected HttpClient client = null; 59 protected SimpleHttpServer server = null; 60 61 protected SimpleProxy proxy = null; 62 private boolean useProxy = false; 63 private boolean useSSL = false; 64 65 public HttpClientTestBase(final String testName) throws IOException { 67 super(testName); 68 } 69 70 public static void main(String args[]) { 72 String [] testCaseName = { HttpClientTestBase.class.getName() }; 73 junit.textui.TestRunner.main(testCaseName); 74 } 75 76 78 public static Test suite() { 79 return new TestSuite(HttpClientTestBase.class); 80 } 81 82 public void setUseProxy(boolean useProxy) { 83 this.useProxy = useProxy; 84 } 85 86 public void setUseSSL(boolean b) { 87 this.useSSL = b; 88 } 89 90 public boolean isUseSSL() { 91 return this.useSSL; 92 } 93 94 96 public void setUp() throws IOException { 97 SimpleSocketFactory serversocketfactory = null; 99 Protocol testhttp = null; 100 if (this.useSSL) { 101 serversocketfactory = new SimpleSSLSocketFactory(); 102 testhttp = new Protocol("https", 103 (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443); 104 } else { 105 serversocketfactory = new SimplePlainSocketFactory(); 106 testhttp = Protocol.getProtocol("http"); 107 } 108 109 this.server = new SimpleHttpServer(serversocketfactory, 0); this.server.setTestname(getName()); 111 112 this.client = new HttpClient(); 113 114 this.client.getHostConfiguration().setHost( 115 this.server.getLocalAddress(), 116 this.server.getLocalPort(), 117 testhttp); 118 119 if (this.useProxy) { 120 this.proxy = new SimpleProxy(); 121 client.getHostConfiguration().setProxy( 122 proxy.getLocalAddress(), 123 proxy.getLocalPort()); 124 } 125 } 126 127 public void tearDown() throws IOException { 128 this.client = null; 129 this.server.destroy(); 130 this.server = null; 131 if (proxy != null) { 132 proxy.destroy(); 133 proxy = null; 134 } 135 } 136 } 137 | Popular Tags |