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.ProtocolException;
37  import org.apache.hc.core5.http.message.BasicHttpRequest;
38  import org.apache.hc.core5.http.protocol.HttpContext;
39  import org.apache.hc.core5.net.URIAuthority;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.BeforeEach;
42  import org.junit.jupiter.api.Test;
43  import org.mockito.Mockito;
44  
45  /**
46   * Tests for {@link DefaultRoutePlanner}.
47   */
48  public class TestDefaultRoutePlanner {
49  
50      private SchemePortResolver schemePortResolver;
51      private DefaultRoutePlanner routePlanner;
52  
53      @BeforeEach
54      public void setup() {
55          schemePortResolver = Mockito.mock(SchemePortResolver.class);
56          routePlanner = new DefaultRoutePlanner(schemePortResolver);
57      }
58  
59      @Test
60      public void testDirect() throws Exception {
61          final HttpHost target = new HttpHost("http", "somehost", 80);
62  
63          final HttpContext context = HttpClientContext.create();
64          final HttpRoute route = routePlanner.determineRoute(target, context);
65  
66          Assertions.assertEquals(target, route.getTargetHost());
67          Assertions.assertEquals(1, route.getHopCount());
68          Assertions.assertFalse(route.isSecure());
69          Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.any());
70      }
71  
72      @Test
73      public void testDirectDefaultPort() throws Exception {
74          final HttpHost target = new HttpHost("https", "somehost", -1);
75          Mockito.when(schemePortResolver.resolve(target)).thenReturn(443);
76  
77          final HttpContext context = HttpClientContext.create();
78          final HttpRoute route = routePlanner.determineRoute(target, context);
79  
80          Assertions.assertEquals(new HttpHost("https", "somehost", 443), route.getTargetHost());
81          Assertions.assertEquals(1, route.getHopCount());
82          Assertions.assertTrue(route.isSecure());
83      }
84  
85      @Test
86      @SuppressWarnings("deprecation")
87      public void testViaProxy() throws Exception {
88          final HttpHost target = new HttpHost("http", "somehost", 80);
89          final HttpHost proxy = new HttpHost("proxy", 8080);
90  
91          final HttpClientContext context = HttpClientContext.create();
92          context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build());
93          final HttpRoute route = routePlanner.determineRoute(target, context);
94  
95          Assertions.assertEquals(target, route.getTargetHost());
96          Assertions.assertEquals(proxy, route.getProxyHost());
97          Assertions.assertEquals(2, route.getHopCount());
98          Assertions.assertFalse(route.isSecure());
99          Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.any());
100     }
101 
102     @Test
103     public void testNullTarget() throws Exception {
104         final HttpContext context = HttpClientContext.create();
105         Assertions.assertThrows(ProtocolException.class, () ->
106                 routePlanner.determineRoute(null, context));
107     }
108 
109     @Test
110     public void testVirtualSecureHost() throws Exception {
111         final HttpHost target = new HttpHost("https", "somehost", 443);
112         final URIAuthority virtualHost = new URIAuthority("someotherhost", 443);
113         final HttpRequest request = new BasicHttpRequest("https", "GET", virtualHost, "/");
114 
115         final HttpContext context = HttpClientContext.create();
116         final HttpRoute route = routePlanner.determineRoute(target, request, context);
117 
118         Assertions.assertEquals(target, route.getTargetHost());
119         Assertions.assertEquals(virtualHost, route.getTargetName());
120         Assertions.assertEquals(1, route.getHopCount());
121         Assertions.assertTrue(route.isSecure());
122         Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.any());
123     }
124 
125     @Test
126     public void testVirtualInsecureHost() throws Exception {
127         final HttpHost target = new HttpHost("http", "somehost", 80);
128         final URIAuthority virtualHost = new URIAuthority("someotherhost", 80);
129         final HttpRequest request = new BasicHttpRequest("http", "GET", virtualHost, "/");
130 
131         final HttpContext context = HttpClientContext.create();
132         final HttpRoute route = routePlanner.determineRoute(target, request, context);
133 
134         Assertions.assertEquals(target, route.getTargetHost());
135         Assertions.assertNull(route.getTargetName());
136         Assertions.assertEquals(1, route.getHopCount());
137         Assertions.assertFalse(route.isSecure());
138         Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.any());
139     }
140 }