1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestConnectionPersistence.java,v 1.1 2004/11/07 12:31:42 olegk Exp $
3    * $Revision: 1.1 $
4    * $Date: 2004/11/07 12:31:42 $
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  
35  import junit.framework.Test;
36  import junit.framework.TestSuite;
37  
38  /***
39   * Connection persistence tests
40   * 
41   * @author Oleg Kalnichevski
42   *
43   * @version $Id: TestConnectionPersistence.java,v 1.1 2004/11/07 12:31:42 olegk Exp $
44   */
45  public class TestConnectionPersistence extends HttpClientTestBase {
46      
47      // ------------------------------------------------------------ Constructor
48      public TestConnectionPersistence(final String testName) throws IOException {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------------------- Main
53      public static void main(String args[]) {
54          String[] testCaseName = { TestConnectionPersistence.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------- TestCase Methods
59  
60      public static Test suite() {
61          return new TestSuite(TestConnectionPersistence.class);
62      }
63  
64      // ----------------------------------------------------------- Test Methods
65  
66      public void testConnPersisenceHTTP10() throws Exception {
67          this.server.setHttpService(new EchoService());
68  
69          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
70          
71          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
72          this.client.setHttpConnectionManager(connman);
73          
74          PostMethod httppost = new PostMethod("/test/");
75          httppost.setRequestEntity(new StringRequestEntity("stuff"));
76          try {
77              this.client.executeMethod(httppost);
78          } finally {
79              httppost.releaseConnection();
80          }
81          assertFalse(connman.getConection().isOpen());
82  
83          httppost = new PostMethod("/test/");
84          httppost.setRequestEntity(new StringRequestEntity("more stuff"));
85          try {
86              this.client.executeMethod(httppost);
87          } finally {
88              httppost.releaseConnection();
89          }
90          assertFalse(connman.getConection().isOpen());
91      }
92  
93      public void testConnPersisenceHTTP11() throws Exception {
94          this.server.setHttpService(new EchoService());
95  
96          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
97          
98          this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
99          this.client.setHttpConnectionManager(connman);
100         
101         PostMethod httppost = new PostMethod("/test/");
102         httppost.setRequestEntity(new StringRequestEntity("stuff"));
103         try {
104             this.client.executeMethod(httppost);
105         } finally {
106             httppost.releaseConnection();
107         }
108         assertTrue(connman.getConection().isOpen());
109 
110         httppost = new PostMethod("/test/");
111         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
112         try {
113             this.client.executeMethod(httppost);
114         } finally {
115             httppost.releaseConnection();
116         }
117         assertTrue(connman.getConection().isOpen());
118     }
119 
120     public void testConnClose() throws Exception {
121         this.server.setHttpService(new EchoService());
122 
123         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
124         
125         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
126         this.client.setHttpConnectionManager(connman);
127         
128         PostMethod httppost = new PostMethod("/test/");
129         httppost.setRequestEntity(new StringRequestEntity("stuff"));
130         try {
131             this.client.executeMethod(httppost);
132         } finally {
133             httppost.releaseConnection();
134         }
135         assertTrue(connman.getConection().isOpen());
136 
137         httppost = new PostMethod("/test/");
138         httppost.setRequestHeader("Connection", "close");
139         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
140         try {
141             this.client.executeMethod(httppost);
142         } finally {
143             httppost.releaseConnection();
144         }
145         assertFalse(connman.getConection().isOpen());
146     }
147 
148     public void testConnKeepAlive() throws Exception {
149         this.server.setHttpService(new EchoService());
150 
151         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
152         
153         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
154         this.client.setHttpConnectionManager(connman);
155         
156         PostMethod httppost = new PostMethod("/test/");
157         httppost.setRequestEntity(new StringRequestEntity("stuff"));
158         try {
159             this.client.executeMethod(httppost);
160         } finally {
161             httppost.releaseConnection();
162         }
163         assertFalse(connman.getConection().isOpen());
164 
165         httppost = new PostMethod("/test/");
166         httppost.setRequestHeader("Connection", "keep-alive");
167         httppost.setRequestEntity(new StringRequestEntity("more stuff"));
168         try {
169             this.client.executeMethod(httppost);
170         } finally {
171             httppost.releaseConnection();
172         }
173         assertTrue(connman.getConection().isOpen());
174     }
175 
176 }
177