1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.apache.commons.httpclient;
30
31 import java.io.IOException;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.protocol.Protocol;
38 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
39 import org.apache.commons.httpclient.server.SimpleHttpServer;
40 import org.apache.commons.httpclient.server.SimplePlainSocketFactory;
41 import org.apache.commons.httpclient.server.SimpleProxy;
42 import org.apache.commons.httpclient.server.SimpleSocketFactory;
43 import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory;
44 import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48 /***
49 * Base class for test cases using
50 * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based
51 * testing framework.
52 *
53 * @author Oleg Kalnichevski
54 *
55 * @version $Id: HttpClientTestBase.java 155418 2005-02-26 13:01:52Z dirkv $
56 */
57 public class HttpClientTestBase extends TestCase {
58
59 private static final Log LOG = LogFactory.getLog(HttpClientTestBase.class);
60
61 protected HttpClient client = null;
62 protected SimpleHttpServer server = null;
63
64 protected SimpleProxy proxy = null;
65 private boolean useProxy = false;
66 private boolean useSSL = false;
67
68
69 public HttpClientTestBase(final String testName) throws IOException {
70 super(testName);
71 }
72
73
74 public static void main(String args[]) {
75 String[] testCaseName = { HttpClientTestBase.class.getName() };
76 junit.textui.TestRunner.main(testCaseName);
77 }
78
79
80
81 public static Test suite() {
82 return new TestSuite(HttpClientTestBase.class);
83 }
84
85 public void setUseProxy(boolean useProxy) {
86 this.useProxy = useProxy;
87 }
88
89 public void setUseSSL(boolean b) {
90 this.useSSL = b;
91 }
92
93 public boolean isUseSSL() {
94 return this.useSSL;
95 }
96
97
98
99 public void setUp() throws IOException {
100
101 SimpleSocketFactory serversocketfactory = null;
102 Protocol testhttp = null;
103 if (this.useSSL) {
104 serversocketfactory = new SimpleSSLSocketFactory();
105 testhttp = new Protocol("https",
106 (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
107 } else {
108 serversocketfactory = new SimplePlainSocketFactory();
109 testhttp = Protocol.getProtocol("http");
110 }
111
112 this.server = new SimpleHttpServer(serversocketfactory, 0);
113 this.server.setTestname(getName());
114
115 this.client = new HttpClient();
116
117 this.client.getHostConfiguration().setHost(
118 this.server.getLocalAddress(),
119 this.server.getLocalPort(),
120 testhttp);
121
122 if (this.useProxy) {
123 this.proxy = new SimpleProxy();
124 client.getHostConfiguration().setProxy(
125 proxy.getLocalAddress(),
126 proxy.getLocalPort());
127 }
128 }
129
130 public void tearDown() throws IOException {
131 this.client = null;
132 this.server.destroy();
133 this.server = null;
134 if (proxy != null) {
135 proxy.destroy();
136 proxy = null;
137 }
138 }
139 }