Home | History | Annotate | Download | only in net
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.apache.harmony.tests.java.net;
     18 
     19 import java.net.CookiePolicy;
     20 import java.net.HttpCookie;
     21 import java.net.URI;
     22 import java.net.URISyntaxException;
     23 
     24 import junit.framework.TestCase;
     25 
     26 public class CookiePolicyTest extends TestCase {
     27 
     28     /**
     29      * java.net.CookiePolicy#shouldAccept(java.net.URI,
     30      *java.net.HttpCookie).
     31      * @since 1.6
     32      */
     33     public void test_ShouldAccept_LURI_LHttpCookie() throws URISyntaxException {
     34         HttpCookie cookie = new HttpCookie("Harmony_6", "ongoing");
     35         URI uri = new URI("");
     36         boolean accept;
     37 
     38         // Policy: ACCEPT_ALL, always returns true
     39         accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie);
     40         assertTrue(accept);
     41 
     42         accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null);
     43         assertTrue(accept);
     44 
     45         accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null);
     46         assertTrue(accept);
     47 
     48         // Policy: ACCEPT_NONE, always returns false
     49         accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie);
     50         assertFalse(accept);
     51 
     52         accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null);
     53         assertFalse(accept);
     54 
     55         accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null);
     56         assertFalse(accept);
     57 
     58         // Policy: ACCEPT_ORIGINAL_SERVER
     59         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
     60         assertFalse(accept);
     61 
     62         cookie.setDomain(".b.c");
     63         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     64                 "schema://a.b.c"), cookie);
     65         assertTrue(accept);
     66 
     67         cookie.setDomain(".b.c");
     68         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     69                 "s://a.b.c.d"), cookie);
     70         assertFalse(accept);
     71 
     72         cookie.setDomain("b.c");
     73         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     74                 "s://a.b.c.d"), cookie);
     75         assertFalse(accept);
     76 
     77         cookie.setDomain("a.b.c.d");
     78         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     79                 "s://a.b.c.d"), cookie);
     80         assertTrue(accept);
     81 
     82         cookie.setDomain(".");
     83         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     84                 "s://a.b.c.d"), cookie);
     85         assertFalse(accept);
     86 
     87         cookie.setDomain("");
     88         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
     89                 "s://a.b.c.d"), cookie);
     90         assertFalse(accept);
     91 
     92         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie);
     93         assertFalse(accept);
     94 
     95         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null);
     96         assertFalse(accept);
     97 
     98         accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null);
     99         assertFalse(accept);
    100     }
    101 
    102 }
    103