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.Assert;
39  import org.junit.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          Assert.assertEquals("somehost", host1.getHostName());
51          Assert.assertEquals(-1, host1.getPort());
52          Assert.assertEquals("http", host1.getSchemeName());
53          final HttpHost host2 = new HttpHost("somehost", 8080);
54          Assert.assertEquals("somehost", host2.getHostName());
55          Assert.assertEquals(8080, host2.getPort());
56          Assert.assertEquals("http", host2.getSchemeName());
57          final HttpHost host3 = new HttpHost("somehost", -1);
58          Assert.assertEquals("somehost", host3.getHostName());
59          Assert.assertEquals(-1, host3.getPort());
60          Assert.assertEquals("http", host3.getSchemeName());
61          final HttpHost host4 = new HttpHost("https", "somehost", 443);
62          Assert.assertEquals("somehost", host4.getHostName());
63          Assert.assertEquals(443, host4.getPort());
64          Assert.assertEquals("https", host4.getSchemeName());
65          final HttpHost host5 = new HttpHost("https", "somehost");
66          Assert.assertEquals("somehost", host5.getHostName());
67          Assert.assertEquals(-1, host5.getPort());
68          Assert.assertEquals("https", host5.getSchemeName());
69          try {
70              new HttpHost(null, (String) null, -1);
71              Assert.fail("NullPointerException should have been thrown");
72          } catch (final NullPointerException expected) {
73          }
74          try {
75              new HttpHost(null, "   ", -1);
76              Assert.fail("IllegalArgumentException should have been thrown");
77          } catch (final IllegalArgumentException expected) {
78          }
79          try {
80              new HttpHost(null, (InetAddress) null, -1);
81              Assert.fail("NullPointerException should have been thrown");
82          } catch (final NullPointerException expected) {
83          }
84      }
85  
86      @Test
87      public void testHashCode() throws Exception {
88          final HttpHost host1 = new HttpHost("http", "somehost", 8080);
89          final HttpHost host2 = new HttpHost("http", "somehost", 80);
90          final HttpHost host3 = new HttpHost("http", "someotherhost", 8080);
91          final HttpHost host4 = new HttpHost("http", "somehost", 80);
92          final HttpHost host5 = new HttpHost("http", "SomeHost", 80);
93          final HttpHost host6 = new HttpHost("myhttp", "SomeHost", 80);
94          final HttpHost host7 = new HttpHost(
95                  "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
96          final HttpHost host8 = new HttpHost("http", "127.0.0.1", 80);
97          final HttpHost host9 = new HttpHost(
98                          "http", InetAddress.getByAddress("somehost",new byte[] {127,0,0,1}), 80);
99          final HttpHost host10 = new HttpHost(
100                         "http", InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80);
101         final HttpHost host11 = new HttpHost(
102                         "http", InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80);
103 
104         Assert.assertTrue(host1.hashCode() == host1.hashCode());
105         Assert.assertTrue(host1.hashCode() != host2.hashCode());
106         Assert.assertTrue(host1.hashCode() != host3.hashCode());
107         Assert.assertTrue(host2.hashCode() == host4.hashCode());
108         Assert.assertTrue(host2.hashCode() == host5.hashCode());
109         Assert.assertTrue(host5.hashCode() != host6.hashCode());
110         Assert.assertTrue(host7.hashCode() != host8.hashCode());
111         Assert.assertTrue(host8.hashCode() != host9.hashCode());
112         Assert.assertTrue(host9.hashCode() == host10.hashCode());
113         Assert.assertTrue(host10.hashCode() != host11.hashCode());
114         Assert.assertTrue(host9.hashCode() != host11.hashCode());
115     }
116 
117     @Test
118     public void testEquals() throws Exception {
119         final HttpHost host1 = new HttpHost("http", "somehost", 8080);
120         final HttpHost host2 = new HttpHost("http", "somehost", 80);
121         final HttpHost host3 = new HttpHost("http", "someotherhost", 8080);
122         final HttpHost host4 = new HttpHost("http", "somehost", 80);
123         final HttpHost host5 = new HttpHost("http", "SomeHost", 80);
124         final HttpHost host6 = new HttpHost("myhttp", "SomeHost", 80);
125         final HttpHost host7 = new HttpHost(
126                 "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
127         final HttpHost host8 = new HttpHost("http", "127.0.0.1", 80);
128         final HttpHost host9 = new HttpHost(
129                         "http", InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80);
130         final HttpHost host10 = new HttpHost(
131                         "http", InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80);
132         final HttpHost host11 = new HttpHost(
133                         "http", InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80);
134 
135         Assert.assertTrue(host1.equals(host1));
136         Assert.assertFalse(host1.equals(host2));
137         Assert.assertFalse(host1.equals(host3));
138         Assert.assertTrue(host2.equals(host4));
139         Assert.assertTrue(host2.equals(host5));
140         Assert.assertFalse(host5.equals(host6));
141         Assert.assertFalse(host7.equals(host8));
142         Assert.assertTrue(!host7.equals(host9));
143         Assert.assertFalse(host1.equals(null));
144         Assert.assertFalse(host1.equals("http://somehost"));
145         Assert.assertFalse(host9.equals("http://somehost"));
146         Assert.assertFalse(host8.equals(host9));
147         Assert.assertTrue(host9.equals(host10));
148         Assert.assertFalse(host9.equals(host11));
149     }
150 
151     @Test
152     public void testToString() throws Exception {
153         final HttpHost host1 = new HttpHost("somehost");
154         Assert.assertEquals("http://somehost", host1.toString());
155         final HttpHost host2 = new HttpHost("somehost", -1);
156         Assert.assertEquals("http://somehost", host2.toString());
157         final HttpHost host3 = new HttpHost("somehost", -1);
158         Assert.assertEquals("http://somehost", host3.toString());
159         final HttpHost host4 = new HttpHost("somehost", 8888);
160         Assert.assertEquals("http://somehost:8888", host4.toString());
161         final HttpHost host5 = new HttpHost("myhttp", "somehost", -1);
162         Assert.assertEquals("myhttp://somehost", host5.toString());
163         final HttpHost host6 = new HttpHost("myhttp", "somehost", 80);
164         Assert.assertEquals("myhttp://somehost:80", host6.toString());
165         final HttpHost host7 = new HttpHost(
166                 "http", InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80);
167         Assert.assertEquals("http://127.0.0.1:80", host7.toString());
168         final HttpHost host9 = new HttpHost(
169                         "http", InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80);
170         Assert.assertEquals("http://somehost:80", host9.toString());
171     }
172 
173     @Test
174     public void testToHostString() {
175         final HttpHost host1 = new HttpHost("somehost");
176         Assert.assertEquals("somehost", host1.toHostString());
177         final HttpHost host2 = new HttpHost("somehost");
178         Assert.assertEquals("somehost", host2.toHostString());
179         final HttpHost host3 = new HttpHost("somehost", -1);
180         Assert.assertEquals("somehost", host3.toHostString());
181         final HttpHost host4 = new HttpHost("somehost", 8888);
182         Assert.assertEquals("somehost:8888", host4.toHostString());
183     }
184 
185     @Test
186     public void testSerialization() throws Exception {
187         final HttpHost orig = new HttpHost("https", "somehost", 8080);
188         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
189         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
190         outStream.writeObject(orig);
191         outStream.close();
192         final byte[] raw = outbuffer.toByteArray();
193         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
194         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
195         final HttpHost clone = (HttpHost) inStream.readObject();
196         Assert.assertEquals(orig, clone);
197     }
198 
199     @Test
200     public void testCreateFromString() throws Exception {
201         Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("https://somehost:8080"));
202         Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create("HttpS://SomeHost:8080"));
203         Assert.assertEquals(new HttpHost(null, "somehost", 1234), HttpHost.create("somehost:1234"));
204         Assert.assertEquals(new HttpHost(null, "somehost", -1), HttpHost.create("somehost"));
205     }
206 
207     @Test
208     public void testCreateFromURI() throws Exception {
209         Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("https://somehost:8080")));
210         Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("HttpS://SomeHost:8080")));
211         Assert.assertEquals(new HttpHost("https", "somehost", 8080), HttpHost.create(URI.create("HttpS://SomeHost:8080/foo")));
212     }
213 
214     @Test
215     public void testCreateFromStringInvalid() throws Exception {
216         try {
217             HttpHost.create(" host ");
218             Assert.fail("URISyntaxException expected");
219         } catch (final URISyntaxException expected) {
220         }
221         try {
222             HttpHost.create("host :8080");
223             Assert.fail("URISyntaxException expected");
224         } catch (final URISyntaxException expected) {
225         }
226         try {
227             HttpHost.create("");
228             Assert.fail("IllegalArgumentException expected");
229         } catch (final IllegalArgumentException expected) {
230         }
231     }
232 
233     @Test
234     public void testIpv6HostAndPort() throws Exception {
235         final HttpHost host = HttpHost.create("[::1]:80");
236         Assert.assertEquals("http", host.getSchemeName());
237         Assert.assertEquals("::1", host.getHostName());
238         Assert.assertEquals(80, host.getPort());
239     }
240 
241     @Test
242     public void testIpv6HostAndPortWithScheme() throws Exception {
243         final HttpHost host = HttpHost.create("https://[::1]:80");
244         Assert.assertEquals("https", host.getSchemeName());
245         Assert.assertEquals("::1", host.getHostName());
246         Assert.assertEquals(80, host.getPort());
247     }
248 
249     @Test
250     public void testIpv6HostAndPortWithoutBrackets() throws Exception {
251         try {
252             // ambiguous
253             HttpHost.create("::1:80");
254             Assert.fail("URISyntaxException expected");
255         } catch (final URISyntaxException expected) {
256         }
257     }
258 
259     @Test
260     public void testIpv6HostWithoutPort() throws Exception {
261         try {
262             HttpHost.create("::1");
263             Assert.fail("URISyntaxException expected");
264         } catch (final URISyntaxException expected) {
265         }
266     }
267 
268     @Test
269     public void testIpv6HostToString() {
270         Assert.assertEquals("http://[::1]:80", new HttpHost("::1", 80).toString());
271         Assert.assertEquals("http://[::1]", new HttpHost("::1", -1).toString());
272     }
273 }