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
17package org.apache.harmony.tests.java.net;
18
19import java.net.CookiePolicy;
20import java.net.HttpCookie;
21import java.net.URI;
22import java.net.URISyntaxException;
23
24import junit.framework.TestCase;
25
26public 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        try {
37            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie);
38            fail("Should throw NullPointerException");
39        } catch (NullPointerException e) {
40            // expected
41        }
42
43        try {
44            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null);
45            fail("Should throw NullPointerException");
46        } catch (NullPointerException e) {
47            // expected
48        }
49
50        try {
51            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null);
52            fail("Should throw NullPointerException");
53        } catch (NullPointerException e) {
54            // expected
55        }
56
57        // Policy: ACCEPT_ALL, always returns true
58        boolean accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie);
59        assertTrue(accept);
60
61        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null);
62        assertTrue(accept);
63
64        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null);
65        assertTrue(accept);
66
67        // Policy: ACCEPT_NONE, always returns false
68        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie);
69        assertFalse(accept);
70
71        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null);
72        assertFalse(accept);
73
74        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null);
75        assertFalse(accept);
76
77        // Policy: ACCEPT_ORIGINAL_SERVER
78        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
79        assertFalse(accept);
80
81        cookie.setDomain(".b.c");
82        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
83                "schema://a.b.c"), cookie);
84        assertTrue(accept);
85
86        cookie.setDomain(".b.c");
87        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
88                "s://a.b.c.d"), cookie);
89        assertFalse(accept);
90
91        cookie.setDomain("b.c");
92        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
93                "s://a.b.c.d"), cookie);
94        assertFalse(accept);
95
96        cookie.setDomain("a.b.c.d");
97        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
98                "s://a.b.c.d"), cookie);
99        assertTrue(accept);
100
101        cookie.setDomain(".");
102        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
103                "s://a.b.c.d"), cookie);
104        assertFalse(accept);
105
106        cookie.setDomain("");
107        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
108                "s://a.b.c.d"), cookie);
109        assertFalse(accept);
110    }
111
112}
113