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
46 /***
47 * Base class for test cases using
48 * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based
49 * testing framework.
50 *
51 * @author Oleg Kalnichevski
52 *
53 * @version $Id: HttpClientTestBase.java 224451 2005-07-23 10:23:59Z olegk $
54 */
55 public class HttpClientTestBase extends TestCase {
56
57 protected HttpClient client = null;
58 protected SimpleHttpServer server = null;
59
60 protected SimpleProxy proxy = null;
61 private boolean useProxy = false;
62 private boolean useSSL = false;
63
64
65 public HttpClientTestBase(final String testName) throws IOException {
66 super(testName);
67 }
68
69
70 public static void main(String args[]) {
71 String[] testCaseName = { HttpClientTestBase.class.getName() };
72 junit.textui.TestRunner.main(testCaseName);
73 }
74
75
76
77 public static Test suite() {
78 return new TestSuite(HttpClientTestBase.class);
79 }
80
81 public void setUseProxy(boolean useProxy) {
82 this.useProxy = useProxy;
83 }
84
85 public void setUseSSL(boolean b) {
86 this.useSSL = b;
87 }
88
89 public boolean isUseSSL() {
90 return this.useSSL;
91 }
92
93
94
95 public void setUp() throws IOException {
96
97 SimpleSocketFactory serversocketfactory = null;
98 Protocol testhttp = null;
99 if (this.useSSL) {
100 serversocketfactory = new SimpleSSLSocketFactory();
101 testhttp = new Protocol("https",
102 (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
103 } else {
104 serversocketfactory = new SimplePlainSocketFactory();
105 testhttp = Protocol.getProtocol("http");
106 }
107
108 this.server = new SimpleHttpServer(serversocketfactory, 0);
109 this.server.setTestname(getName());
110
111 this.client = new HttpClient();
112
113 this.client.getHostConfiguration().setHost(
114 this.server.getLocalAddress(),
115 this.server.getLocalPort(),
116 testhttp);
117
118 if (this.useProxy) {
119 this.proxy = new SimpleProxy();
120 client.getHostConfiguration().setProxy(
121 proxy.getLocalAddress(),
122 proxy.getLocalPort());
123 }
124 }
125
126 public void tearDown() throws IOException {
127 this.client = null;
128 this.server.destroy();
129 this.server = null;
130 if (proxy != null) {
131 proxy.destroy();
132 proxy = null;
133 }
134 }
135 }