1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 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 Assertions.assertNotNull(in);
47 final PublicSuffixList suffixList;
48 try {
49 final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
50 suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
51 } finally {
52 in.close();
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 public void testParseByType() throws Exception {
61 final ClassLoader classLoader = getClass().getClassLoader();
62 final InputStream in = classLoader.getResourceAsStream("suffixlist2.txt");
63 Assertions.assertNotNull(in);
64 final List<PublicSuffixList> suffixLists;
65 try {
66 final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
67 suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
68 } finally {
69 in.close();
70 }
71 Assertions.assertNotNull(suffixLists);
72 Assertions.assertEquals(2, suffixLists.size());
73 final PublicSuffixList publicSuffixList1 = suffixLists.get(0);
74 Assertions.assertNotNull(publicSuffixList1);
75 Assertions.assertEquals(DomainType.ICANN, publicSuffixList1.getType());
76 Assertions.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp"), publicSuffixList1.getRules());
77 Assertions.assertEquals(Collections.singletonList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
78
79 final PublicSuffixList publicSuffixList2 = suffixLists.get(1);
80 Assertions.assertNotNull(publicSuffixList2);
81 Assertions.assertEquals(DomainType.PRIVATE, publicSuffixList2.getType());
82 Assertions.assertEquals(Arrays.asList("googleapis.com", "googlecode.com"), publicSuffixList2.getRules());
83 Assertions.assertEquals(Collections.<String>emptyList(), publicSuffixList2.getExceptions());
84
85 }
86 }