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.net;
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.URISyntaxException;
35  
36  import org.hamcrest.CoreMatchers;
37  import org.hamcrest.MatcherAssert;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  /**
42   * Unit tests for {@link URIAuthority}.
43   *
44   */
45  public class TestURIAuthority {
46  
47      @Test
48      public void testConstructor() {
49          final URIAuthority host1 = new URIAuthority("somehost");
50          Assert.assertEquals("somehost", host1.getHostName());
51          Assert.assertEquals(-1, host1.getPort());
52          final URIAuthority host2 = new URIAuthority("somehost", 8080);
53          Assert.assertEquals("somehost", host2.getHostName());
54          Assert.assertEquals(8080, host2.getPort());
55          final URIAuthority host3 = new URIAuthority("somehost", -1);
56          Assert.assertEquals("somehost", host3.getHostName());
57          Assert.assertEquals(-1, host3.getPort());
58      }
59  
60      @Test
61      public void testHashCode() throws Exception {
62          final URIAuthority host1 = new URIAuthority("somehost", 8080);
63          final URIAuthority host2 = new URIAuthority("somehost", 80);
64          final URIAuthority host3 = new URIAuthority("someotherhost", 8080);
65          final URIAuthority host4 = new URIAuthority("somehost", 80);
66          final URIAuthority host5 = new URIAuthority("SomeHost", 80);
67          final URIAuthority host6 = new URIAuthority("user", "SomeHost", 80);
68          final URIAuthority host7 = new URIAuthority("user", "somehost", 80);
69  
70          Assert.assertTrue(host1.hashCode() == host1.hashCode());
71          Assert.assertTrue(host1.hashCode() != host2.hashCode());
72          Assert.assertTrue(host1.hashCode() != host3.hashCode());
73          Assert.assertTrue(host2.hashCode() == host4.hashCode());
74          Assert.assertTrue(host2.hashCode() == host5.hashCode());
75          Assert.assertTrue(host5.hashCode() != host6.hashCode());
76          Assert.assertTrue(host6.hashCode() == host7.hashCode());
77      }
78  
79      @Test
80      public void testEquals() throws Exception {
81          final URIAuthority host1 = new URIAuthority("somehost", 8080);
82          final URIAuthority host2 = new URIAuthority("somehost", 80);
83          final URIAuthority host3 = new URIAuthority("someotherhost", 8080);
84          final URIAuthority host4 = new URIAuthority("somehost", 80);
85          final URIAuthority host5 = new URIAuthority("SomeHost", 80);
86          final URIAuthority host6 = new URIAuthority("user", "SomeHost", 80);
87          final URIAuthority host7 = new URIAuthority("user", "somehost", 80);
88  
89          Assert.assertTrue(host1.equals(host1));
90          Assert.assertFalse(host1.equals(host2));
91          Assert.assertFalse(host1.equals(host3));
92          Assert.assertTrue(host2.equals(host4));
93          Assert.assertTrue(host2.equals(host5));
94          Assert.assertFalse(host5.equals(host6));
95          Assert.assertTrue(host6.equals(host7));
96      }
97  
98      @Test
99      public void testToString() throws Exception {
100         final URIAuthority host1 = new URIAuthority("somehost");
101         Assert.assertEquals("somehost", host1.toString());
102         final URIAuthority host2 = new URIAuthority("somehost", -1);
103         Assert.assertEquals("somehost", host2.toString());
104         final URIAuthority host3 = new URIAuthority("somehost", 8888);
105         Assert.assertEquals("somehost:8888", host3.toString());
106     }
107 
108     @Test
109     public void testSerialization() throws Exception {
110         final URIAuthority orig = new URIAuthority("somehost", 8080);
111         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
112         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
113         outStream.writeObject(orig);
114         outStream.close();
115         final byte[] raw = outbuffer.toByteArray();
116         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
117         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
118         final URIAuthority clone = (URIAuthority) inStream.readObject();
119         Assert.assertEquals(orig, clone);
120     }
121 
122     @Test
123     public void testParse() throws Exception {
124         MatcherAssert.assertThat(URIAuthority.parse("somehost"),
125                 CoreMatchers.equalTo(new URIAuthority("somehost", -1)));
126         MatcherAssert.assertThat(URIAuthority.parse("somehost/blah"),
127                 CoreMatchers.equalTo(new URIAuthority("somehost", -1)));
128         MatcherAssert.assertThat(URIAuthority.parse("somehost?blah"),
129                 CoreMatchers.equalTo(new URIAuthority("somehost", -1)));
130         MatcherAssert.assertThat(URIAuthority.parse("somehost#blah"),
131                 CoreMatchers.equalTo(new URIAuthority("somehost", -1)));
132         MatcherAssert.assertThat(URIAuthority.parse("aaaa@:8080"),
133                 CoreMatchers.equalTo(new URIAuthority("aaaa", "", 8080)));
134         MatcherAssert.assertThat(URIAuthority.parse("@:"),
135                 CoreMatchers.equalTo(new URIAuthority(null, "", -1)));
136         MatcherAssert.assertThat(URIAuthority.parse("somehost:8080"),
137                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
138         MatcherAssert.assertThat(URIAuthority.parse("somehost:8080/blah"),
139                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
140         MatcherAssert.assertThat(URIAuthority.parse("somehost:8080?blah"),
141                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
142         MatcherAssert.assertThat(URIAuthority.parse("somehost:8080#blah"),
143                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
144         MatcherAssert.assertThat(URIAuthority.parse("somehost:008080"),
145                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
146         try {
147             URIAuthority.create("somehost:aaaaa");
148             Assert.fail("URISyntaxException expected");
149         } catch (final URISyntaxException expected) {
150         }
151         try {
152             URIAuthority.create("somehost:90ab");
153             Assert.fail("URISyntaxException expected");
154         } catch (final URISyntaxException expected) {
155         }
156 
157         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost"),
158                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
159         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost/blah"),
160                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
161         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost?blah"),
162                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
163         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost#blah"),
164                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
165 
166         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:"),
167                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
168         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:/blah"),
169                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
170         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:?blah"),
171                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
172         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:#blah"),
173                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1)));
174 
175         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080"),
176                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080)));
177         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080/blah"),
178                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080)));
179         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080?blah"),
180                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080)));
181         MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080#blah"),
182                 CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080)));
183         MatcherAssert.assertThat(URIAuthority.parse("@somehost:8080"),
184                 CoreMatchers.equalTo(new URIAuthority("somehost", 8080)));
185         MatcherAssert.assertThat(URIAuthority.parse("test:test@localhost:38339"),
186                 CoreMatchers.equalTo(new URIAuthority("test:test", "localhost", 38339)));
187         try {
188             URIAuthority.create("blah@goggle.com:80@google.com/");
189             Assert.fail("URISyntaxException expected");
190         } catch (final URISyntaxException expected) {
191         }
192     }
193 
194     @Test
195     public void testCreateFromString() throws Exception {
196         Assert.assertEquals(new URIAuthority("somehost", 8080), URIAuthority.create("somehost:8080"));
197         Assert.assertEquals(new URIAuthority("SomeHost", 8080), URIAuthority.create("SomeHost:8080"));
198         Assert.assertEquals(new URIAuthority("somehost", 1234), URIAuthority.create("somehost:1234"));
199         Assert.assertEquals(new URIAuthority("somehost", -1), URIAuthority.create("somehost"));
200         Assert.assertEquals(new URIAuthority("user", "somehost", -1), URIAuthority.create("user@somehost"));
201         try {
202             URIAuthority.create(" host");
203             Assert.fail("URISyntaxException expected");
204         } catch (final URISyntaxException expected) {
205         }
206         try {
207             URIAuthority.create("host  ");
208             Assert.fail("URISyntaxException expected");
209         } catch (final URISyntaxException expected) {
210         }
211         try {
212             URIAuthority.create("host :8080");
213             Assert.fail("URISyntaxException expected");
214         } catch (final URISyntaxException expected) {
215         }
216         try {
217             URIAuthority.create("user @ host:8080");
218             Assert.fail("URISyntaxException expected");
219         } catch (final URISyntaxException expected) {
220         }
221     }
222 
223     @Test
224     public void testCreateFromIPv6String() throws Exception {
225         Assert.assertEquals(new URIAuthority("::1", 8080), URIAuthority.create("[::1]:8080"));
226         Assert.assertEquals(new URIAuthority("::1", -1), URIAuthority.create("[::1]"));
227         try {
228             URIAuthority.create("::1");
229             Assert.fail("URISyntaxException expected");
230         } catch (final URISyntaxException expected) {
231         }
232         try {
233             URIAuthority.create("[::1");
234             Assert.fail("URISyntaxException expected");
235         } catch (final URISyntaxException expected) {
236             Assert.assertTrue(expected.getMessage().contains("Expected an IPv6 closing bracket"));
237         }
238 
239         try {
240             URIAuthority.create("[a]:8080");
241             Assert.fail("URISyntaxException expected");
242         } catch (final URISyntaxException expected) {
243             Assert.assertTrue(expected.getMessage().contains("Expected an IPv6 address"));
244         }
245     }
246 
247     @Test
248     public void testIpv6HostToString() {
249         Assert.assertEquals("[::1]:80", new URIAuthority("::1", 80).toString());
250         Assert.assertEquals("user@[::1]:80", new URIAuthority("user", "::1", 80).toString());
251         Assert.assertEquals("[::1]", new URIAuthority("::1", -1).toString());
252         Assert.assertEquals("user@[::1]", new URIAuthority("user", "::1", -1).toString());
253     }
254 }