View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client;
28  
29  import org.apache.http.HttpResponse;
30  import org.apache.http.HttpStatus;
31  import org.apache.http.HttpVersion;
32  import org.apache.http.conn.ConnectionKeepAliveStrategy;
33  import org.apache.http.message.BasicHttpResponse;
34  import org.apache.http.message.BasicStatusLine;
35  import org.apache.http.protocol.BasicHttpContext;
36  import org.apache.http.protocol.HttpContext;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   *  Simple tests for {@link DefaultConnectionKeepAliveStrategy}.
42   */
43  public class TestDefaultConnKeepAliveStrategy {
44  
45      @Test(expected=IllegalArgumentException.class)
46      public void testIllegalResponseArg() throws Exception {
47          final HttpContext context = new BasicHttpContext(null);
48          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
49          keepAliveStrat.getKeepAliveDuration(null, context);
50      }
51  
52      @Test
53      public void testNoKeepAliveHeader() throws Exception {
54          final HttpContext context = new BasicHttpContext(null);
55          final HttpResponse response = new BasicHttpResponse(
56                  new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
57          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
58          final long d = keepAliveStrat.getKeepAliveDuration(response, context);
59          Assert.assertEquals(-1, d);
60      }
61  
62      @Test
63      public void testEmptyKeepAliveHeader() throws Exception {
64          final HttpContext context = new BasicHttpContext(null);
65          final HttpResponse response = new BasicHttpResponse(
66                  new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
67          response.addHeader("Keep-Alive", "timeout, max=20");
68          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
69          final long d = keepAliveStrat.getKeepAliveDuration(response, context);
70          Assert.assertEquals(-1, d);
71      }
72  
73      @Test
74      public void testInvalidKeepAliveHeader() throws Exception {
75          final HttpContext context = new BasicHttpContext(null);
76          final HttpResponse response = new BasicHttpResponse(
77                  new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
78          response.addHeader("Keep-Alive", "timeout=whatever, max=20");
79          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
80          final long d = keepAliveStrat.getKeepAliveDuration(response, context);
81          Assert.assertEquals(-1, d);
82      }
83  
84      @Test
85      public void testKeepAliveHeader() throws Exception {
86          final HttpContext context = new BasicHttpContext(null);
87          final HttpResponse response = new BasicHttpResponse(
88                  new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
89          response.addHeader("Keep-Alive", "timeout=300, max=20");
90          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
91          final long d = keepAliveStrat.getKeepAliveDuration(response, context);
92          Assert.assertEquals(300000, d);
93      }
94  
95  }