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