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 java.net.InetAddress;
31  import java.net.InetSocketAddress;
32  import java.net.Proxy;
33  import java.net.ProxySelector;
34  import java.net.URI;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import org.apache.hc.client5.http.HttpRoute;
39  import org.apache.hc.client5.http.SchemePortResolver;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.protocol.BasicHttpContext;
42  import org.apache.hc.core5.http.protocol.HttpContext;
43  import org.junit.Assert;
44  import org.junit.Before;
45  import org.junit.Test;
46  import org.mockito.ArgumentMatchers;
47  import org.mockito.Mockito;
48  
49  /**
50   * Tests for {@link SystemDefaultRoutePlanner}.
51   */
52  @SuppressWarnings("boxing") // test code
53  public class TestSystemDefaultRoutePlanner {
54  
55      private SchemePortResolver schemePortResolver;
56      private ProxySelector proxySelector;
57      private SystemDefaultRoutePlanner routePlanner;
58  
59      @Before
60      public void setup() {
61          schemePortResolver = Mockito.mock(SchemePortResolver.class);
62          proxySelector = Mockito.mock(ProxySelector.class);
63          routePlanner = new SystemDefaultRoutePlanner(schemePortResolver, proxySelector);
64      }
65  
66      @Test
67      public void testDirect() throws Exception {
68          final HttpHost target = new HttpHost("http", "somehost", 80);
69  
70          final HttpContext context = new BasicHttpContext();
71          final HttpRoute route = routePlanner.determineRoute(target, context);
72  
73          Assert.assertEquals(target, route.getTargetHost());
74          Assert.assertEquals(1, route.getHopCount());
75          Assert.assertFalse(route.isSecure());
76          Mockito.verify(schemePortResolver, Mockito.never()).resolve(ArgumentMatchers.<HttpHost>any());
77      }
78  
79      @Test
80      public void testDirectDefaultPort() throws Exception {
81          final HttpHost target = new HttpHost("https", "somehost", -1);
82          Mockito.when(schemePortResolver.resolve(target)).thenReturn(443);
83  
84          final HttpContext context = new BasicHttpContext();
85          final HttpRoute route = routePlanner.determineRoute(target, context);
86  
87          Assert.assertEquals(new HttpHost("https", "somehost", 443), route.getTargetHost());
88          Assert.assertEquals(1, route.getHopCount());
89          Assert.assertTrue(route.isSecure());
90      }
91  
92      @Test
93      public void testProxy() throws Exception {
94  
95          final InetAddress ia = InetAddress.getByAddress(new byte[] {
96              (byte)127, (byte)0, (byte)0, (byte)1
97          });
98          final InetSocketAddress isa1 = new InetSocketAddress(ia, 11111);
99          final InetSocketAddress isa2 = new InetSocketAddress(ia, 22222);
100 
101         final List<Proxy> proxies = new ArrayList<>(2);
102         proxies.add(new Proxy(Proxy.Type.HTTP, isa1));
103         proxies.add(new Proxy(Proxy.Type.HTTP, isa2));
104 
105         Mockito.when(proxySelector.select(new URI("http://somehost:80"))).thenReturn(proxies);
106 
107         final HttpHost target = new HttpHost("http", "somehost", 80);
108 
109         final HttpContext context = new BasicHttpContext();
110         final HttpRoute route = routePlanner.determineRoute(target, context);
111 
112         Assert.assertEquals(target, route.getTargetHost());
113         Assert.assertEquals(2, route.getHopCount());
114         Assert.assertEquals(isa1.getPort(), route.getProxyHost().getPort());
115     }
116 
117 }