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
31
32 package org.apache.commons.httpclient;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.net.InetAddress;
38 import java.net.Socket;
39 import java.net.UnknownHostException;
40
41 import junit.framework.Test;
42 import junit.framework.TestSuite;
43
44 import org.apache.commons.httpclient.methods.GetMethod;
45 import org.apache.commons.httpclient.params.HttpConnectionParams;
46 import org.apache.commons.httpclient.protocol.Protocol;
47 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
48 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
49
50 /***
51 *
52 * Unit tests for {@link HttpConnection}.
53 *
54 * @author Sean C. Sullivan
55 *
56 * @version $Id: TestHttpConnection.java 608018 2008-01-02 05:57:23Z rolandw $
57 *
58 */
59 public class TestHttpConnection extends HttpClientTestBase {
60
61
62 public TestHttpConnection(String testName) throws Exception {
63 super(testName);
64 }
65
66
67 public static void main(String args[]) {
68 String[] testCaseName = { TestHttpConnection.class.getName() };
69 junit.textui.TestRunner.main(testCaseName);
70 }
71
72
73
74 public static Test suite() {
75 return new TestSuite(TestHttpConnection.class);
76 }
77
78
79
80
81 public void testConstructThenClose() {
82 this.server.setHttpService(new EchoService());
83 HttpConnection conn = new HttpConnection(
84 this.server.getLocalAddress(), this.server.getLocalPort());
85 conn.close();
86 assertTrue(!conn.isOpen());
87 }
88
89 public void testConnTimeoutRelease() {
90 this.server.setHttpService(new EchoService());
91
92 Protocol testProtocol = new Protocol(
93 "timeout",
94 new DelayedProtocolSocketFactory(
95 500,
96 Protocol.getProtocol("http").getSocketFactory()
97 ),
98 this.server.getLocalPort()
99 );
100
101 NoHostHttpConnectionManager connectionManager = new NoHostHttpConnectionManager();
102 connectionManager.setConnection(
103 new HttpConnection(
104 this.server.getLocalAddress(), this.server.getLocalPort(), testProtocol));
105 this.client.setHttpConnectionManager(connectionManager);
106 client.getHostConfiguration().setHost(
107 this.server.getLocalAddress(), this.server.getLocalPort(), testProtocol);
108 client.getHttpConnectionManager().getParams().setConnectionTimeout(1);
109
110 try {
111 GetMethod get = new GetMethod();
112 client.executeMethod(get);
113 fail("Should have timed out");
114 } catch(IOException e) {
115
116 assertTrue(e instanceof ConnectTimeoutException);
117 assertTrue(connectionManager.isConnectionReleased());
118 }
119 }
120
121
122 public void testConnTimeout() {
123
124
125 Protocol testProtocol = new Protocol(
126 "timeout",
127 new DelayedProtocolSocketFactory(
128 500,
129 Protocol.getProtocol("http").getSocketFactory()
130 ),
131 this.server.getLocalPort()
132 );
133
134 HttpConnection conn = new HttpConnection(
135 this.server.getLocalAddress(), this.server.getLocalPort(), testProtocol);
136
137 conn.getParams().setConnectionTimeout(1);
138 try {
139 conn.open();
140 fail("Should have timed out");
141 } catch(IOException e) {
142 assertTrue(e instanceof ConnectTimeoutException);
143
144 }
145 }
146
147 public void testForIllegalStateExceptions() {
148 HttpConnection conn = new HttpConnection(
149 this.server.getLocalAddress(), this.server.getLocalPort());
150 try {
151 OutputStream out = conn.getRequestOutputStream();
152 fail("getRequestOutputStream did not throw the expected exception");
153 }
154 catch (IllegalStateException expected) {
155
156 }
157 catch (IOException ex) {
158 fail("getRequestOutputStream did not throw the expected exception");
159 }
160
161 try {
162 OutputStream out = new ChunkedOutputStream(conn.getRequestOutputStream());
163 fail("getRequestOutputStream(true) did not throw the expected exception");
164 }
165 catch (IllegalStateException expected) {
166
167 }
168 catch (IOException ex) {
169 fail("getRequestOutputStream(true) did not throw the expected exception");
170 }
171
172 try {
173 InputStream in = conn.getResponseInputStream();
174 fail("getResponseInputStream() did not throw the expected exception");
175 }
176 catch (IllegalStateException expected) {
177
178 }
179 catch (IOException ex) {
180 fail("getResponseInputStream() did not throw the expected exception");
181 }
182
183 }
184
185 /***
186 * A ProtocolSocketFactory that delays before creating a socket.
187 */
188 class DelayedProtocolSocketFactory implements ProtocolSocketFactory {
189
190 private int delay;
191 private ProtocolSocketFactory realFactory;
192
193 public DelayedProtocolSocketFactory(int delay, ProtocolSocketFactory realFactory) {
194 this.delay = delay;
195 this.realFactory = realFactory;
196 }
197
198 public Socket createSocket(
199 String host,
200 int port,
201 InetAddress localAddress,
202 int localPort
203 ) throws IOException, UnknownHostException {
204
205 synchronized (this) {
206 try {
207 this.wait(delay);
208 } catch (InterruptedException e) {}
209 }
210 return realFactory.createSocket(host, port, localAddress, localPort);
211 }
212
213 public Socket createSocket(
214 final String host,
215 final int port,
216 final InetAddress localAddress,
217 final int localPort,
218 final HttpConnectionParams params
219 ) throws IOException, UnknownHostException {
220
221 if (params == null) {
222 throw new IllegalArgumentException("Parameters may not be null");
223 }
224 int timeout = params.getConnectionTimeout();
225 ControllerThreadSocketFactory.SocketTask task = new ControllerThreadSocketFactory.SocketTask() {
226 public void doit() throws IOException {
227 synchronized (this) {
228 try {
229 this.wait(delay);
230 } catch (InterruptedException e) {}
231 }
232 setSocket(realFactory.createSocket(host, port, localAddress, localPort));
233 }
234 };
235 return ControllerThreadSocketFactory.createSocket(task, timeout);
236 }
237
238 public Socket createSocket(String host, int port)
239 throws IOException, UnknownHostException {
240 synchronized (this) {
241 try {
242 this.wait(delay);
243 } catch (InterruptedException e) {}
244 }
245 return realFactory.createSocket(host, port);
246 }
247
248 }
249
250 }
251