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  
28  package org.apache.http.impl.conn;
29  
30  import org.apache.http.HttpHost;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpVersion;
33  import org.apache.http.client.config.RequestConfig;
34  import org.apache.http.client.protocol.HttpClientContext;
35  import org.apache.http.conn.SchemePortResolver;
36  import org.apache.http.conn.routing.HttpRoute;
37  import org.apache.http.message.BasicHttpRequest;
38  import org.apache.http.protocol.BasicHttpContext;
39  import org.apache.http.protocol.HttpContext;
40  import org.junit.Assert;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.mockito.Mockito;
44  
45  /**
46   * Tests for {@link DefaultProxyRoutePlanner}.
47   */
48  public class TestDefaultProxyRoutePlanner {
49  
50      private HttpHost defaultProxy;
51      private SchemePortResolver schemePortResolver;
52      private DefaultProxyRoutePlanner routePlanner;
53  
54      @Before
55      public void setup() {
56          defaultProxy = new HttpHost("default.proxy.host", 8888);
57          schemePortResolver = Mockito.mock(SchemePortResolver.class);
58          routePlanner = new DefaultProxyRoutePlanner(defaultProxy,
59              schemePortResolver);
60      }
61  
62      @Test
63      public void testDefaultProxyDirect() throws Exception {
64          final HttpHost target = new HttpHost("somehost", 80, "http");
65          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
66  
67          final HttpContext context = new BasicHttpContext();
68          final HttpRoute route = routePlanner.determineRoute(target, request, context);
69  
70          Assert.assertEquals(target, route.getTargetHost());
71          Assert.assertEquals(defaultProxy, route.getProxyHost());
72          Assert.assertEquals(2, route.getHopCount());
73          Assert.assertFalse(route.isSecure());
74      }
75  
76      @Test
77      public void testViaProxy() throws Exception {
78          final HttpHost target = new HttpHost("somehost", 80, "http");
79          final HttpHost proxy = new HttpHost("custom.proxy.host", 8080);
80          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
81  
82          final HttpClientContext context = HttpClientContext.create();
83          context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build());
84          final HttpRoute route = routePlanner.determineRoute(target, request, context);
85  
86          Assert.assertEquals(target, route.getTargetHost());
87          Assert.assertEquals(proxy, route.getProxyHost());
88          Assert.assertEquals(2, route.getHopCount());
89          Assert.assertFalse(route.isSecure());
90      }
91  
92  }