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