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.util.TimeValue;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 class TestDefaultConnKeepAliveStrategy {
43
44 @Test
45 void testIllegalResponseArg() {
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 void testNoKeepAliveHeader() {
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 void testEmptyKeepAliveHeader() {
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 void testInvalidKeepAliveHeader() {
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 void testKeepAliveHeader() {
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 }