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 junit.framework.TestCase;
33
34 /***
35 * The base class for all tests that need to connection to the localhost
36 * web server.
37 *
38 * @author Michael Becke
39 *
40 * @deprecated
41 */
42 public abstract class TestLocalHostBase extends TestCase {
43
44 private final String protocol = System.getProperty(
45 "httpclient.test.localHost.protocol",
46 "http"
47 );
48 private final String host = System.getProperty("httpclient.test.localHost","localhost");
49 private final int port;
50 private final String proxyHost = System.getProperty("httpclient.test.proxy.host");
51 private final int proxyPort;
52
53 /***
54 * Constructor for TestLocalHostBase.
55 * @param testName
56 */
57 public TestLocalHostBase(String testName) {
58 super(testName);
59 String portString = System.getProperty("httpclient.test.localPort","8080");
60 int tempPort = 8080;
61 try {
62 tempPort = Integer.parseInt(portString);
63 } catch(Exception e) {
64 tempPort = 8080;
65 }
66 port = tempPort;
67 String proxyPortString = System.getProperty("httpclient.test.proxy.port","3128");
68 int tempProxyPort = 3128;
69 try {
70 tempProxyPort = Integer.parseInt(proxyPortString);
71 } catch(Exception e) {
72 tempProxyPort = 3128;
73 }
74 proxyPort = tempProxyPort;
75 }
76
77 /***
78 * Gets a new HttpClient instance. This instance has been configured
79 * with all appropriate host/proxy values.
80 *
81 * @return a new HttpClient instance
82 */
83 public HttpClient createHttpClient() {
84 return createHttpClient(null);
85 }
86
87 /***
88 * Gets a new HttpClient instance that uses the given connection manager.
89 * This instance has been configured with all appropriate host/proxy values.
90 *
91 * @param connectionManager the connection manager to use or <code>null</code>
92 *
93 * @return a new HttpClient instance
94 */
95 public HttpClient createHttpClient(HttpConnectionManager connectionManager) {
96
97 HttpClient client = null;
98
99 if (connectionManager == null) {
100 client = new HttpClient();
101 } else {
102 client = new HttpClient(connectionManager);
103 }
104
105 configureHostConfiguration(client.getHostConfiguration());
106
107 return client;
108 }
109
110 /***
111 * Configures the host config with the correct host and proxy settings.
112 *
113 * @param hostConfiguration
114 */
115 public void configureHostConfiguration(HostConfiguration hostConfiguration) {
116 hostConfiguration.setHost(host, port, protocol);
117 if (proxyHost != null) {
118 hostConfiguration.setProxy(proxyHost, proxyPort);
119 }
120 }
121
122 /***
123 * @return String
124 */
125 public String getHost() {
126 return host;
127 }
128
129 /***
130 * @return int
131 */
132 public int getPort() {
133 return port;
134 }
135
136 /***
137 * @return String
138 */
139 public String getProtocol() {
140 return protocol;
141 }
142
143 }