1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }