1 /*
2 * $Header$
3 * $Revision$
4 * $Date$
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63 package org.apache.commons.httpclient;
64
65 import java.io.IOException;
66 import java.io.InputStream;
67 import java.io.OutputStream;
68 import java.net.InetAddress;
69 import java.net.Socket;
70 import java.net.UnknownHostException;
71
72 import junit.framework.Test;
73 import junit.framework.TestSuite;
74
75 import org.apache.commons.httpclient.protocol.Protocol;
76 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
77
78 /***
79 *
80 * Unit tests for {@link HttpConnection}.
81 *
82 * @author Sean C. Sullivan
83 *
84 * @version $Id$
85 *
86 */
87 public class TestHttpConnection extends TestLocalHostBase {
88
89 // ------------------------------------------------------------ Constructor
90 public TestHttpConnection(String testName) {
91 super(testName);
92 }
93
94 // ------------------------------------------------------------------- Main
95 public static void main(String args[]) {
96 String[] testCaseName = { TestHttpConnection.class.getName() };
97 junit.textui.TestRunner.main(testCaseName);
98 }
99
100 // ------------------------------------------------------- TestCase Methods
101
102 public static Test suite() {
103 return new TestSuite(TestHttpConnection.class);
104 }
105
106
107 // ----------------------------------------------------------- Test Methods
108
109 public void testConstructThenClose() {
110 HttpConnection conn = new HttpConnection(getHost(), getPort());
111 conn.close();
112 assertTrue( ! conn.isOpen() );
113 }
114
115 public void testConnTimeout() {
116
117 // create a custom protocol that will delay for 500 milliseconds
118 Protocol testProtocol = new Protocol(
119 "timeout",
120 new DelayedProtocolSocketFactory(
121 500,
122 Protocol.getProtocol("http").getSocketFactory()
123 ),
124 getPort()
125 );
126
127 HttpConnection conn = new HttpConnection(getHost(), getPort(), testProtocol);
128 // 1 ms is short enough to make this fail
129 conn.setConnectionTimeout(1);
130 try {
131 conn.open();
132 fail("Should have timed out");
133 } catch(IOException e) {
134 assertTrue(e instanceof HttpConnection.ConnectionTimeoutException);
135 /* should fail */
136 }
137 }
138
139 public void testForIllegalStateExceptions() {
140 HttpConnection conn = new HttpConnection(getHost(), getPort());
141
142 try {
143 OutputStream out = conn.getRequestOutputStream();
144 fail("getRequestOutputStream did not throw the expected exception");
145 }
146 catch (IllegalStateException expected) {
147 // this exception is expected
148 }
149 catch (IOException ex) {
150 fail("getRequestOutputStream did not throw the expected exception");
151 }
152
153 try {
154 OutputStream out = new ChunkedOutputStream(conn.getRequestOutputStream());
155 fail("getRequestOutputStream(true) did not throw the expected exception");
156 }
157 catch (IllegalStateException expected) {
158 // this exception is expected
159 }
160 catch (IOException ex) {
161 fail("getRequestOutputStream(true) did not throw the expected exception");
162 }
163
164 try {
165 InputStream in = conn.getResponseInputStream();
166 fail("getResponseInputStream() did not throw the expected exception");
167 }
168 catch (IllegalStateException expected) {
169 // this exception is expected
170 }
171 catch (IOException ex) {
172 fail("getResponseInputStream() did not throw the expected exception");
173 }
174
175 }
176
177 /***
178 * A ProtocolSocketFactory that delays before creating a socket.
179 */
180 class DelayedProtocolSocketFactory implements ProtocolSocketFactory {
181
182 private int delay;
183 private ProtocolSocketFactory realFactory;
184
185 public DelayedProtocolSocketFactory(int delay, ProtocolSocketFactory realFactory) {
186 this.delay = delay;
187 this.realFactory = realFactory;
188 }
189
190 public Socket createSocket(
191 String host,
192 int port,
193 InetAddress clientHost,
194 int clientPort
195 ) throws IOException, UnknownHostException {
196
197 synchronized (this) {
198 try {
199 this.wait(delay);
200 } catch (InterruptedException e) {}
201 }
202 return realFactory.createSocket(host, port, clientHost, clientPort);
203 }
204
205 public Socket createSocket(String host, int port)
206 throws IOException, UnknownHostException {
207 synchronized (this) {
208 try {
209 this.wait(delay);
210 } catch (InterruptedException e) {}
211 }
212 return realFactory.createSocket(host, port);
213 }
214
215 }
216
217 }
218
This page was automatically generated by Maven