WICKET-5147 cookie handling now with cookies visible in request

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/42b9d50b
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/42b9d50b
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/42b9d50b

Branch: refs/heads/master
Commit: 42b9d50bf3c92b37560f63f1dd52eac07f4838a4
Parents: 184e51e
Author: Michael Mosmann <mich...@mosmann.de>
Authored: Fri Apr 26 01:23:29 2013 +0200
Committer: Michael Mosmann <mich...@mosmann.de>
Committed: Fri Apr 26 01:23:29 2013 +0200

----------------------------------------------------------------------
 wicket-core/.settings/org.eclipse.jdt.ui.prefs     |    4 +-
 .../protocol/http/mock/CookieCollection.java       |  127 +++++++++++++++
 .../apache/wicket/protocol/http/mock/Cookies.java  |   99 +++++++++++-
 .../protocol/http/mock/MockHttpServletRequest.java |   16 +-
 .../wicket/util/tester/BaseWicketTester.java       |  102 ++++--------
 .../apache/wicket/util/tester/WicketTester.java    |    4 +-
 .../wicket/util/tester/WicketTesterTest.java       |   92 ++++++++----
 7 files changed, 331 insertions(+), 113 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/.settings/org.eclipse.jdt.ui.prefs
----------------------------------------------------------------------
diff --git a/wicket-core/.settings/org.eclipse.jdt.ui.prefs 
b/wicket-core/.settings/org.eclipse.jdt.ui.prefs
index 1f00276..108d88d 100644
--- a/wicket-core/.settings/org.eclipse.jdt.ui.prefs
+++ b/wicket-core/.settings/org.eclipse.jdt.ui.prefs
@@ -1,4 +1,3 @@
-#Fri Nov 05 09:40:42 PDT 2010
 comment_clear_blank_lines=false
 comment_format_comments=true
 comment_format_header=true
@@ -10,7 +9,7 @@ comment_line_length=80
 comment_new_line_for_parameter=true
 comment_separate_root_tags=true
 eclipse.preferences.version=1
-editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
+editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=false
 formatter_profile=_Wicket
 formatter_settings_version=11
 instance/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true
@@ -22,6 +21,7 @@ sp_cleanup.add_missing_deprecated_annotations=true
 sp_cleanup.add_missing_methods=false
 sp_cleanup.add_missing_nls_tags=false
 sp_cleanup.add_missing_override_annotations=true
+sp_cleanup.add_missing_override_annotations_interface_methods=false
 sp_cleanup.add_serial_version_id=false
 sp_cleanup.always_use_blocks=true
 sp_cleanup.always_use_parentheses_in_expressions=false

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/CookieCollection.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/CookieCollection.java
 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/CookieCollection.java
new file mode 100644
index 0000000..d316450
--- /dev/null
+++ 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/CookieCollection.java
@@ -0,0 +1,127 @@
+/*
+ * 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.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.Cookie;
+
+import org.apache.wicket.protocol.http.mock.Cookies.Key;
+
+/**
+ * cookie collection utility
+ * @author mosmann
+ */
+public class CookieCollection
+{
+       Map<Cookies.Key, Cookie> cookies = new LinkedHashMap<Cookies.Key, 
Cookie>();
+       Map<Cookies.Key, Cookie> expiredCookies = new 
LinkedHashMap<Cookies.Key, Cookie>();
+
+       /**
+        * add cookie to collection
+        *   if cookie is expired, it will be moved to expired cookie set
+        *   overwrite existing cookie with new value 
+        * @param cookie a cookie
+        */
+       public void add(Cookie cookie)
+       {
+               Key key = Cookies.keyOf(cookie);
+               Cookie copyOfCookie = Cookies.copyOf(cookie);
+               if (isExpired(cookie))
+               {
+                       expiredCookies.put(key, copyOfCookie);
+                       cookies.remove(key);
+               }
+               else
+               {
+                       cookies.put(key, copyOfCookie);
+               }
+       }
+
+       /**
+        * calls add on each cookie
+        * @param cookies array of cookies
+        */
+       public void addAll(Cookie[] cookies)
+       {
+               if (cookies != null)
+                       addAll(Arrays.asList(cookies));
+       }
+
+       /**
+        * calls add on each cookie
+        * @param cookies list of cookies
+        */
+       public void addAll(List<Cookie> cookies)
+       {
+               for (Cookie cookie : cookies)
+               {
+                       add(cookie);
+               }
+       }
+
+       /**
+        * list of non expired cookies
+        * @return as list
+        */
+       public List<Cookie> asList()
+       {
+               ArrayList<Cookie> ret = new ArrayList<Cookie>();
+               ret.addAll(cookies.values());
+               return ret;
+       }
+
+       /**
+        * list of expired cookies
+        * @return as list
+        */
+       public List<Cookie> expiredAsList()
+       {
+               ArrayList<Cookie> ret = new ArrayList<Cookie>();
+               ret.addAll(expiredCookies.values());
+               return ret;
+       }
+
+       /**
+        * list of all cookies, expired or not
+        * @return as list
+        */
+       public List<Cookie> allAsList()
+       {
+               ArrayList<Cookie> ret = new ArrayList<Cookie>();
+               ret.addAll(cookies.values());
+               ret.addAll(expiredCookies.values());
+               return ret;
+       }
+
+       /**
+        * detect if this cookie is expired
+        * 
+        * @param cookie
+        * @return true, if expired
+        */
+       public static boolean isExpired(Cookie cookie)
+       {
+               return cookie.getMaxAge() == 0;
+       }
+
+
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/Cookies.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/Cookies.java 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/Cookies.java
index d3a8a6e..f785ecb 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/Cookies.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/Cookies.java
@@ -16,8 +16,14 @@
  */
 package org.apache.wicket.protocol.http.mock;
 
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
 import javax.servlet.http.Cookie;
 
+import org.apache.wicket.protocol.http.mock.Cookies.Key;
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Objects;
 
@@ -45,6 +51,16 @@ public final class Cookies
        }
 
        /**
+        * creates a key based on the property for cookie equality
+        * @param cookie cookie
+        * @return key
+        */
+       public static Key keyOf(Cookie cookie)
+       {
+               return new Key(cookie);
+       }
+       
+       /**
         * Checks whether two cookies are equal.
         * See http://www.ietf.org/rfc/rfc2109.txt, p.4.3.3
         *
@@ -59,8 +75,85 @@ public final class Cookies
                Args.notNull(c1, "c1");
                Args.notNull(c2, "c2");
 
-               return c1.getName().equals(c2.getName()) &&
-                               Objects.isEqual(c1.getPath(), c2.getPath()) &&
-                               Objects.isEqual(c1.getDomain(), c2.getDomain());
+//             return c1.getName().equals(c2.getName()) &&
+//                             Objects.isEqual(c1.getPath(), c2.getPath()) &&
+//                             Objects.isEqual(c1.getDomain(), c2.getDomain());
+               return new Key(c1).equals(new Key(c2));
        }
+       
+       /**
+        * detect if this cookie is expired
+        * @param cookie
+        * @return
+        */
+       public static boolean isExpired(Cookie cookie)
+       {
+               return cookie.getMaxAge() == 0;
+       }
+       
+       public static class Key implements Serializable {
+               
+               private final String name;
+               private final String path;
+               private final String domain;
+
+               protected Key(Cookie cookie) {
+                       this.name=cookie.getName();
+                       this.path=cookie.getPath();
+                       this.domain=cookie.getDomain();
+               }
+
+               @Override
+               public int hashCode()
+               {
+                       final int prime = 31;
+                       int result = 1;
+                       result = prime * result + ((domain == null) ? 0 : 
domain.hashCode());
+                       result = prime * result + ((name == null) ? 0 : 
name.hashCode());
+                       result = prime * result + ((path == null) ? 0 : 
path.hashCode());
+                       return result;
+               }
+
+               @Override
+               public boolean equals(Object obj)
+               {
+                       if (this == obj)
+                               return true;
+                       if (obj == null)
+                               return false;
+                       if (getClass() != obj.getClass())
+                               return false;
+                       Key other = (Key)obj;
+                       if (domain == null)
+                       {
+                               if (other.domain != null)
+                                       return false;
+                       }
+                       else if (!domain.equals(other.domain))
+                               return false;
+                       if (name == null)
+                       {
+                               if (other.name != null)
+                                       return false;
+                       }
+                       else if (!name.equals(other.name))
+                               return false;
+                       if (path == null)
+                       {
+                               if (other.path != null)
+                                       return false;
+                       }
+                       else if (!path.equals(other.path))
+                               return false;
+                       return true;
+               }
+               
+               @Override
+               public String toString()
+               {
+                       return name+";"+domain+"/"+path;
+               }
+               
+       }
+
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
index 8c1a123..82c3923 100755
--- 
a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
@@ -30,6 +30,7 @@ import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
@@ -161,7 +162,7 @@ public class MockHttpServletRequest implements 
HttpServletRequest
 
        private final ServletContext context;
 
-       private final List<Cookie> cookies = new ArrayList<Cookie>();
+       private final Map<Cookies.Key,Cookie> cookies = new 
LinkedHashMap<Cookies.Key, Cookie>();
 
        private final ValueMap headers = new ValueMap();
 
@@ -217,7 +218,7 @@ public class MockHttpServletRequest implements 
HttpServletRequest
         */
        public void addCookie(final Cookie cookie)
        {
-               cookies.add(cookie);
+               cookies.put(Cookies.keyOf(cookie),cookie);
        }
 
        /**
@@ -478,16 +479,13 @@ public class MockHttpServletRequest implements 
HttpServletRequest
        @Override
        public Cookie[] getCookies()
        {
-               if (cookies.size() == 0)
+               if (cookies.isEmpty())
                {
                        return null;
                }
-               Cookie[] result = new Cookie[cookies.size()];
-               for (int i = 0; i < cookies.size(); i++)
-               {
-                       result[i] = Cookies.copyOf(cookies.get(i));
-               }
-               return result;
+               List<Cookie> cookieValues = new ArrayList<Cookie>();
+               cookieValues.addAll(cookies.values());
+               return cookieValues.toArray(new Cookie[cookieValues.size()]);
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
index 5c164e2..cc28aa5 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
@@ -98,6 +98,7 @@ import org.apache.wicket.page.IPageManagerContext;
 import org.apache.wicket.protocol.http.IMetaDataBufferingWebResponse;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.protocol.http.WicketFilter;
+import org.apache.wicket.protocol.http.mock.CookieCollection;
 import org.apache.wicket.protocol.http.mock.Cookies;
 import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
 import org.apache.wicket.protocol.http.mock.MockHttpServletResponse;
@@ -376,16 +377,44 @@ public class BaseWicketTester
                // Wicket tests assert that a cookie is in the response,
                // even after redirects (see 
org.apache.wicket.util.cookies.SetCookieAndRedirectTest.statefulPage())
                // They should assert that the cookie is in the next *request*
-               if (lastResponse != null && lastResponse.isRedirect())
+               if (lastResponse != null)
                {
                        List<Cookie> lastResponseCookies = 
lastResponse.getCookies();
-                       for (Cookie cookie : lastResponseCookies)
+                       if (lastResponse.isRedirect()) 
                        {
-                               if (cookie.getMaxAge() != 0)
+                               CookieCollection responseCookies=new 
CookieCollection();
+                               
+                               // if the last request is a redirect, all 
cookies from last response should appear in current reponse
+                               // this call will filter duplicates
+                               responseCookies.addAll(lastResponseCookies);
+                               for (Cookie cookie : 
responseCookies.allAsList())
                                {
-                                       // max-age==0 are already handled in 
#transferRequestCookies() above
                                        response.addCookie(cookie);
                                }
+                               
+                               // copy all request cookies from last request 
to the new request because of redirect handling
+                               // this way, the cookie will be send to the 
next requested page
+                               if (lastRequest!=null) {
+                                       CookieCollection requestCookies=new 
CookieCollection();
+                                       // this call will filter duplicates
+                                       
requestCookies.addAll(lastRequest.getCookies());
+                                       
request.addCookies(requestCookies.asList());
+                               }
+                       } 
+                       else
+                       {
+                               // if the last response is not a redirect 
+                               // - copy last request cookies to collection
+                               // - copy last response cookies to collection
+                               // - set only the not expired cookies to the 
next request
+                               CookieCollection cookies=new 
CookieCollection();                                
+                               if (lastRequest!=null) {
+                                       // this call will filter duplicates
+                                       
cookies.addAll(lastRequest.getCookies());
+                               }
+                               // this call will filter duplicates
+                               cookies.addAll(lastResponseCookies);
+                               request.addCookies(cookies.asList());
                        }
                }
 
@@ -433,69 +462,6 @@ public class BaseWicketTester
        }
 
        /**
-        * Simulates browser behavior by preserving all non-removed cookies from
-        * the previous request.
-        * A cookie is removed if the response contains a cookie with the same
-        * name, path and domain and max-age=0
-        */
-       private void transferRequestCookies()
-       {
-               List<Cookie> lastRequestCookies = new ArrayList<Cookie>();
-
-               // copy all cookies from the previous request
-               if (lastRequest != null && lastRequest.getCookies() != null)
-               {
-                       for (Cookie lastRequestCookie : 
lastRequest.getCookies())
-                       {
-                               lastRequestCookies.add(lastRequestCookie);
-                       }
-               }
-
-               // filter out all removed cookies
-               if (lastResponse != null)
-               {
-                       List<Cookie> cookies = lastResponse.getCookies();
-                       if (cookies != null)
-                       {
-                               for (Cookie cookie : cookies)
-                               {
-                                       // maxAge == -1 -> means session cookie
-                                       // maxAge == 0 -> delete the cookie
-                                       // maxAge > 0 -> the cookie will expire 
after this age
-                                       if (cookie.getMaxAge() == 0)
-                                       {
-                                               Iterator<Cookie> cookieIterator 
= lastRequestCookies.iterator();
-                                               while (cookieIterator.hasNext())
-                                               {
-                                                       Cookie 
lastRequestCookie = cookieIterator.next();
-                                                       if 
(Cookies.isEqual(lastRequestCookie, cookie))
-                                                       {
-                                                               
cookieIterator.remove();
-                                                       }
-                                               }
-                                       }
-                                       else
-                                       {
-                                               Iterator<Cookie> cookieIterator 
= lastRequestCookies.iterator();
-                                               while (cookieIterator.hasNext())
-                                               {
-                                                       Cookie oldCookie = 
cookieIterator.next();
-                                                       if 
(Cookies.isEqual(cookie, oldCookie))
-                                                       {
-                                                               
cookieIterator.remove();
-                                                       }
-                                               }
-                                               lastRequestCookies.add(cookie);
-                                       }
-                               }
-                       }
-               }
-
-               // transfer only the non-removed ones
-               request.addCookies(lastRequestCookies);
-       }
-
-       /**
         * @param servletWebRequest
         * @return servlet web response
         */
@@ -681,8 +647,6 @@ public class BaseWicketTester
 
                try
                {
-                       transferRequestCookies();
-
                        applyRequest();
                        requestCycle.scheduleRequestHandlerAfterCurrent(null);
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
index aaec246..3786693 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
@@ -153,8 +153,8 @@ import org.slf4j.LoggerFactory;
  *   after the request is made 
  * - all cookies set in the response should appear even after a redirect 
response is made
  *   until the final response (tester.getLastResponse()) is written to the 
client (wicket tester) 
- * - all valid cookies (maxAge!=0) from the last response should be added or 
should overwrite 
- *   the next request cookies (not visible in tester.getRequest().getCookies())
+ * - all valid cookies (maxAge!=0) from the last response should be added to
+ *   the next request cookies (tester.getRequest().getCookies())
  * 
  * 
  * TODO General: Example usage of FormTester

http://git-wip-us.apache.org/repos/asf/wicket/blob/42b9d50b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java 
b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
index 2087a3f..2e937f5 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.wicket.util.tester;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -83,6 +85,8 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
+import sun.security.provider.certpath.OCSPResponse.ResponseStatus;
+
 /**
  * 
  * @author Juergen Donnerstag
@@ -1059,7 +1063,7 @@ public class WicketTesterTest extends WicketTestCase
                assertEquals(cookieAge, cookie2.getMaxAge());
 
                // assert that the cookie will be preserved for the next request
-               assertEquals(cookieValue, 
tester.getLastRequest().getCookie(cookieName).getValue());
+               assertEquals(cookieValue, 
tester.getRequest().getCookie(cookieName).getValue());
        }
 
        /**
@@ -1347,7 +1351,7 @@ public class WicketTesterTest extends WicketTestCase
                // The cookie should be in each following request unless the 
server code
                // schedules it for removal it with cookie.setMaxAge(0)
                Assert.assertEquals("The cookie should be in each following 
request",
-                               1, tester.getLastRequest().getCookies().length);
+                               1, tester.getRequest().getCookies().length);
        }
 
        /**
@@ -1378,6 +1382,7 @@ public class WicketTesterTest extends WicketTestCase
        /**
         * @see WicketTester
         * 
+        * TODO add a cookie to request, which should override cookie from last 
response and last request
         * https://issues.apache.org/jira/browse/WICKET-5147
         */
        @Test
@@ -1387,41 +1392,48 @@ public class WicketTesterTest extends WicketTestCase
                Assert.assertTrue("no cookie in first 
request",collectingPage.getCookies().isEmpty());
                lastResponseDoesNotHaveAnyCookies();
                responseDoesNotHaveAnyCookies();
+               requestDoesNotHaveAnyCookies();
                
                // set cookie on request
-               Cookie cookieA = newCookie("a","1",1);
-               tester.getRequest().addCookie(cookieA);
+               Cookie firstCookie = newCookie("a","firstValue",1);
+               tester.getRequest().addCookie(firstCookie);
                collectingPage = collectAllRequestCookiesOnThisPage();
-               requestOnPageShouldHaveTheseCookies(collectingPage, cookieA);
+               requestOnPageShouldHaveTheseCookies(collectingPage, 
firstCookie);
                lastResponseDoesNotHaveAnyCookies();
+               requestShouldHaveTheseCookies(firstCookie);
                responseDoesNotHaveAnyCookies();
 
                // cookies from last request should appear on following requests
                collectingPage = collectAllRequestCookiesOnThisPage();
-               requestOnPageShouldHaveTheseCookies(collectingPage, cookieA);
+               requestOnPageShouldHaveTheseCookies(collectingPage, 
firstCookie);
                lastResponseDoesNotHaveAnyCookies();
+               requestShouldHaveTheseCookies(firstCookie);
                responseDoesNotHaveAnyCookies();
 
                // cookie will be overwritten if response will do so
-               Cookie newCookieA = newCookie("a","newValue",1);
-               setCookieInResponse(newCookieA);
-               lastResponseShouldHaveTheseCookies(newCookieA);
+               Cookie cookieSetInResponse = 
newCookie("a","overwriteWithNewValue",1);
+               setCookieInResponse(cookieSetInResponse);
+               lastResponseShouldHaveTheseCookies(cookieSetInResponse);
+               requestShouldHaveTheseCookies(cookieSetInResponse);
                
                // cookies from last response then should appear on following 
requests
                collectingPage = collectAllRequestCookiesOnThisPage();
-               requestOnPageShouldHaveTheseCookies(collectingPage, newCookieA);
+               requestOnPageShouldHaveTheseCookies(collectingPage, 
cookieSetInResponse);
                lastResponseDoesNotHaveAnyCookies();
+               requestShouldHaveTheseCookies(cookieSetInResponse);
                
                // cookies from requests will be deleted if the response will 
do so
-               Cookie removeCookieA = newCookie("a","removeMe",0);
-               setCookieInResponse(removeCookieA);
-               lastResponseShouldHaveTheseCookies(removeCookieA);
+               Cookie expiredCookieSetInResponse = newCookie("a","removeMe",0);
+               setCookieInResponse(expiredCookieSetInResponse);
+               lastResponseShouldHaveTheseCookies(expiredCookieSetInResponse);
                responseDoesNotHaveAnyCookies();
+               requestDoesNotHaveAnyCookies();
                
                // no cookies in next request while last cookie was deleted
                collectingPage = collectAllRequestCookiesOnThisPage();
                requestOnPageShouldHaveTheseCookies(collectingPage);
                lastResponseDoesNotHaveAnyCookies();
+               requestDoesNotHaveAnyCookies();
                responseDoesNotHaveAnyCookies();
        }
 
@@ -1433,41 +1445,47 @@ public class WicketTesterTest extends WicketTestCase
        @Test
        public void wicketTesterCookieHandlingWithRedirect() {
                // set cookie in response then redirect to other page
-               Cookie newCookieA = newCookie("a","newValue",1);
-               setCookieInResponseAndRedirect(newCookieA);
-               lastResponseShouldHaveTheseCookies(newCookieA);
+               Cookie firstCookie = newCookie("a","firstValue",1);
+               setCookieInResponseAndRedirect(firstCookie);
+               lastResponseShouldHaveTheseCookies(firstCookie);
+               requestShouldHaveTheseCookies(firstCookie);
 
                // cookie in response after redirect should appear in next 
request
                CollectAllRequestCookiesPage collectingPage = 
collectAllRequestCookiesOnThisPage();
-               requestOnPageShouldHaveTheseCookies(collectingPage,newCookieA);
+               requestOnPageShouldHaveTheseCookies(collectingPage,firstCookie);
                lastResponseDoesNotHaveAnyCookies();
+               requestShouldHaveTheseCookies(firstCookie);
                responseDoesNotHaveAnyCookies();
                
                // set cookie on request and overwrite in response then 
redirect to other page
-               Cookie cookieA = newCookie("a","1",1);
-               newCookieA = newCookie("a","newValue",1);
-               tester.getRequest().addCookie(cookieA);
-               setCookieInResponseAndRedirect(newCookieA);
-               lastResponseShouldHaveTheseCookies(newCookieA);
+               Cookie cookieSetInRequest = newCookie("a","valueFromRequest",1);
+               Cookie cookieSetInResponse = 
newCookie("a","overwriteInResponse",1);
+               tester.getRequest().addCookie(cookieSetInRequest);
+               setCookieInResponseAndRedirect(cookieSetInResponse);
+               lastResponseShouldHaveTheseCookies(cookieSetInResponse);
+               requestShouldHaveTheseCookies(cookieSetInResponse);
                
                // cookie in response after redirect should appear in next 
request
                collectingPage = collectAllRequestCookiesOnThisPage();
-               requestOnPageShouldHaveTheseCookies(collectingPage,newCookieA);
+               
requestOnPageShouldHaveTheseCookies(collectingPage,cookieSetInResponse);
                lastResponseDoesNotHaveAnyCookies();
+               requestShouldHaveTheseCookies(cookieSetInResponse);
                responseDoesNotHaveAnyCookies();
                
                // set cookie on request and remove it in response then 
redirect to other page
-               cookieA = newCookie("a","1",1);
-               newCookieA = newCookie("a","newValue",0);
-               tester.getRequest().addCookie(cookieA);
-               setCookieInResponseAndRedirect(newCookieA);
-               lastResponseDoesNotHaveAnyCookies();
+               Cookie nextCookieSetInRequest = 
newCookie("a","nextValueFromRequest",1);
+               Cookie nextCookieSetInResponse = newCookie("a","newValue",0);
+               tester.getRequest().addCookie(nextCookieSetInRequest);
+               setCookieInResponseAndRedirect(nextCookieSetInResponse);
+               lastResponseShouldHaveTheseCookies(nextCookieSetInResponse);
+               requestDoesNotHaveAnyCookies();
                responseDoesNotHaveAnyCookies();
                
                // no cookies left
                collectingPage = collectAllRequestCookiesOnThisPage();
                requestOnPageShouldHaveTheseCookies(collectingPage);
                lastResponseDoesNotHaveAnyCookies();
+               requestDoesNotHaveAnyCookies();
                responseDoesNotHaveAnyCookies();
        }
        
@@ -1521,6 +1539,16 @@ public class WicketTesterTest extends WicketTestCase
        }
 
        /**
+        * check cookies in current request
+        * @param page page
+        * @param cookies cookies
+        */
+       private void requestShouldHaveTheseCookies(Cookie...cookies) {
+               Cookie[] cookieFromRequest = tester.getRequest().getCookies();
+               listShouldMatchAll(cookieFromRequest!=null ? 
Arrays.asList(cookieFromRequest) : new ArrayList<Cookie>(), cookies);
+       }
+       
+       /**
         * check if every cookie is found in the list and no cookie is left 
         * @param cookieList cookie list
         * @param cookies cookies to check
@@ -1594,6 +1622,14 @@ public class WicketTesterTest extends WicketTestCase
        }
        
        /**
+        * request should not have any cookies
+        */
+       private void requestDoesNotHaveAnyCookies()
+       {
+               requestShouldHaveTheseCookies();
+       }
+       
+       /**
         * create a cookie map based on cookie name
         * @param cookies cookie list
         * @return as map

Reply via email to