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