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.http.conn.util;
29  
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  
33  import org.apache.http.Consts;
34  import org.junit.Assert;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class TestPublicSuffixMatcher {
39  
40      private static final String SOURCE_FILE = "suffixlist.txt";
41  
42      private PublicSuffixMatcher matcher;
43  
44      @Before
45      public void setUp() throws Exception {
46          final ClassLoader classLoader = getClass().getClassLoader();
47          final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
48          Assert.assertNotNull(in);
49          final PublicSuffixList suffixList;
50          try {
51              final PublicSuffixListParser parser = new PublicSuffixListParser();
52              suffixList = parser.parse(new InputStreamReader(in, Consts.UTF_8));
53          } finally {
54              in.close();
55          }
56          matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
57      }
58  
59      @Test
60      public void testGetDomainRoot() throws Exception {
61          Assert.assertEquals("example.xx", matcher.getDomainRoot("example.XX"));
62          Assert.assertEquals("example.xx", matcher.getDomainRoot("www.example.XX"));
63          Assert.assertEquals("example.xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
64          Assert.assertEquals(null, matcher.getDomainRoot("xx"));
65          Assert.assertEquals(null, matcher.getDomainRoot("jp"));
66          Assert.assertEquals(null, matcher.getDomainRoot("example"));
67          Assert.assertEquals("example.example", matcher.getDomainRoot("example.example"));
68          Assert.assertEquals(null, matcher.getDomainRoot("ac.jp"));
69          Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp"));
70          Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp"));
71          Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp"));
72          Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp"));
73      }
74  
75      @Test
76      public void testMatch() throws Exception {
77          Assert.assertTrue(matcher.matches(".jp"));
78          Assert.assertTrue(matcher.matches(".ac.jp"));
79          Assert.assertTrue(matcher.matches(".any.tokyo.jp"));
80          // exception
81          Assert.assertFalse(matcher.matches(".metro.tokyo.jp"));
82      }
83  
84      @Test
85      public void testMatchUnicode() throws Exception {
86          Assert.assertTrue(matcher.matches(".h\u00E5.no")); // \u00E5 is <aring>
87          Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
88          Assert.assertTrue(matcher.matches(".h\u00E5.no"));
89          Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
90      }
91  
92  }