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.psl;
29  
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.nio.charset.StandardCharsets;
33  import java.util.Arrays;
34  import java.util.Collections;
35  import java.util.List;
36  
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  public class TestPublicSuffixListParser {
41  
42      @Test
43      public void testParse() throws Exception {
44          final ClassLoader classLoader = getClass().getClassLoader();
45          final InputStream in = classLoader.getResourceAsStream("suffixlist.txt");
46          Assert.assertNotNull(in);
47          final PublicSuffixList suffixList;
48          try {
49              final PublicSuffixListParser parser = new PublicSuffixListParser();
50              suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
51          } finally {
52              in.close();
53          }
54          Assert.assertNotNull(suffixList);
55          Assert.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
56          Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), suffixList.getExceptions());
57      }
58  
59      @Test
60      public void testParseByType() throws Exception {
61          final ClassLoader classLoader = getClass().getClassLoader();
62          final InputStream in = classLoader.getResourceAsStream("suffixlist2.txt");
63          Assert.assertNotNull(in);
64          final List<PublicSuffixList> suffixLists;
65          try {
66              final PublicSuffixListParser parser = new PublicSuffixListParser();
67              suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
68          } finally {
69              in.close();
70          }
71          Assert.assertNotNull(suffixLists);
72          Assert.assertEquals(2, suffixLists.size());
73          final PublicSuffixList publicSuffixList1 = suffixLists.get(0);
74          Assert.assertNotNull(publicSuffixList1);
75          Assert.assertEquals(DomainType.ICANN, publicSuffixList1.getType());
76          Assert.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp"), publicSuffixList1.getRules());
77          Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
78  
79          final PublicSuffixList publicSuffixList2 = suffixLists.get(1);
80          Assert.assertNotNull(publicSuffixList2);
81          Assert.assertEquals(DomainType.PRIVATE, publicSuffixList2.getType());
82          Assert.assertEquals(Arrays.asList("googleapis.com", "googlecode.com"), publicSuffixList2.getRules());
83          Assert.assertEquals(Collections.<String>emptyList(), publicSuffixList2.getExceptions());
84  
85      }
86  }