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.client5.http.impl;
29  
30  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  import java.net.InetAddress;
34  import java.net.UnknownHostException;
35  
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Tests {@link InMemoryDnsResolver}.
40   */
41  public class InMemoryDnsResolverTest {
42  
43      @Test
44      void resolve() throws UnknownHostException {
45          final InMemoryDnsResolver resolver = new InMemoryDnsResolver();
46          final String hostString = "127.0.0.1";
47          final byte[] address = new byte[] { 127, 0, 0, 1 };
48          resolver.add(hostString, InetAddress.getByAddress(hostString, address));
49          final InetAddress[] result1 = resolver.resolve(hostString);
50          assertEquals(1, result1.length);
51          assertArrayEquals(address, result1[0].getAddress());
52      }
53  
54      @Test
55      void resolveIPv6ZoneId() throws UnknownHostException {
56          final InMemoryDnsResolver resolver = new InMemoryDnsResolver();
57          final String hostStr = "[fe80::221:b7ff:fe8a:57d5%en4]";
58          final byte[] address = new byte[] { (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x21, (byte) 0xb7, (byte) 0xff, (byte) 0xfe, (byte) 0x8a, 0x57,
59                  (byte) 0xd5 };
60          resolver.add(hostStr, InetAddress.getByAddress(hostStr, address));
61          // This is an IPv6 address literal with zone ID
62          final InetAddress[] result = resolver.resolve(hostStr);
63          assertEquals(1, result.length);
64          assertArrayEquals(address, result[0].getAddress());
65  
66      }
67  
68  }