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: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 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.PostMethod;
33  import org.apache.commons.httpclient.methods.StringRequestEntity;
34  import org.apache.commons.httpclient.server.HttpRequestHandler;
35  import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
36  import org.apache.commons.httpclient.server.SimpleRequest;
37  import org.apache.commons.httpclient.server.SimpleResponse;
38  
39  import junit.framework.Test;
40  import junit.framework.TestSuite;
41  
42  /***
43   * Connection persistence tests
44   * 
45   * @author Oleg Kalnichevski
46   *
47   * @version $Id: TestConnectionPersistence.java 155418 2005-02-26 13:01:52Z dirkv $
48   */
49  public class TestConnectionPersistence extends HttpClientTestBase {
50      
51      // ------------------------------------------------------------ Constructor
52      public TestConnectionPersistence(final String testName) throws IOException {
53          super(testName);
54      }
55  
56      // ------------------------------------------------------------------- Main
57      public static void main(String args[]) {
58          String[] testCaseName = { TestConnectionPersistence.class.getName() };
59          junit.textui.TestRunner.main(testCaseName);
60      }
61  
62      // ------------------------------------------------------- TestCase Methods
63  
64      public static Test suite() {
65          return new TestSuite(TestConnectionPersistence.class);
66      }
67  
68      // ----------------------------------------------------------- Test Methods
69  
70      public void testConnPersisenceHTTP10() throws Exception {
71          this.server.setHttpService(new EchoService());
72  
73          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
74          
75          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
76          this.client.setHttpConnectionManager(connman);
77          
78          PostMethod httppost = new PostMethod("/test/");
79          httppost.setRequestEntity(new StringRequestEntity("stuff"));
80          try {
81              this.client.executeMethod(httppost);
82          } finally {
83              httppost.releaseConnection();
84          }
85          assertFalse(connman.getConection().isOpen());
86  
87          httppost = new PostMethod("/test/");
88          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
89          try {
90              this.client.executeMethod(httppost);
91          } finally {
92              httppost.releaseConnection();
93          }
94          assertFalse(connman.getConection().isOpen());
95      }
96  
97      public void testConnPersisenceHTTP11() throws Exception {
98          this.server.setHttpService(new EchoService());
99  
100         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
101         
102         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
103         this.client.setHttpConnectionManager(connman);
104         
105         PostMethod httppost = new PostMethod("/test/");
106         httppost.setRequestEntity(new StringRequestEntity("stuff"));
107         try {
108             this.client.executeMethod(httppost);
109         } finally {
110             httppost.releaseConnection();
111         }
112         assertTrue(connman.getConection().isOpen());
113 
114         httppost = new PostMethod("/test/");
115         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
116         try {
117             this.client.executeMethod(httppost);
118         } finally {
119             httppost.releaseConnection();
120         }
121         assertTrue(connman.getConection().isOpen());
122     }
123 
124     public void testConnClose() throws Exception {
125         this.server.setHttpService(new EchoService());
126 
127         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
128         
129         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
130         this.client.setHttpConnectionManager(connman);
131         
132         PostMethod httppost = new PostMethod("/test/");
133         httppost.setRequestEntity(new StringRequestEntity("stuff"));
134         try {
135             this.client.executeMethod(httppost);
136         } finally {
137             httppost.releaseConnection();
138         }
139         assertTrue(connman.getConection().isOpen());
140 
141         httppost = new PostMethod("/test/");
142         httppost.setRequestHeader("Connection", "close");
143         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
144         try {
145             this.client.executeMethod(httppost);
146         } finally {
147             httppost.releaseConnection();
148         }
149         assertFalse(connman.getConection().isOpen());
150     }
151 
152     public void testConnKeepAlive() throws Exception {
153         this.server.setHttpService(new EchoService());
154 
155         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
156         
157         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
158         this.client.setHttpConnectionManager(connman);
159         
160         PostMethod httppost = new PostMethod("/test/");
161         httppost.setRequestEntity(new StringRequestEntity("stuff"));
162         try {
163             this.client.executeMethod(httppost);
164         } finally {
165             httppost.releaseConnection();
166         }
167         assertFalse(connman.getConection().isOpen());
168 
169         httppost = new PostMethod("/test/");
170         httppost.setRequestHeader("Connection", "keep-alive");
171         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
172         try {
173             this.client.executeMethod(httppost);
174         } finally {
175             httppost.releaseConnection();
176         }
177         assertTrue(connman.getConection().isOpen());
178     }
179 
180     public void testRequestConnClose() throws Exception {
181         this.server.setRequestHandler(new HttpRequestHandler() {
182            
183             public boolean processRequest(
184                     final SimpleHttpServerConnection conn,
185                     final SimpleRequest request) throws IOException {
186 
187                 // Make sure the request if fully consumed
188                 request.getBodyBytes();
189                 
190                 SimpleResponse response = new SimpleResponse();
191                 response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
192                 response.setBodyString("stuff back");
193 
194                 conn.setKeepAlive(true);
195                 conn.writeResponse(response);
196                 
197                 return true;
198             }
199             
200         });
201 
202         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
203         
204         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
205         this.client.setHttpConnectionManager(connman);
206         
207         PostMethod httppost = new PostMethod("/test/");
208         httppost.setRequestHeader("Connection", "close");
209         httppost.setRequestEntity(new StringRequestEntity("stuff"));
210         try {
211             this.client.executeMethod(httppost);
212         } finally {
213             httppost.releaseConnection();
214         }
215         assertFalse(connman.getConection().isOpen());
216     }
217 
218 }
219