001: /*
002: * $Header$
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037: import java.net.InetAddress;
038: import java.net.Socket;
039: import java.net.UnknownHostException;
040:
041: import junit.framework.Test;
042: import junit.framework.TestSuite;
043:
044: import org.apache.commons.httpclient.methods.GetMethod;
045: import org.apache.commons.httpclient.params.HttpConnectionParams;
046: import org.apache.commons.httpclient.protocol.Protocol;
047: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
048: import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
049:
050: /**
051: *
052: * Unit tests for {@link HttpConnection}.
053: *
054: * @author Sean C. Sullivan
055: *
056: * @version $Id: TestHttpConnection.java 480424 2006-11-29 05:56:49Z bayard $
057: *
058: */
059: public class TestHttpConnection extends HttpClientTestBase {
060:
061: // ------------------------------------------------------------ Constructor
062: public TestHttpConnection(String testName) throws Exception {
063: super (testName);
064: }
065:
066: // ------------------------------------------------------------------- Main
067: public static void main(String args[]) {
068: String[] testCaseName = { TestHttpConnection.class.getName() };
069: junit.textui.TestRunner.main(testCaseName);
070: }
071:
072: // ------------------------------------------------------- TestCase Methods
073:
074: public static Test suite() {
075: return new TestSuite(TestHttpConnection.class);
076: }
077:
078: // ----------------------------------------------------------- Test Methods
079:
080: public void testConstructThenClose() {
081: this .server.setHttpService(new EchoService());
082: HttpConnection conn = new HttpConnection(this .server
083: .getLocalAddress(), this .server.getLocalPort());
084: conn.close();
085: assertTrue(!conn.isOpen());
086: }
087:
088: public void testConnTimeoutRelease() {
089: this .server.setHttpService(new EchoService());
090: // create a custom protocol that will delay for 500 milliseconds
091: Protocol testProtocol = new Protocol("timeout",
092: new DelayedProtocolSocketFactory(500, Protocol
093: .getProtocol("http").getSocketFactory()),
094: this .server.getLocalPort());
095:
096: NoHostHttpConnectionManager connectionManager = new NoHostHttpConnectionManager();
097: connectionManager.setConnection(new HttpConnection(this .server
098: .getLocalAddress(), this .server.getLocalPort(),
099: testProtocol));
100: this .client.setHttpConnectionManager(connectionManager);
101: client.getHostConfiguration().setHost(
102: this .server.getLocalAddress(),
103: this .server.getLocalPort(), testProtocol);
104: client.getHttpConnectionManager().getParams()
105: .setConnectionTimeout(1);
106:
107: try {
108: GetMethod get = new GetMethod();
109: client.executeMethod(get);
110: fail("Should have timed out");
111: } catch (IOException e) {
112: /* should fail */
113: assertTrue(e instanceof ConnectTimeoutException);
114: assertTrue(connectionManager.isConnectionReleased());
115: }
116: }
117:
118: public void testConnTimeout() {
119:
120: // create a custom protocol that will delay for 500 milliseconds
121: Protocol testProtocol = new Protocol("timeout",
122: new DelayedProtocolSocketFactory(500, Protocol
123: .getProtocol("http").getSocketFactory()),
124: this .server.getLocalPort());
125:
126: HttpConnection conn = new HttpConnection(this .server
127: .getLocalAddress(), this .server.getLocalPort(),
128: testProtocol);
129: // 1 ms is short enough to make this fail
130: conn.getParams().setConnectionTimeout(1);
131: try {
132: conn.open();
133: fail("Should have timed out");
134: } catch (IOException e) {
135: assertTrue(e instanceof ConnectTimeoutException);
136: /* should fail */
137: }
138: }
139:
140: public void testForIllegalStateExceptions() {
141: HttpConnection conn = new HttpConnection(this .server
142: .getLocalAddress(), this .server.getLocalPort());
143: try {
144: OutputStream out = conn.getRequestOutputStream();
145: fail("getRequestOutputStream did not throw the expected exception");
146: } catch (IllegalStateException expected) {
147: // this exception is expected
148: } catch (IOException ex) {
149: fail("getRequestOutputStream did not throw the expected exception");
150: }
151:
152: try {
153: OutputStream out = new ChunkedOutputStream(conn
154: .getRequestOutputStream());
155: fail("getRequestOutputStream(true) did not throw the expected exception");
156: } catch (IllegalStateException expected) {
157: // this exception is expected
158: } catch (IOException ex) {
159: fail("getRequestOutputStream(true) did not throw the expected exception");
160: }
161:
162: try {
163: InputStream in = conn.getResponseInputStream();
164: fail("getResponseInputStream() did not throw the expected exception");
165: } catch (IllegalStateException expected) {
166: // this exception is expected
167: } catch (IOException ex) {
168: fail("getResponseInputStream() did not throw the expected exception");
169: }
170:
171: }
172:
173: /**
174: * A ProtocolSocketFactory that delays before creating a socket.
175: */
176: class DelayedProtocolSocketFactory implements ProtocolSocketFactory {
177:
178: private int delay;
179: private ProtocolSocketFactory realFactory;
180:
181: public DelayedProtocolSocketFactory(int delay,
182: ProtocolSocketFactory realFactory) {
183: this .delay = delay;
184: this .realFactory = realFactory;
185: }
186:
187: public Socket createSocket(String host, int port,
188: InetAddress localAddress, int localPort)
189: throws IOException, UnknownHostException {
190:
191: synchronized (this ) {
192: try {
193: this .wait(delay);
194: } catch (InterruptedException e) {
195: }
196: }
197: return realFactory.createSocket(host, port, localAddress,
198: localPort);
199: }
200:
201: public Socket createSocket(final String host, final int port,
202: final InetAddress localAddress, final int localPort,
203: final HttpConnectionParams params) throws IOException,
204: UnknownHostException {
205:
206: if (params == null) {
207: throw new IllegalArgumentException(
208: "Parameters may not be null");
209: }
210: int timeout = params.getConnectionTimeout();
211: ControllerThreadSocketFactory.SocketTask task = new ControllerThreadSocketFactory.SocketTask() {
212: public void doit() throws IOException {
213: synchronized (this ) {
214: try {
215: this .wait(delay);
216: } catch (InterruptedException e) {
217: }
218: }
219: setSocket(realFactory.createSocket(host, port,
220: localAddress, localPort));
221: }
222: };
223: return ControllerThreadSocketFactory.createSocket(task,
224: timeout);
225: }
226:
227: public Socket createSocket(String host, int port)
228: throws IOException, UnknownHostException {
229: synchronized (this ) {
230: try {
231: this .wait(delay);
232: } catch (InterruptedException e) {
233: }
234: }
235: return realFactory.createSocket(host, port);
236: }
237:
238: }
239:
240: }
|