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  package org.apache.hc.client5.http.impl.classic;
28  
29  import org.apache.hc.client5.http.cookie.CookieIdentityComparator;
30  import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Simple tests for {@link CookieIdentityComparator}.
36   */
37  public class TestCookieIdentityComparator {
38  
39      @Test
40      public void testCookieIdentityComparasionByName() {
41          final CookieIdentityComparator comparator = new CookieIdentityComparator();
42          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
43          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
44          Assert.assertTrue(comparator.compare(c1, c2) == 0);
45  
46          final BasicClientCookie c3 = new BasicClientCookie("name1", "value");
47          final BasicClientCookie c4 = new BasicClientCookie("name2", "value");
48          Assert.assertFalse(comparator.compare(c3, c4) == 0);
49      }
50  
51      @Test
52      public void testCookieIdentityComparasionByNameAndDomain() {
53          final CookieIdentityComparator comparator = new CookieIdentityComparator();
54          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
55          c1.setDomain("www.domain.com");
56          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
57          c2.setDomain("www.domain.com");
58          Assert.assertTrue(comparator.compare(c1, c2) == 0);
59  
60          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
61          c3.setDomain("www.domain.com");
62          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
63          c4.setDomain("domain.com");
64          Assert.assertFalse(comparator.compare(c3, c4) == 0);
65      }
66  
67      @Test
68      public void testCookieIdentityComparasionByNameAndNullDomain() {
69          final CookieIdentityComparator comparator = new CookieIdentityComparator();
70          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
71          c1.setDomain(null);
72          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
73          c2.setDomain(null);
74          Assert.assertTrue(comparator.compare(c1, c2) == 0);
75  
76          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
77          c3.setDomain("www.domain.com");
78          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
79          c4.setDomain(null);
80          Assert.assertFalse(comparator.compare(c3, c4) == 0);
81      }
82  
83      @Test
84      public void testCookieIdentityComparasionByNameDomainAndPath() {
85          final CookieIdentityComparator comparator = new CookieIdentityComparator();
86          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
87          c1.setDomain("www.domain.com");
88          c1.setPath("/whatever");
89          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
90          c2.setDomain("www.domain.com");
91          c2.setPath("/whatever");
92          Assert.assertTrue(comparator.compare(c1, c2) == 0);
93  
94          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
95          c3.setDomain("www.domain.com");
96          c3.setPath("/whatever");
97          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
98          c4.setDomain("domain.com");
99          c4.setPath("/whatever-not");
100         Assert.assertFalse(comparator.compare(c3, c4) == 0);
101     }
102 
103     @Test
104     public void testCookieIdentityComparasionByNameDomainAndNullPath() {
105         final CookieIdentityComparator comparator = new CookieIdentityComparator();
106         final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
107         c1.setDomain("www.domain.com");
108         c1.setPath("/");
109         final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
110         c2.setDomain("www.domain.com");
111         c2.setPath(null);
112         Assert.assertTrue(comparator.compare(c1, c2) == 0);
113 
114         final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
115         c3.setDomain("www.domain.com");
116         c3.setPath("/whatever");
117         final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
118         c4.setDomain("domain.com");
119         c4.setPath(null);
120         Assert.assertFalse(comparator.compare(c3, c4) == 0);
121     }
122 
123 }