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.http.client.protocol;
28  
29  import org.apache.http.Header;
30  import org.apache.http.HttpHost;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpRequestInterceptor;
33  import org.apache.http.conn.routing.HttpRoute;
34  import org.apache.http.conn.routing.RouteInfo.LayerType;
35  import org.apache.http.conn.routing.RouteInfo.TunnelType;
36  import org.apache.http.message.BasicHttpRequest;
37  import org.apache.http.protocol.HTTP;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  public class TestRequestClientConnControl {
42  
43      @Test(expected=IllegalArgumentException.class)
44      public void testRequestParameterCheck() throws Exception {
45          final HttpClientContext context = HttpClientContext.create();
46          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
47          interceptor.process(null, context);
48      }
49  
50      @Test
51      public void testConnectionKeepAliveForConnectRequest() throws Exception {
52          final HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
53          final HttpClientContext context = HttpClientContext.create();
54  
55          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
56          interceptor.process(request, context);
57          final Header header1 = request.getFirstHeader("Proxy-Connection");
58          Assert.assertNotNull(header1);
59          Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
60          final Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
61          Assert.assertNull(header2);
62      }
63  
64      @Test
65      public void testConnectionKeepAliveForDirectRequests() throws Exception {
66          final HttpRequest request = new BasicHttpRequest("GET", "/");
67          final HttpClientContext context = HttpClientContext.create();
68  
69          final HttpHost target = new HttpHost("localhost", 80, "http");
70          final HttpRoute route = new HttpRoute(target, null, false);
71  
72          context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
73  
74          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
75          interceptor.process(request, context);
76  
77          final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
78          Assert.assertNotNull(header1);
79          Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
80          final Header header2 = request.getFirstHeader("Proxy-Connection");
81          Assert.assertNull(header2);
82      }
83  
84      @Test
85      public void testConnectionKeepAliveForTunneledRequests() throws Exception {
86          final HttpRequest request = new BasicHttpRequest("GET", "/");
87          final HttpClientContext context = HttpClientContext.create();
88  
89          final HttpHost target = new HttpHost("localhost", 443, "https");
90          final HttpHost proxy = new HttpHost("localhost", 8080);
91          final HttpRoute route = new HttpRoute(target, null, proxy, true,
92                  TunnelType.TUNNELLED, LayerType.LAYERED);
93  
94          context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
95  
96          final HttpRequestInterceptor interceptor = new RequestClientConnControl();
97          interceptor.process(request, context);
98  
99          final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
100         Assert.assertNotNull(header1);
101         Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
102         final Header header2 = request.getFirstHeader("Proxy-Connection");
103         Assert.assertNull(header2);
104     }
105 
106     @Test
107     public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception {
108         final HttpRequest request = new BasicHttpRequest("GET", "/");
109         final HttpClientContext context = HttpClientContext.create();
110 
111         final HttpHost target = new HttpHost("localhost", 80, "http");
112         final HttpHost proxy = new HttpHost("localhost", 8080);
113         final HttpRoute route = new HttpRoute(target, null, proxy, false,
114                 TunnelType.PLAIN, LayerType.PLAIN);
115 
116         context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
117 
118         final HttpRequestInterceptor interceptor = new RequestClientConnControl();
119         interceptor.process(request, context);
120 
121         final Header header1 = request.getFirstHeader("Proxy-Connection");
122         Assert.assertNotNull(header1);
123         Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
124         final Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
125         Assert.assertNull(header2);
126     }
127 
128     @Test
129     public void testPreserveCustomConnectionHeader() throws Exception {
130         final HttpRequest request = new BasicHttpRequest("GET", "/");
131         request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
132         final HttpClientContext context = HttpClientContext.create();
133 
134         final HttpHost target = new HttpHost("localhost", 443, "https");
135         final HttpHost proxy = new HttpHost("localhost", 8080);
136         final HttpRoute route = new HttpRoute(target, null, proxy, true,
137                 TunnelType.TUNNELLED, LayerType.LAYERED);
138 
139         context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
140 
141         final HttpRequestInterceptor interceptor = new RequestClientConnControl();
142         interceptor.process(request, context);
143 
144         final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
145         Assert.assertNotNull(header1);
146         Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue());
147         final Header header2 = request.getFirstHeader("Proxy-Connection");
148         Assert.assertNull(header2);
149     }
150 
151     @Test
152     public void testPreserveCustomProxyConnectionHeader() throws Exception {
153         final HttpRequest request = new BasicHttpRequest("GET", "/");
154         request.addHeader("Proxy-Connection", HTTP.CONN_CLOSE);
155         final HttpClientContext context = HttpClientContext.create();
156 
157         final HttpHost target = new HttpHost("localhost", 80, "http");
158         final HttpHost proxy = new HttpHost("localhost", 8080);
159         final HttpRoute route = new HttpRoute(target, null, proxy, false,
160                 TunnelType.PLAIN, LayerType.PLAIN);
161 
162         context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
163 
164         final HttpRequestInterceptor interceptor = new RequestClientConnControl();
165         interceptor.process(request, context);
166 
167         final Header header1 = request.getFirstHeader("Proxy-Connection");
168         Assert.assertNotNull(header1);
169         Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue());
170     }
171 
172 }