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.core5.http;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.net.InetAddress;
35  import java.net.URI;
36  import java.net.URISyntaxException;
37  
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.Test;
40  
41  /**
42   * Unit tests for {@link HttpHost}.
43   *
44   */
45  public class TestHttpHost {
46  
47      @Test
48      public void testConstructor() {
49          final HttpHost host1 = new HttpHost("somehost");
50          Assertions.assertEquals("somehost", host1.getHostName());
51          Assertions.assertEquals(-1, host1.getPort());
52          Assertions.assertEquals("http", host1.getSchemeName());
53          final HttpHost host2 = new HttpHost("somehost", 8080);
54          Assertions.assertEquals("somehost", host2.getHostName());
55          Assertions.assertEquals(8080, host2.getPort());
56          Assertions.assertEquals("http", host2.getSchemeName());
57          final HttpHost host3 = new HttpHost("somehost", -1);
58          Assertions.assertEquals("somehost", host3.getHostName());
59          Assertions.assertEquals(-1, host3.getPort());
60          Assertions.assertEquals("http", host3.getSchemeName());
61          final HttpHost host4 = new HttpHost("https", "somehost", 443);
62          Assertions.assertEquals("somehost", host4.getHostName());
63          Assertions.assertEquals(443, host4.getPort());
64          Assertions.assertEquals("https", host4.getSchemeName());
65          final HttpHost host5 = new HttpHost("https", "somehost");
66          Assertions.assertEquals("somehost", host5.getHostName());
67          Assertions.assertEquals(-1, host5.getPort());
68          Assertions.assertEquals("https", host5.getSchemeName());
69          Assertions.assertThrows(NullPointerException.class, () -> new HttpHost(null, (String) null, -1));
70          Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpHost(null, "   ", -1));
71          Assertions.assertThrows(NullPointerException.class, () -> new HttpHost(null, (InetAddress) null, -1));
72      }
73  
74      @Test
75      public void testHashCode() throws Exception {
76          final HttpHost host1 = new HttpHost("http", "somehost", 8080);
77          final HttpHost host2 = new HttpHost("http", "somehost", 80);
78          final HttpHost host3 = new HttpHost("http", "someotherhost", 8080);
79          final HttpHost host4 = new HttpHost("http", "somehost", 80);
80          final HttpHost host5 = new HttpHost("http", "SomeHost", 80);
81          final HttpHost host6 = new HttpHost("myhttp", "SomeHost", 80);
82          final HttpHost host7 = new HttpHost(
83                  "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
84          final HttpHost host8 = new HttpHost("http", "127.0.0.1", 80);
85          final HttpHost host9 = new HttpHost(
86                          "http", InetAddress.getByAddress("somehost",new byte[] {127,0,0,1}), 80);
87          final HttpHost host10 = new HttpHost(
88                          "http", InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80);
89          final HttpHost host11 = new HttpHost(
90                          "http", InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80);
91  
92          Assertions.assertEquals(host1.hashCode(), host1.hashCode());
93          Assertions.assertTrue(host1.hashCode() != host2.hashCode());
94          Assertions.assertTrue(host1.hashCode() != host3.hashCode());
95          Assertions.assertEquals(host2.hashCode(), host4.hashCode());
96          Assertions.assertEquals(host2.hashCode(), host5.hashCode());
97          Assertions.assertTrue(host5.hashCode() != host6.hashCode());
98          Assertions.assertTrue(host7.hashCode() != host8.hashCode());
99          Assertions.assertTrue(host8.hashCode() != host9.hashCode());
100         Assertions.assertEquals(host9.hashCode(), host10.hashCode());
101         Assertions.assertTrue(host10.hashCode() != host11.hashCode());
102         Assertions.assertTrue(host9.hashCode() != host11.hashCode());
103     }
104 
105     @Test
106     public void testEquals() throws Exception {
107         final HttpHost host1 = new HttpHost("http", "somehost", 8080);
108         final HttpHost host2 = new HttpHost("http", "somehost", 80);
109         final HttpHost host3 = new HttpHost("http", "someotherhost", 8080);
110         final HttpHost host4 = new HttpHost("http", "somehost", 80);
111         final HttpHost host5 = new HttpHost("http", "SomeHost", 80);
112         final HttpHost host6 = new HttpHost("myhttp", "SomeHost", 80);
113         final HttpHost host7 = new HttpHost(
114                 "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
115         final HttpHost host8 = new HttpHost("http", "127.0.0.1", 80);
116         final HttpHost host9 = new HttpHost(
117                         "http", InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80);
118         final HttpHost host10 = new HttpHost(
119                         "http", InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80);
120         final HttpHost host11 = new HttpHost(
121                         "http", InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80);
122 
123         Assertions.assertEquals(host1, host1);
124         Assertions.assertNotEquals(host1, host2);
125         Assertions.assertNotEquals(host1, host3);
126         Assertions.assertEquals(host2, host4);
127         Assertions.assertEquals(host2, host5);
128         Assertions.assertNotEquals(host5, host6);
129         Assertions.assertNotEquals(host7, host8);
130         Assertions.assertFalse(host7.equals(host9));
131         Assertions.assertNotEquals(null, host1);
132         Assertions.assertNotEquals("http://somehost", host1);
133         Assertions.assertNotEquals("http://somehost", host9);
134         Assertions.assertNotEquals(host8, host9);
135         Assertions.assertEquals(host9, host10);
136         Assertions.assertNotEquals(host9, host11);
137     }
138 
139     @Test
140     public void testToString() throws Exception {
141         final HttpHost host1 = new HttpHost("somehost");
142         Assertions.assertEquals("http://somehost", host1.toString());
143         final HttpHost host2 = new HttpHost("somehost", -1);
144         Assertions.assertEquals("http://somehost", host2.toString());
145         final HttpHost host3 = new HttpHost("somehost", -1);
146         Assertions.assertEquals("http://somehost", host3.toString());
147         final HttpHost host4 = new HttpHost("somehost", 8888);
148         Assertions.assertEquals("http://somehost:8888", host4.toString());
149         final HttpHost host5 = new HttpHost("myhttp", "somehost", -1);
150         Assertions.assertEquals("myhttp://somehost", host5.toString());
151         final HttpHost host6 = new HttpHost("myhttp", "somehost", 80);
152         Assertions.assertEquals("myhttp://somehost:80", host6.toString());
153         final HttpHost host7 = new HttpHost(
154                 "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
155         Assertions.assertEquals("http://127.0.0.1:80", host7.toString());
156         final HttpHost host9 = new HttpHost(
157                         "http", InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80);
158         Assertions.assertEquals("http://somehost:80", host9.toString());
159     }
160 
161     @Test
162     public void testToHostString() {
163         final HttpHost host1 = new HttpHost("somehost");
164         Assertions.assertEquals("somehost", host1.toHostString());
165         final HttpHost host2 = new HttpHost("somehost");
166         Assertions.assertEquals("somehost", host2.toHostString());
167         final HttpHost host3 = new HttpHost("somehost", -1);
168         Assertions.assertEquals("somehost", host3.toHostString());
169         final HttpHost host4 = new HttpHost("somehost", 8888);
170         Assertions.assertEquals("somehost:8888", host4.toHostString());
171     }
172 
173     @Test
174     public void testSerialization() throws Exception {
175         final HttpHost orig = new HttpHost("https", "somehost", 8080);
176         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
177         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
178         outStream.writeObject(orig);
179         outStream.close();
180         final byte[] raw = outbuffer.toByteArray();
181         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
182         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
183         final HttpHost clone = (HttpHost) inStream.readObject();
184         Assertions.assertEquals(orig, clone);
185     }
186 
187     @Test
188     public void testCreateFromString() throws Exception {
189         Assertions.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("https://somehost:8080"));
190         Assertions.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("HttpS://SomeHost:8080"));
191         Assertions.assertEquals(new HttpHost(null, "somehost", 1234), HttpHost.create("somehost:1234"));
192         Assertions.assertEquals(new HttpHost(null, "somehost", -1), HttpHost.create("somehost"));
193     }
194 
195     @Test
196     public void testCreateFromURI() throws Exception {
197         Assertions.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("https://somehost:8080")));
198         Assertions.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("HttpS://SomeHost:8080")));
199         Assertions.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("HttpS://SomeHost:8080/foo")));
200     }
201 
202     @Test
203     public void testCreateFromStringInvalid() throws Exception {
204         Assertions.assertThrows(URISyntaxException.class, () -> HttpHost.create(" host "));
205         Assertions.assertThrows(URISyntaxException.class, () -> HttpHost.create("host :8080"));
206         Assertions.assertThrows(IllegalArgumentException.class, () -> HttpHost.create(""));
207     }
208 
209     @Test
210     public void testIpv6HostAndPort() throws Exception {
211         final HttpHost host = HttpHost.create("[::1]:80");
212         Assertions.assertEquals("http", host.getSchemeName());
213         Assertions.assertEquals("::1", host.getHostName());
214         Assertions.assertEquals(80, host.getPort());
215     }
216 
217     @Test
218     public void testIpv6HostAndPortWithScheme() throws Exception {
219         final HttpHost host = HttpHost.create("https://[::1]:80");
220         Assertions.assertEquals("https", host.getSchemeName());
221         Assertions.assertEquals("::1", host.getHostName());
222         Assertions.assertEquals(80, host.getPort());
223     }
224 
225     @Test
226     public void testIpv6HostAndPortWithoutBrackets() throws Exception {
227         Assertions.assertThrows(URISyntaxException.class, () -> HttpHost.create("::1:80"));
228     }
229 
230     @Test
231     public void testIpv6HostWithoutPort() throws Exception {
232         Assertions.assertThrows(URISyntaxException.class, () -> HttpHost.create("::1"));
233     }
234 
235     @Test
236     public void testIpv6HostToString() {
237         Assertions.assertEquals("http://[::1]:80", new HttpHost("::1", 80).toString());
238         Assertions.assertEquals("http://[::1]", new HttpHost("::1", -1).toString());
239     }
240 }