View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.misc;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.chemistry.opencmis.client.bindings.spi.cookies.CmisCookieManager;
29  
30  public class CookiesTest extends TestCase {
31  
32      public void testCookies() {
33          CmisCookieManager manager = new CmisCookieManager();
34          String url;
35  
36          url = "https://www.example.com/s/test/abc?xyz";
37          addCookie(manager, url, "cookie1", "c1-1111", "/s/");
38  
39          url = "https://www.example.com/s/test/abc?xyz";
40          addCookie(manager, url, "cookie2", "c2-1111", "/s/");
41          deleteCookie(manager, url, "cookie2", "/s/");
42  
43          url = "https://www.example.com/s/test/abc";
44          addCookie(manager, url, "cookie1", "c1-2222", "/s/");
45  
46          url = "https://www.example.com/s/test/abc";
47          addCookie(manager, url, "cookie1", "c1-3333", "/s/t");
48  
49          url = "https://www.example.com/s/test/abc?abc";
50          addCookie(manager, url, "cookie1", "c1-4444", "/s/x");
51  
52          List<String> cookies = manager.get("https://www.example.com/s/test/abc/s", new HashMap<String, List<String>>())
53                  .get("Cookie");
54  
55          assertEquals(1, cookies.size());
56          assertEquals(cookies.get(0), "cookie1=c1-3333; cookie1=c1-2222");
57          //assertEquals(cookies.get(1), "cookie1=c1-2222");
58      }
59  
60      private void addCookie(CmisCookieManager manager, String url, String name, String value, String path) {
61          Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
62  
63          responseHeaders.put(null, Collections.singletonList("HTTP/1.1 200 OK"));
64          responseHeaders
65                  .put("Set-Cookie", Collections.singletonList(name + "=" + value + "; Path=" + path + "; Secure"));
66  
67          manager.put(url, responseHeaders);
68      }
69  
70      private void deleteCookie(CmisCookieManager manager, String url, String name, String path) {
71          Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
72  
73          responseHeaders.put(null, Collections.singletonList("HTTP/1.1 200 OK"));
74          responseHeaders.put("Set-Cookie",
75                  Collections.singletonList(name + "=delete; Path=" + path + "; Secure; Max-Age=0"));
76  
77          manager.put(url, responseHeaders);
78      }
79  }