Author: mgrigorov
Date: Tue Dec 13 13:52:07 2011
New Revision: 1213694

URL: http://svn.apache.org/viewvc?rev=1213694&view=rev
Log:
WICKET-4292
MockHttpServletResponse.addCookie(Cookie) adds duplicate cookies

When adding a new cookie check the equality by name, path and domain


Added:
    
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponseTest.java
Modified:
    
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponse.java

Modified: 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponse.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponse.java?rev=1213694&r1=1213693&r2=1213694&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponse.java
 (original)
+++ 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponse.java
 Tue Dec 13 13:52:07 2011
@@ -27,6 +27,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.GregorianCalendar;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
@@ -101,7 +102,18 @@ public class MockHttpServletResponse imp
        public void addCookie(final Cookie cookie)
        {
                // remove any potential duplicates
-               cookies.remove(cookie);
+               // see http://www.ietf.org/rfc/rfc2109.txt, p.4.3.3
+               Iterator<Cookie> iterator = cookies.iterator();
+               while (iterator.hasNext())
+               {
+                       Cookie old = iterator.next();
+                       if (cookie.getName().equals(old.getName()) &&
+                               ((cookie.getPath() == null && old.getPath() == 
null) || (cookie.getPath().equals(old.getPath()))) &&
+                               ((cookie.getDomain() == null && old.getDomain() 
== null) || (cookie.getDomain().equals(old.getDomain()))))
+                       {
+                               iterator.remove();
+                       }
+               }
                cookies.add(cookie);
        }
 

Added: 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponseTest.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponseTest.java?rev=1213694&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponseTest.java
 (added)
+++ 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/protocol/http/mock/MockHttpServletResponseTest.java
 Tue Dec 13 13:52:07 2011
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.protocol.http.mock;
+
+import java.util.List;
+
+import javax.servlet.http.Cookie;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for {@link MockHttpServletResponse}
+ */
+public class MockHttpServletResponseTest extends Assert
+{
+       private MockHttpServletResponse response;
+
+       /**
+        * Prepare
+        */
+       @Before
+       public void before()
+       {
+               response = new MockHttpServletResponse(null);
+       }
+
+       /**
+        * Clean up
+        */
+       @After
+       public void after()
+       {
+               response = null;
+       }
+
+       /**
+        * Add a cookie
+        */
+       @Test
+       public void addCookie()
+       {
+               Cookie cookie = new Cookie("name", "value");
+               response.addCookie(cookie);
+
+               List<Cookie> cookies = response.getCookies();
+               assertEquals(1, cookies.size());
+               assertEquals("name", cookies.get(0).getName());
+               assertEquals("value", cookies.get(0).getValue());
+               assertNull(cookies.get(0).getComment());
+               assertNull(cookies.get(0).getDomain());
+               assertEquals(-1, cookies.get(0).getMaxAge());
+               assertNull(cookies.get(0).getPath());
+               assertEquals(false, cookies.get(0).getSecure());
+               assertEquals(0, cookies.get(0).getVersion());
+       }
+
+       /**
+        * Add a duplicate cookie. <br/>
+        * https://issues.apache.org/jira/browse/WICKET-4292
+        */
+       @Test
+       public void addDuplicateCookie()
+       {
+               Cookie cookie1 = new Cookie("name", "value");
+               response.addCookie(cookie1);
+               assertEquals(1, response.getCookies().size());
+
+               Cookie cookie2 = new Cookie("name", "value");
+               response.addCookie(cookie2);
+               assertEquals(1, response.getCookies().size());
+
+               Cookie cookie3 = new Cookie("name", "value");
+               cookie3.setPath("/");
+               response.addCookie(cookie3);
+               assertEquals(2, response.getCookies().size());
+
+               Cookie cookie4 = new Cookie("name", "value");
+               cookie4.setPath("/");
+               response.addCookie(cookie4);
+               assertEquals(2, response.getCookies().size());
+
+               Cookie cookie5 = new Cookie("name", "value");
+               cookie5.setPath("/");
+               cookie5.setDomain("example.com");
+               response.addCookie(cookie5);
+               assertEquals(3, response.getCookies().size());
+
+               Cookie cookie6 = new Cookie("name", "value");
+               cookie6.setPath("/");
+               cookie6.setDomain("example.com");
+               response.addCookie(cookie6);
+               assertEquals(3, response.getCookies().size());
+       }
+}


Reply via email to