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