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.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 }