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
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 /***
48 * Base class for test cases using
49 * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based
50 * testing framework.
51 *
52 * @author Oleg Kalnichevski
53 *
54 * @version $Id: HttpClientTestBase.java 608014 2008-01-02 05:48:53Z rolandw $
55 */
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
66 public HttpClientTestBase(final String testName) throws IOException {
67 super(testName);
68 }
69
70
71 public static void main(String args[]) {
72 String[] testCaseName = { HttpClientTestBase.class.getName() };
73 junit.textui.TestRunner.main(testCaseName);
74 }
75
76
77
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
95
96 public void setUp() throws IOException {
97
98 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);
110 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 }