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.impl.cookie;
29  
30  import java.io.InputStreamReader;
31  import java.io.Reader;
32  
33  import org.apache.http.cookie.CookieOrigin;
34  import org.junit.Assert;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class TestPublicSuffixListParser {
39      private static final String LIST_FILE = "/suffixlist.txt";
40      private PublicSuffixFilter filter;
41  
42      @Before
43      public void setUp() throws Exception {
44          final Reader r = new InputStreamReader(getClass().getResourceAsStream(LIST_FILE), "UTF-8");
45          filter = new PublicSuffixFilter(new RFC2109DomainHandler());
46          final PublicSuffixListParser parser = new PublicSuffixListParser(filter);
47          parser.parse(r);
48      }
49  
50      @Test
51      public void testParse() throws Exception {
52          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
53  
54          cookie.setDomain(".jp");
55          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.jp", 80, "/stuff", false)));
56  
57          cookie.setDomain(".ac.jp");
58          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.ac.jp", 80, "/stuff", false)));
59  
60          cookie.setDomain(".any.tokyo.jp");
61          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.any.tokyo.jp", 80, "/stuff", false)));
62  
63          // exception
64          cookie.setDomain(".metro.tokyo.jp");
65          Assert.assertTrue(filter.match(cookie, new CookieOrigin("apache.metro.tokyo.jp", 80, "/stuff", false)));
66      }
67  
68      @Test
69      public void testUnicode() throws Exception {
70          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
71  
72          cookie.setDomain(".h\u00E5.no"); // \u00E5 is <aring>
73          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
74  
75          cookie.setDomain(".xn--h-2fa.no");
76          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
77  
78          cookie.setDomain(".h\u00E5.no");
79          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
80  
81          cookie.setDomain(".xn--h-2fa.no");
82          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
83      }
84  
85      @Test
86      public void testWhitespace() throws Exception {
87          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
88          cookie.setDomain(".xx");
89          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.xx", 80, "/stuff", false)));
90  
91          // yy appears after whitespace
92          cookie.setDomain(".yy");
93          Assert.assertTrue(filter.match(cookie, new CookieOrigin("apache.yy", 80, "/stuff", false)));
94  
95          // zz is commented
96          cookie.setDomain(".zz");
97          Assert.assertTrue(filter.match(cookie, new CookieOrigin("apache.zz", 80, "/stuff", false)));
98      }
99  
100 }