1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestConnectionPersistence.java,v 1.2 2004/12/20 11:42:30 olegk Exp $
3    * $Revision: 280648 $
4    * $Date: 2005-09-13 16:34:16 -0400 (Tue, 13 Sep 2005) $
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  
28  package org.apache.commons.httpclient;
29  
30  import java.io.IOException;
31  
32  import org.apache.commons.httpclient.methods.GetMethod;
33  import org.apache.commons.httpclient.methods.PostMethod;
34  import org.apache.commons.httpclient.methods.StringRequestEntity;
35  import org.apache.commons.httpclient.server.HttpRequestHandler;
36  import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
37  import org.apache.commons.httpclient.server.SimpleProxy;
38  import org.apache.commons.httpclient.server.SimpleRequest;
39  import org.apache.commons.httpclient.server.SimpleResponse;
40  
41  import junit.framework.Test;
42  import junit.framework.TestSuite;
43  
44  /***
45   * Connection persistence tests
46   * 
47   * @author Oleg Kalnichevski
48   *
49   * @version $Id: TestConnectionPersistence.java 280648 2005-09-13 20:34:16Z olegk $
50   */
51  public class TestConnectionPersistence extends HttpClientTestBase {
52      
53      // ------------------------------------------------------------ Constructor
54      public TestConnectionPersistence(final String testName) throws IOException {
55          super(testName);
56      }
57  
58      // ------------------------------------------------------------------- Main
59      public static void main(String args[]) {
60          String[] testCaseName = { TestConnectionPersistence.class.getName() };
61          junit.textui.TestRunner.main(testCaseName);
62      }
63  
64      // ------------------------------------------------------- TestCase Methods
65  
66      public static Test suite() {
67          return new TestSuite(TestConnectionPersistence.class);
68      }
69  
70      // ----------------------------------------------------------- Test Methods
71  
72      public void testConnPersisenceHTTP10() throws Exception {
73          this.server.setHttpService(new EchoService());
74  
75          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
76          
77          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
78          this.client.setHttpConnectionManager(connman);
79          
80          PostMethod httppost = new PostMethod("/test/");
81          httppost.setRequestEntity(new StringRequestEntity("stuff"));
82          try {
83              this.client.executeMethod(httppost);
84          } finally {
85              httppost.releaseConnection();
86          }
87          assertFalse(connman.getConection().isOpen());
88  
89          httppost = new PostMethod("/test/");
90          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
91          try {
92              this.client.executeMethod(httppost);
93          } finally {
94              httppost.releaseConnection();
95          }
96          assertFalse(connman.getConection().isOpen());
97      }
98  
99      public void testConnPersisenceHTTP11() throws Exception {
100         this.server.setHttpService(new EchoService());
101 
102         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
103         
104         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
105         this.client.setHttpConnectionManager(connman);
106         
107         PostMethod httppost = new PostMethod("/test/");
108         httppost.setRequestEntity(new StringRequestEntity("stuff"));
109         try {
110             this.client.executeMethod(httppost);
111         } finally {
112             httppost.releaseConnection();
113         }
114         assertTrue(connman.getConection().isOpen());
115 
116         httppost = new PostMethod("/test/");
117         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
118         try {
119             this.client.executeMethod(httppost);
120         } finally {
121             httppost.releaseConnection();
122         }
123         assertTrue(connman.getConection().isOpen());
124     }
125 
126     public void testConnClose() throws Exception {
127         this.server.setHttpService(new EchoService());
128 
129         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
130         
131         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
132         this.client.setHttpConnectionManager(connman);
133         
134         PostMethod httppost = new PostMethod("/test/");
135         httppost.setRequestEntity(new StringRequestEntity("stuff"));
136         try {
137             this.client.executeMethod(httppost);
138         } finally {
139             httppost.releaseConnection();
140         }
141         assertTrue(connman.getConection().isOpen());
142 
143         httppost = new PostMethod("/test/");
144         httppost.setRequestHeader("Connection", "close");
145         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
146         try {
147             this.client.executeMethod(httppost);
148         } finally {
149             httppost.releaseConnection();
150         }
151         assertFalse(connman.getConection().isOpen());
152     }
153 
154     public void testConnKeepAlive() throws Exception {
155         this.server.setHttpService(new EchoService());
156 
157         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
158         
159         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
160         this.client.setHttpConnectionManager(connman);
161         
162         PostMethod httppost = new PostMethod("/test/");
163         httppost.setRequestEntity(new StringRequestEntity("stuff"));
164         try {
165             this.client.executeMethod(httppost);
166         } finally {
167             httppost.releaseConnection();
168         }
169         assertFalse(connman.getConection().isOpen());
170 
171         httppost = new PostMethod("/test/");
172         httppost.setRequestHeader("Connection", "keep-alive");
173         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
174         try {
175             this.client.executeMethod(httppost);
176         } finally {
177             httppost.releaseConnection();
178         }
179         assertTrue(connman.getConection().isOpen());
180     }
181 
182     public void testRequestConnClose() throws Exception {
183         this.server.setRequestHandler(new HttpRequestHandler() {
184            
185             public boolean processRequest(
186                     final SimpleHttpServerConnection conn,
187                     final SimpleRequest request) throws IOException {
188 
189                 // Make sure the request if fully consumed
190                 request.getBodyBytes();
191                 
192                 SimpleResponse response = new SimpleResponse();
193                 response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
194                 response.setBodyString("stuff back");
195 
196                 conn.setKeepAlive(true);
197                 conn.writeResponse(response);
198                 
199                 return true;
200             }
201             
202         });
203 
204         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
205         
206         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
207         this.client.setHttpConnectionManager(connman);
208         
209         PostMethod httppost = new PostMethod("/test/");
210         httppost.setRequestHeader("Connection", "close");
211         httppost.setRequestEntity(new StringRequestEntity("stuff"));
212         try {
213             this.client.executeMethod(httppost);
214         } finally {
215             httppost.releaseConnection();
216         }
217         assertFalse(connman.getConection().isOpen());
218     }
219 
220     public void testProxyConnClose() throws Exception {
221         this.server.setHttpService(new EchoService());
222         this.proxy = new SimpleProxy();
223         this.client.getHostConfiguration().setProxy(
224             proxy.getLocalAddress(), 
225             proxy.getLocalPort());                
226 
227         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
228         
229         this.client.setHttpConnectionManager(connman);
230         
231         GetMethod httpget = new GetMethod("/test/");
232         try {
233             this.client.executeMethod(httpget);
234         } finally {
235         	httpget.releaseConnection();
236         }
237         assertTrue(connman.getConection().isOpen());
238 
239         httpget = new GetMethod("/test/");
240         httpget.setRequestHeader("Proxy-Connection", "Close");
241         try {
242             this.client.executeMethod(httpget);
243         } finally {
244         	httpget.releaseConnection();
245         }
246         assertFalse(connman.getConection().isOpen());
247         assertEquals("Close", httpget.getRequestHeader("Proxy-Connection").getValue());
248     }
249 
250     
251 }
252