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.Assert;
39 import org.junit.Test;
40
41
42
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 }