    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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    * ====================================================================
    6    *
    7    *  Licensed to the Apache Software Foundation (ASF) under one or more
    8    *  contributor license agreements.  See the NOTICE file distributed with
    9    *  this work for additional information regarding copyright ownership.
    10   *  The ASF licenses this file to You under the Apache License, Version 2.0
    11   *  (the "License"); you may not use this file except in compliance with
    12   *  the License.  You may obtain a copy of the License at
    13   *
    14   *      http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   *  Unless required by applicable law or agreed to in writing, software
    17   *  distributed under the License is distributed on an "AS IS" BASIS,
    18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   *  See the License for the specific language governing permissions and
    20   *  limitations under the License.
    21   * ====================================================================
    22   *
    23   * This software consists of voluntary contributions made by many
    24   * individuals on behalf of the Apache Software Foundation.  For more
    25   * information on the Apache Software Foundation, please see
    26   * <http://www.apache.org/>.
    27   */
    28
    29  package org.apache.commons.httpclient;
    30
    31  import java.io.IOException;
    32
    33  import org.apache.commons.httpclient.methods.GetMethod;
    34  import org.apache.commons.httpclient.methods.PostMethod;
    35  import org.apache.commons.httpclient.methods.StringRequestEntity;
    36  import org.apache.commons.httpclient.server.HttpRequestHandler;
    37  import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
    38  import org.apache.commons.httpclient.server.SimpleProxy;
    39  import org.apache.commons.httpclient.server.SimpleRequest;
    40  import org.apache.commons.httpclient.server.SimpleResponse;
    41
    42  import junit.framework.Test;
    43  import junit.framework.TestSuite;
    44
    45  /**
    46   * Connection persistence tests
    47   *
    48   * @author Oleg Kalnichevski
    49   *
    50   * @version $Id: TestConnectionPersistence.java 480424 2006-11-29 05:56:49Z bayard $
    51   */
    52  public class TestConnectionPersistence extends HttpClientTestBase {
    53
    54      // ------------------------------------------------------------ Constructor
    55      public TestConnectionPersistence(final String testName) throws IOException {
    56          super(testName);
    57      }
    58
    59      // ------------------------------------------------------------------- Main
    60      public static void main(String args[]) {
    61          String[] testCaseName = { TestConnectionPersistence.class.getName() };
    62          junit.textui.TestRunner.main(testCaseName);
    63      }
    64
    65      // ------------------------------------------------------- TestCase Methods
    66
    67      public static Test suite() {
    68          return new TestSuite(TestConnectionPersistence.class);
    69      }
    70
    71      // ----------------------------------------------------------- Test Methods
    72
    73      public void testConnPersisenceHTTP10() throws Exception {
    74          this.server.setHttpService(new EchoService());
    75
    76          AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    77
    78          this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
    79          this.client.setHttpConnectionManager(connman);
    80
    81          PostMethod httppost = new PostMethod("/test/");
    82          httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
    83          try {
    84              this.client.executeMethod(httppost);
    85          } finally {
    86              httppost.releaseConnection();
    87          }
    88          assertFalse(connman.getConection().isOpen());
    89
    90          httppost = new PostMethod("/test/");
    91          httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
    92          try {
    93              this.client.executeMethod(httppost);
    94          } finally {
    95              httppost.releaseConnection();
    96          }
    97          assertFalse(connman.getConection().isOpen());
    98      }
    99
    100     public void testConnPersisenceHTTP11() throws Exception {
    101         this.server.setHttpService(new EchoService());
    102
    103         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    104
    105         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
    106         this.client.setHttpConnectionManager(connman);
    107
    108         PostMethod httppost = new PostMethod("/test/");
    109         httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
    110         try {
    111             this.client.executeMethod(httppost);
    112         } finally {
    113             httppost.releaseConnection();
    114         }
    115         assertTrue(connman.getConection().isOpen());
    116
    117         httppost = new PostMethod("/test/");
    118         httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
    119         try {
    120             this.client.executeMethod(httppost);
    121         } finally {
    122             httppost.releaseConnection();
    123         }
    124         assertTrue(connman.getConection().isOpen());
    125     }
    126
    127     public void testConnClose() throws Exception {
    128         this.server.setHttpService(new EchoService());
    129
    130         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    131
    132         this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
    133         this.client.setHttpConnectionManager(connman);
    134
    135         PostMethod httppost = new PostMethod("/test/");
    136         httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
    137         try {
    138             this.client.executeMethod(httppost);
    139         } finally {
    140             httppost.releaseConnection();
    141         }
    142         assertTrue(connman.getConection().isOpen());
    143
    144         httppost = new PostMethod("/test/");
    145         httppost.setRequestHeader("Connection", "close");
    146         httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
    147         try {
    148             this.client.executeMethod(httppost);
    149         } finally {
    150             httppost.releaseConnection();
    151         }
    152         assertFalse(connman.getConection().isOpen());
    153     }
    154
    155     public void testConnKeepAlive() throws Exception {
    156         this.server.setHttpService(new EchoService());
    157
    158         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    159
    160         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
    161         this.client.setHttpConnectionManager(connman);
    162
    163         PostMethod httppost = new PostMethod("/test/");
    164         httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
    165         try {
    166             this.client.executeMethod(httppost);
    167         } finally {
    168             httppost.releaseConnection();
    169         }
    170         assertFalse(connman.getConection().isOpen());
    171
    172         httppost = new PostMethod("/test/");
    173         httppost.setRequestHeader("Connection", "keep-alive");
    174         httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
    175         try {
    176             this.client.executeMethod(httppost);
    177         } finally {
    178             httppost.releaseConnection();
    179         }
    180         assertTrue(connman.getConection().isOpen());
    181     }
    182
    183     public void testRequestConnClose() throws Exception {
    184         this.server.setRequestHandler(new HttpRequestHandler() {
    185
    186             public boolean processRequest(
    187                     final SimpleHttpServerConnection conn,
    188                     final SimpleRequest request) throws IOException {
    189
    190                 // Make sure the request if fully consumed
    191                 request.getBodyBytes();
    192
    193                 SimpleResponse response = new SimpleResponse();
    194                 response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
    195                 response.setBodyString("stuff back");
    196
    197                 conn.setKeepAlive(true);
    198                 conn.writeResponse(response);
    199
    200                 return true;
    201             }
    202
    203         });
    204
    205         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    206
    207         this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
    208         this.client.setHttpConnectionManager(connman);
    209
    210         PostMethod httppost = new PostMethod("/test/");
    211         httppost.setRequestHeader("Connection", "close");
    212         httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
    213         try {
    214             this.client.executeMethod(httppost);
    215         } finally {
    216             httppost.releaseConnection();
    217         }
    218         assertFalse(connman.getConection().isOpen());
    219     }
    220
    221     public void testProxyConnClose() throws Exception {
    222         this.server.setHttpService(new EchoService());
    223         this.proxy = new SimpleProxy();
    224         this.client.getHostConfiguration().setProxy(
    225             proxy.getLocalAddress(),
    226             proxy.getLocalPort());
    227
    228         AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    229
    230         this.client.setHttpConnectionManager(connman);
    231
    232         GetMethod httpget = new GetMethod("/test/");
    233         try {
    234             this.client.executeMethod(httpget);
    235         } finally {
    236         	httpget.releaseConnection();
    237         }
    238         assertTrue(connman.getConection().isOpen());
    239
    240         httpget = new GetMethod("/test/");
    241         httpget.setRequestHeader("Proxy-Connection", "Close");
    242         try {
    243             this.client.executeMethod(httpget);
    244         } finally {
    245         	httpget.releaseConnection();
    246         }
    247         assertFalse(connman.getConection().isOpen());
    248         assertEquals("Close", httpget.getRequestHeader("Proxy-Connection").getValue());
    249     }
    250
    251
    252 }
    253