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