1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    * ====================================================================
6    *
7    *  Copyright 2002-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  import java.net.InetAddress;
37  import java.net.Socket;
38  import java.net.UnknownHostException;
39  
40  import junit.framework.Test;
41  import junit.framework.TestSuite;
42  
43  import org.apache.commons.httpclient.protocol.Protocol;
44  import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
45  
46  /***
47   *
48   * Unit tests for {@link HttpConnection}.
49   *
50   * @author Sean C. Sullivan
51   *
52   * @version $Id$
53   *
54   */
55  public class TestHttpConnection extends TestLocalHostBase {
56      
57      // ------------------------------------------------------------ Constructor
58      public TestHttpConnection(String testName) {
59          super(testName);
60      }
61  
62      // ------------------------------------------------------------------- Main
63      public static void main(String args[]) {
64          String[] testCaseName = { TestHttpConnection.class.getName() };
65          junit.textui.TestRunner.main(testCaseName);
66      }
67  
68      // ------------------------------------------------------- TestCase Methods
69  
70      public static Test suite() {
71          return new TestSuite(TestHttpConnection.class);
72      }
73  
74  
75      // ----------------------------------------------------------- Test Methods
76  
77      public void testConstructThenClose() {
78          HttpConnection conn = new HttpConnection(getHost(), getPort());
79          conn.close();
80          assertTrue( ! conn.isOpen() );
81      }
82  
83      public void testConnTimeout() {
84  
85          // create a custom protocol that will delay for 500 milliseconds
86          Protocol testProtocol = new Protocol(
87              "timeout",
88              new DelayedProtocolSocketFactory(
89                  500, 
90                  Protocol.getProtocol("http").getSocketFactory()
91              ),
92              getPort()
93          );
94  
95          HttpConnection conn = new HttpConnection(getHost(), getPort(), testProtocol);
96          // 1 ms is short enough to make this fail
97          conn.setConnectionTimeout(1);
98          try {
99              conn.open();
100             fail("Should have timed out");
101         } catch(IOException e) {
102             assertTrue(e instanceof HttpConnection.ConnectionTimeoutException);
103             /* should fail */
104         }
105     }
106 
107     public void testForIllegalStateExceptions() {
108         HttpConnection conn = new HttpConnection(getHost(), getPort());
109 
110         try {
111             OutputStream out = conn.getRequestOutputStream();
112             fail("getRequestOutputStream did not throw the expected exception");
113         }
114         catch (IllegalStateException expected) {
115             // this exception is expected
116         }
117         catch (IOException ex) {
118             fail("getRequestOutputStream did not throw the expected exception");
119         }
120 
121         try {
122             OutputStream out = new ChunkedOutputStream(conn.getRequestOutputStream());
123             fail("getRequestOutputStream(true) did not throw the expected exception");
124         }
125         catch (IllegalStateException expected) {
126             // this exception is expected
127         }
128         catch (IOException ex) {
129             fail("getRequestOutputStream(true) did not throw the expected exception");
130         }
131 
132         try {
133             InputStream in = conn.getResponseInputStream();
134             fail("getResponseInputStream() did not throw the expected exception");
135         }
136         catch (IllegalStateException expected) {
137             // this exception is expected
138         }
139         catch (IOException ex) {
140             fail("getResponseInputStream() did not throw the expected exception");
141         }
142 
143     }
144     
145     /***
146      * A ProtocolSocketFactory that delays before creating a socket.
147      */
148     class DelayedProtocolSocketFactory implements ProtocolSocketFactory {
149         
150         private int delay;
151         private ProtocolSocketFactory realFactory;
152             
153         public DelayedProtocolSocketFactory(int delay, ProtocolSocketFactory realFactory) {
154             this.delay = delay;
155             this.realFactory = realFactory;            
156         }
157                 
158         public Socket createSocket(
159             String host,
160             int port,
161             InetAddress clientHost,
162             int clientPort
163         ) throws IOException, UnknownHostException {
164             
165             synchronized (this) {
166                 try {
167                     this.wait(delay);
168                 } catch (InterruptedException e) {}
169             }
170             return realFactory.createSocket(host, port, clientHost, clientPort);
171         }
172 
173         public Socket createSocket(String host, int port)
174             throws IOException, UnknownHostException {
175             synchronized (this) {
176                 try {
177                     this.wait(delay);
178                 } catch (InterruptedException e) {}
179             }
180             return realFactory.createSocket(host, port);
181         }
182 
183     }
184 
185 }
186