/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java

https://github.com/apache/httpcore · Java · 240 lines · 190 code · 20 blank · 30 comment · 7 complexity · f44dac88cdd8424cdbc8856a9d7e2b95 MD5 · raw file

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