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.protocol;
28  
29  import org.apache.hc.client5.http.HttpRoute;
30  import org.apache.hc.client5.http.RouteInfo.LayerType;
31  import org.apache.hc.client5.http.RouteInfo.TunnelType;
32  import org.apache.hc.core5.http.Header;
33  import org.apache.hc.core5.http.HeaderElements;
34  import org.apache.hc.core5.http.HttpHeaders;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpRequestInterceptor;
38  import org.apache.hc.core5.http.message.BasicHttpRequest;
39  import org.junit.jupiter.api.Assertions;
40  import org.junit.jupiter.api.Test;
41  
42  public class TestRequestClientConnControl {
43  
44      @Test
45      public void testRequestParameterCheck() throws Exception {
46          final HttpClientContext context = HttpClientContext.create();
47          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
48          Assertions.assertThrows(NullPointerException.class, () ->
49                  interceptor.process(null, null, context));
50      }
51  
52      @Test
53      public void testConnectionKeepAliveForConnectRequest() throws Exception {
54          final HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
55          final HttpClientContext context = HttpClientContext.create();
56  
57          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
58          interceptor.process(request, null, context);
59          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
60          Assertions.assertNull(header);
61      }
62  
63      @Test
64      public void testConnectionKeepAliveForDirectRequests() throws Exception {
65          final HttpRequest request = new BasicHttpRequest("GET", "/");
66          final HttpClientContext context = HttpClientContext.create();
67  
68          final HttpHost target = new HttpHost("http", "localhost", 80);
69          final HttpRoute route = new HttpRoute(target, null, false);
70  
71          context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
72  
73          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
74          interceptor.process(request, null, context);
75  
76          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
77          Assertions.assertNotNull(header);
78          Assertions.assertEquals(HeaderElements.KEEP_ALIVE, header.getValue());
79      }
80  
81      @Test
82      public void testConnectionKeepAliveForTunneledRequests() throws Exception {
83          final HttpRequest request = new BasicHttpRequest("GET", "/");
84          final HttpClientContext context = HttpClientContext.create();
85  
86          final HttpHost target = new HttpHost("https", "localhost", 443);
87          final HttpHost proxy = new HttpHost("localhost", 8080);
88          final HttpRoute route = new HttpRoute(target, null, proxy, true,
89                  TunnelType.TUNNELLED, LayerType.LAYERED);
90  
91          context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
92  
93          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
94          interceptor.process(request, null, context);
95  
96          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
97          Assertions.assertNotNull(header);
98          Assertions.assertEquals(HeaderElements.KEEP_ALIVE, header.getValue());
99      }
100 
101     @Test
102     public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception {
103         final HttpRequest request = new BasicHttpRequest("GET", "/");
104         final HttpClientContext context = HttpClientContext.create();
105 
106         final HttpHost target = new HttpHost("http", "localhost", 80);
107         final HttpHost proxy = new HttpHost("localhost", 8080);
108         final HttpRoute route = new HttpRoute(target, null, proxy, false,
109                 TunnelType.PLAIN, LayerType.PLAIN);
110 
111         context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
112 
113         final HttpRequestInterceptor interceptor = new RequestClientConnControl();
114         interceptor.process(request, null, context);
115 
116         final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
117         Assertions.assertNull(header);
118     }
119 
120     @Test
121     public void testPreserveCustomConnectionHeader() throws Exception {
122         final HttpRequest request = new BasicHttpRequest("GET", "/");
123         request.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
124         final HttpClientContext context = HttpClientContext.create();
125 
126         final HttpHost target = new HttpHost("https", "localhost", 443);
127         final HttpHost proxy = new HttpHost("localhost", 8080);
128         final HttpRoute route = new HttpRoute(target, null, proxy, true,
129                 TunnelType.TUNNELLED, LayerType.LAYERED);
130 
131         context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
132 
133         final HttpRequestInterceptor interceptor = new RequestClientConnControl();
134         interceptor.process(request, null, context);
135 
136         final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
137         Assertions.assertNotNull(header);
138         Assertions.assertEquals(HeaderElements.CLOSE, header.getValue());
139     }
140 
141 }