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