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