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 */ 16package org.apache.harmony.luni.tests.java.net; 17 18import java.io.IOException; 19import java.net.CookieManager; 20import java.net.CookiePolicy; 21import java.net.CookieStore; 22import java.net.HttpCookie; 23import java.net.URI; 24import java.net.URISyntaxException; 25import java.util.ArrayList; 26import java.util.HashMap; 27import java.util.List; 28import java.util.Map; 29import java.util.TreeMap; 30 31import junit.framework.TestCase; 32 33public class CookieManagerTest extends TestCase { 34 35 private static void checkValidParams4Get(URI uri, 36 Map<String, List<String>> map) throws IOException { 37 CookieManager manager = new CookieManager(); 38 try { 39 manager.get(uri, map); 40 fail("Should throw IllegalArgumentException"); 41 } catch (IllegalArgumentException e) { 42 // expected 43 } 44 45 } 46 47 private static void checkValidParams4Put(URI uri, 48 Map<String, List<String>> map) throws IOException { 49 CookieManager manager = new CookieManager(); 50 try { 51 manager.put(uri, map); 52 fail("Should throw IllegalArgumentException"); 53 } catch (IllegalArgumentException e) { 54 // expected 55 } 56 57 } 58 59 /** 60 * @tests {@link java.net.CookieManager#get(java.net.URI, java.util.Map)} & 61 * @tests {@link java.net.CookieManager#put(java.net.URI, java.util.Map)} 62 * IllegalArgumentException 63 * @since 1.6 64 */ 65 public void test_Put_Get_LURI_LMap_exception() throws IOException, 66 URISyntaxException { 67 // get 68 checkValidParams4Get(new URI(""), null); 69 checkValidParams4Get(new URI("http://www.test.com"), null); 70 checkValidParams4Get(null, null); 71 checkValidParams4Get(null, new HashMap<String, List<String>>()); 72 73 // put 74 checkValidParams4Put(new URI(""), null); 75 checkValidParams4Put(new URI("http://www.test.com"), null); 76 checkValidParams4Put(null, null); 77 checkValidParams4Put(null, new HashMap<String, List<String>>()); 78 } 79 80 private static Map<String, List<String>> addCookie(String[][] cookies) { 81 Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(); 82 for (int i = 0; i < cookies.length; i++) { 83 List<String> fields = new ArrayList<String>(); 84 for (int j = 1; j < cookies[i].length; j += 2) { 85 fields.add(cookies[i][j]); 86 } 87 responseHeaders.put(cookies[i][0], fields); 88 } 89 return responseHeaders; 90 } 91 92 private static CookieManager store(String[][] cookies, 93 Map<String, List<String>> responseHeaders, CookiePolicy policy) 94 throws IOException, URISyntaxException { 95 CookieManager manager = new CookieManager(null, policy); 96 // Put all cookies into manager 97 for (int i = 0; i < cookies.length; i++) { 98 for (int j = 2; j < cookies[i].length; j += 2) { 99 URI uri = new URI(cookies[i][j]); 100 manager.put(uri, responseHeaders); 101 } 102 } 103 return manager; 104 } 105 106 /** 107 * @tests {@link java.net.CookieManager#get(java.net.URI, java.util.Map)} & 108 * @tests {@link java.net.CookieManager#put(java.net.URI, java.util.Map)} 109 * 110 * @since 1.6 111 */ 112 public void test_Put_Get_LURI_LMap() throws IOException, URISyntaxException { 113 // cookie-key | (content, URI)... 114 String[][] cookies = { 115 { "Set-cookie", 116 "Set-cookie:PREF=test;path=/;domain=.b.c;", "http://a.b.c/", 117 "Set-cookie:PREF1=test2;path=/;domain=.beg.com;", "http://a.b.c/"}, 118 119 { "Set-cookie2", 120 "Set-cookie2:NAME1=VALUE1;path=/te;domain=.b.c;", "http://a.b.c/test"}, 121 122 { "Set-cookie", 123 "Set-cookie2:NAME=VALUE;path=/;domain=.beg.com;", "http://a.beg.com/test", 124 "Set-cookie2:NAME1=VALUE1;path=/;domain=.beg.com;", "http://a.beg.com/test"}, 125 126 { "Set-cookie2", 127 "Set-cookie3:NAME=VALUE;path=/;domain=.test.org;", "http://a.test.org/test"}, 128 129 { null, 130 "Set-cookie3:NAME=VALUE;path=/te;domain=.test.org;", "http://a.test.org/test"}, 131 132 { "Set-cookie2", 133 "lala", "http://a.test.org/test"} 134 135 }; 136 137 // requires path of cookie is the prefix of uri 138 // domain of cookie must match that of uri 139 Map<String, List<String>> responseHeaders = addCookie(new String[][] { 140 cookies[0], cookies[1] }); 141 CookieManager manager = store( 142 new String[][] { cookies[0], cookies[1] }, responseHeaders, 143 null); 144 145 HashMap<String, List<String>> dummyMap = new HashMap<String, List<String>>(); 146 Map<String, List<String>> map = manager.get(new URI("http://a.b.c/"), 147 dummyMap); 148 149 assertEquals(1, map.size()); 150 List<String> list = map.get("Cookie"); 151 assertEquals(1, list.size()); 152 153 // requires path of cookie is the prefix of uri 154 map = manager.get(new URI("http://a.b.c/te"), dummyMap); 155 list = map.get("Cookie"); 156 assertEquals(2, list.size()); 157 158 // If all cookies are of version 1, then $version=1 will be added 159 // ,no matter the value cookie-key 160 responseHeaders = addCookie(new String[][] { cookies[2] }); 161 manager = store(new String[][] { cookies[2] }, responseHeaders, null); 162 map = manager.get(new URI("http://a.beg.com/test"), dummyMap); 163 list = map.get("Cookie"); 164 assertEquals("$Version=\"1\"", list.get(0)); 165 assertEquals(3, list.size()); 166 167 // cookie-key will not have effect on determining cookie version 168 responseHeaders = addCookie(new String[][] { cookies[3] }); 169 manager = store(new String[][] { cookies[3] }, responseHeaders, null); 170 map = manager.get(new URI("http://a.test.org/"), responseHeaders); 171 list = map.get("Cookie"); 172 assertEquals(1, list.size()); 173 assertEquals("Set-cookie3:NAME=VALUE", list.get(0)); 174 175 // When key is null, no cookie can be stored/retrieved, even if policy = 176 // ACCEPT_ALL 177 responseHeaders = addCookie(new String[][] { cookies[4] }); 178 manager = store(new String[][] { cookies[4] }, responseHeaders, 179 CookiePolicy.ACCEPT_ALL); 180 map = manager.get(new URI("http://a.test.org/"), responseHeaders); 181 list = map.get("Cookie"); 182 assertEquals(0, list.size()); 183 184 // All cookies will be rejected if policy == ACCEPT_NONE 185 responseHeaders = addCookie(new String[][] { cookies[3] }); 186 manager = store(new String[][] { cookies[3] }, responseHeaders, 187 CookiePolicy.ACCEPT_NONE); 188 map = manager.get(new URI("http://a.test.org/"), responseHeaders); 189 list = map.get("Cookie"); 190 assertEquals(0, list.size()); 191 192 responseHeaders = addCookie(new String[][] { cookies[5] }); 193 manager = store(new String[][] { cookies[5] }, responseHeaders, 194 CookiePolicy.ACCEPT_ALL); 195 list = map.get("Cookie"); 196 assertEquals(0, list.size()); 197 198 try { 199 map.put(null, null); 200 fail("Should throw UnsupportedOperationException"); 201 } catch (UnsupportedOperationException e) { 202 // expected 203 } 204 205 } 206 207 /** 208 * @tests {@link java.net.CookieManager#CookieManager()} 209 * 210 * @since 1.6 211 */ 212 public void test_CookieManager() { 213 CookieManager cookieManager = new CookieManager(); 214 assertNotNull(cookieManager); 215 assertNotNull(cookieManager.getCookieStore()); 216 } 217 218 /** 219 * @tests {@link java.net.CookieManager#CookieManager(java.net.CookieStore, java.net.CookiePolicy)} 220 * 221 * @since 1.6 222 */ 223 public void testCookieManager_LCookieStore_LCookiePolicy() { 224 class DummyStore implements CookieStore { 225 public String getName() { 226 return "A dummy store"; 227 } 228 229 public void add(URI uri, HttpCookie cookie) { 230 // expected 231 } 232 233 public List<HttpCookie> get(URI uri) { 234 return null; 235 } 236 237 public List<HttpCookie> getCookies() { 238 return null; 239 } 240 241 public List<URI> getURIs() { 242 return null; 243 } 244 245 public boolean remove(URI uri, HttpCookie cookie) { 246 return false; 247 } 248 249 public boolean removeAll() { 250 return false; 251 } 252 } 253 CookieStore store = new DummyStore(); 254 CookieManager cookieManager = new CookieManager(store, 255 CookiePolicy.ACCEPT_ALL); 256 assertEquals("A dummy store", ((DummyStore) cookieManager 257 .getCookieStore()).getName()); 258 assertSame(store, cookieManager.getCookieStore()); 259 } 260 261 /** 262 * @tests {@link java.net.CookieManager#setCookiePolicy(java.net.CookiePolicy)} 263 * 264 * @since 1.6 265 */ 266 public void test_SetCookiePolicy_LCookiePolicy() throws URISyntaxException, 267 IOException { 268 269 // Policy = ACCEPT_NONE 270 CookieManager manager = new CookieManager(); 271 manager.setCookiePolicy(CookiePolicy.ACCEPT_NONE); 272 Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(); 273 URI uri = new URI("http://a.b.c"); 274 manager.put(uri, responseHeaders); 275 Map<String, List<String>> map = manager.get(uri, 276 new HashMap<String, List<String>>()); 277 278 assertEquals(1, map.size()); 279 assertTrue(map.get("Cookie").isEmpty()); 280 Object key = map.keySet().toArray()[0]; 281 assertNotNull(key); 282 assertEquals("Cookie", key); 283 284 // Policy = ACCEPT_ALL 285 manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 286 responseHeaders = new TreeMap<String, List<String>>(); 287 ArrayList<String> list = new ArrayList<String>(); 288 list.add("test=null"); 289 responseHeaders.put("Set-cookie", list); 290 manager.put(new URI("http://b.c.d"), responseHeaders); 291 map = manager.get(uri, new HashMap<String, List<String>>()); 292 assertEquals(1, map.size()); 293 } 294 295 /** 296 * @tests {@link java.net.CookieManager#getCookieStore()} 297 * 298 * @since 1.6 299 */ 300 public void test_GetCookieStore() { 301 CookieManager cookieManager = new CookieManager(); 302 CookieStore store = cookieManager.getCookieStore(); 303 assertNotNull(store); 304 } 305 306} 307