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 static org.hamcrest.MatcherAssert.assertThat;
31  
32  import java.net.InetAddress;
33  import java.net.URI;
34  
35  import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
36  import org.apache.hc.client5.http.routing.RoutingSupport;
37  import org.apache.hc.core5.http.HttpHost;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.ProtocolException;
40  import org.apache.hc.core5.http.message.BasicHttpRequest;
41  import org.apache.hc.core5.net.URIAuthority;
42  import org.hamcrest.CoreMatchers;
43  import org.junit.jupiter.api.Assertions;
44  import org.junit.jupiter.api.Test;
45  
46  public class TestRoutingSupport {
47  
48      @Test
49      public void testDetermineHost() throws Exception {
50          final HttpRequest request1 = new BasicHttpRequest("GET", "/");
51          final HttpHost host1 = RoutingSupport.determineHost(request1);
52          assertThat(host1, CoreMatchers.nullValue());
53  
54          final HttpRequest request2 = new BasicHttpRequest("GET", new URI("https://somehost:8443/"));
55          final HttpHost host2 = RoutingSupport.determineHost(request2);
56          assertThat(host2, CoreMatchers.equalTo(new HttpHost("https", "somehost", 8443)));
57      }
58  
59      @Test
60      public void testDetermineHostMissingScheme() throws Exception {
61          final HttpRequest request1 = new BasicHttpRequest("GET", "/");
62          request1.setAuthority(new URIAuthority("host"));
63          Assertions.assertThrows(ProtocolException.class, () ->
64                  RoutingSupport.determineHost(request1));
65      }
66  
67      @Test
68      public void testNormalizeHost() throws Exception {
69          Assertions.assertEquals(new HttpHost("http", "somehost", 80),
70                  RoutingSupport.normalize(
71                          new HttpHost("http", "somehost", -1),
72                          DefaultSchemePortResolver.INSTANCE));
73          Assertions.assertEquals(new HttpHost("https", "somehost", 443),
74                  RoutingSupport.normalize(
75                          new HttpHost("https", "somehost", -1),
76                          DefaultSchemePortResolver.INSTANCE));
77          Assertions.assertEquals(new HttpHost("http", InetAddress.getLocalHost(), "localhost", 80),
78                  RoutingSupport.normalize(
79                          new HttpHost("http", InetAddress.getLocalHost(), "localhost", -1),
80                          DefaultSchemePortResolver.INSTANCE));
81          Assertions.assertEquals(new HttpHost("http", "somehost", 8080),
82                  RoutingSupport.normalize(
83                          new HttpHost("http", "somehost", 8080),
84                          DefaultSchemePortResolver.INSTANCE));
85      }
86  
87  }