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.hc.client5.http.impl;
28  
29  import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
30  import org.apache.hc.client5.http.config.RequestConfig;
31  import org.apache.hc.client5.http.protocol.HttpClientContext;
32  import org.apache.hc.core5.http.HttpResponse;
33  import org.apache.hc.core5.http.HttpStatus;
34  import org.apache.hc.core5.http.message.BasicHttpResponse;
35  import org.apache.hc.core5.http.protocol.BasicHttpContext;
36  import org.apache.hc.core5.http.protocol.HttpContext;
37  import org.apache.hc.core5.util.TimeValue;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  /**
42   *  Simple tests for {@link DefaultConnectionKeepAliveStrategy}.
43   */
44  public class TestDefaultConnKeepAliveStrategy {
45  
46      @Test(expected=NullPointerException.class)
47      public void testIllegalResponseArg() throws Exception {
48          final HttpContext context = new BasicHttpContext(null);
49          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
50          keepAliveStrat.getKeepAliveDuration(null, context);
51      }
52  
53      @Test
54      public void testNoKeepAliveHeader() throws Exception {
55          final HttpClientContext context = HttpClientContext.create();
56          context.setRequestConfig( RequestConfig.custom()
57                  .setConnectionKeepAlive(TimeValue.NEG_ONE_MILLISECOND)
58                  .build());
59          final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
60          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
61          final TimeValue d = keepAliveStrat.getKeepAliveDuration(response, context);
62          Assert.assertEquals(TimeValue.NEG_ONE_MILLISECOND, d);
63      }
64  
65      @Test
66      public void testEmptyKeepAliveHeader() throws Exception {
67          final HttpClientContext context = HttpClientContext.create();
68          context.setRequestConfig( RequestConfig.custom()
69                  .setConnectionKeepAlive(TimeValue.NEG_ONE_MILLISECOND)
70                  .build());
71          final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
72          response.addHeader("Keep-Alive", "timeout, max=20");
73          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
74          final TimeValue d = keepAliveStrat.getKeepAliveDuration(response, context);
75          Assert.assertEquals(TimeValue.NEG_ONE_MILLISECOND, d);
76      }
77  
78      @Test
79      public void testInvalidKeepAliveHeader() throws Exception {
80          final HttpClientContext context = HttpClientContext.create();
81          context.setRequestConfig( RequestConfig.custom()
82                  .setConnectionKeepAlive(TimeValue.NEG_ONE_MILLISECOND)
83                  .build());
84          final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
85          response.addHeader("Keep-Alive", "timeout=whatever, max=20");
86          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
87          final TimeValue d = keepAliveStrat.getKeepAliveDuration(response, context);
88          Assert.assertEquals(TimeValue.NEG_ONE_MILLISECOND, d);
89      }
90  
91      @Test
92      public void testKeepAliveHeader() throws Exception {
93          final HttpClientContext context = HttpClientContext.create();
94          context.setRequestConfig( RequestConfig.custom()
95                  .setConnectionKeepAlive(TimeValue.NEG_ONE_MILLISECOND)
96                  .build());
97          final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
98          response.addHeader("Keep-Alive", "timeout=300, max=20");
99          final ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy();
100         final TimeValue d = keepAliveStrat.getKeepAliveDuration(response, context);
101         Assert.assertEquals(TimeValue.ofSeconds(300), d);
102     }
103 
104 }