Author: mgrigorov
Date: Mon Dec 12 14:05:36 2011
New Revision: 1213250

URL: http://svn.apache.org/viewvc?rev=1213250&view=rev
Log:
WICKET-4289
Improve WicketTester handling of cookies


Added:
    
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/CookiePage.java
Modified:
    
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
    
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java

Modified: 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=1213250&r1=1213249&r2=1213250&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 (original)
+++ 
wicket/branches/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 Mon Dec 12 14:05:36 2011
@@ -379,6 +379,8 @@ public class BaseWicketTester
                        request.setServerPort(lastRequest.getServerPort());
                }
 
+               transferCookies();
+
                response = new MockHttpServletResponse(request);
 
                ServletWebRequest servletWebRequest = newServletWebRequest();
@@ -394,6 +396,28 @@ public class BaseWicketTester
        }
 
        /**
+        * Copies all cookies with a positive age from the last response to the 
request that is going to
+        * be used for the next cycle.
+        */
+       private void transferCookies()
+       {
+               if (lastResponse != null)
+               {
+                       List<Cookie> cookies = lastResponse.getCookies();
+                       if (cookies != null)
+                       {
+                               for (Cookie cookie : cookies)
+                               {
+                                       if (cookie.getMaxAge() > 0)
+                                       {
+                                               request.addCookie(cookie);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
         * @param servletWebRequest
         * @return servlet web response
         */
@@ -749,7 +773,6 @@ public class BaseWicketTester
                componentInPage = null;
 
                // prepare request
-               request = new MockHttpServletRequest(application, httpSession, 
servletContext);
                request.setURL(request.getContextPath() + 
request.getServletPath() + "/");
                IRequestHandler handler = new 
RenderPageRequestHandler(pageProvider);
 
@@ -819,7 +842,6 @@ public class BaseWicketTester
                final PageParameters pageParameters)
        {
                // prepare request
-               request = new MockHttpServletRequest(application, httpSession, 
servletContext);
                request.setURL(request.getContextPath() + 
request.getServletPath() + "/");
                IRequestHandler handler = new 
ResourceReferenceRequestHandler(reference, pageParameters);
 

Added: 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/CookiePage.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/CookiePage.java?rev=1213250&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/CookiePage.java
 (added)
+++ 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/CookiePage.java
 Mon Dec 12 14:05:36 2011
@@ -0,0 +1,65 @@
+/*
+ * 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.util.tester;
+
+import junit.framework.Assert;
+
+import org.apache.wicket.util.cookies.CookieUtils;
+
+/**
+ * A test page for https://issues.apache.org/jira/browse/WICKET-4289
+ */
+public class CookiePage extends DummyHomePage
+{
+       private static final long serialVersionUID = 1L;
+
+       private final String cookieName;
+       private final String cookieValue;
+
+       /**
+        * Constructor.
+        * 
+        * @param name
+        *            the cookie name
+        * @param value
+        *            the cookie value
+        */
+       public CookiePage(String name, String value)
+       {
+               cookieName = name;
+               cookieValue = value;
+
+               doAssert();
+       }
+
+       private void doAssert()
+       {
+               CookieUtils utils = new CookieUtils();
+               String v = utils.load(cookieName);
+               Assert.assertEquals(cookieValue, v);
+       }
+
+       @Override
+       protected void onConfigure()
+       {
+               super.onConfigure();
+
+               doAssert();
+       }
+
+
+}

Modified: 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=1213250&r1=1213249&r2=1213250&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
 (original)
+++ 
wicket/branches/wicket-1.5.x/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
 Mon Dec 12 14:05:36 2011
@@ -1037,4 +1037,79 @@ public class WicketTesterTest extends Wi
                tester.startComponentInPage(new Label("testLabel"));
                tester.startPage(tester.getLastRenderedPage());
        }
+
+       /**
+        * Tests that setting a cookie with age > 0 before creating the page 
will survive after the
+        * rendering of the page and it will be used for the next request cycle.
+        */
+       @Test
+       public void transferCookies()
+       {
+               String cookieName = "wicket4289Name";
+               String cookieValue = "wicket4289Value";
+               int cookieAge = 1; // age > 0 => the cookie will be preserved 
for the the next request cycle
+
+               Cookie cookie = new Cookie(cookieName, cookieValue);
+               cookie.setMaxAge(cookieAge);
+               tester.getRequest().addCookie(cookie);
+
+               CookiePage page = new CookiePage(cookieName, cookieValue);
+
+               tester.startPage(page);
+
+               // assert that the cookie was in the response
+               List<Cookie> cookies = tester.getLastResponse().getCookies();
+               assertEquals(1, cookies.size());
+               Cookie cookie2 = cookies.get(0);
+               assertEquals(cookieName, cookie2.getName());
+               assertEquals(cookieValue, cookie2.getValue());
+               assertEquals(cookieAge, cookie2.getMaxAge());
+
+               // assert that the cookie will be preserved for the next request
+               assertEquals(cookieValue, 
tester.getRequest().getCookie(cookieName).getValue());
+       }
+
+       /**
+        * Tests that setting a cookie with age < 0 will not be stored after 
the request cycle.
+        */
+       @Test
+       public void dontTransferCookiesWithNegativeAge()
+       {
+               String cookieName = "wicket4289Name";
+               String cookieValue = "wicket4289Value";
+               int cookieAge = -1; // age < 0 => do not store it
+
+               Cookie cookie = new Cookie(cookieName, cookieValue);
+               cookie.setMaxAge(cookieAge);
+               tester.getRequest().addCookie(cookie);
+
+               CookiePage page = new CookiePage(cookieName, cookieValue);
+
+               tester.startPage(page);
+
+               // assert that the cookie is not preserved for the next request 
cycle
+               assertNull(tester.getRequest().getCookies());
+       }
+
+       /**
+        * Tests that setting a cookie with age < 0 will not be stored after 
the request cycle.
+        */
+       @Test
+       public void dontTransferCookiesWithZeroAge()
+       {
+               String cookieName = "wicket4289Name";
+               String cookieValue = "wicket4289Value";
+               int cookieAge = 0; // age == 0 => delete the cookie
+
+               Cookie cookie = new Cookie(cookieName, cookieValue);
+               cookie.setMaxAge(cookieAge);
+               tester.getRequest().addCookie(cookie);
+
+               CookiePage page = new CookiePage(cookieName, cookieValue);
+
+               tester.startPage(page);
+
+               // assert that the cookie is not preserved for the next request 
cycle
+               assertNull(tester.getRequest().getCookies());
+       }
 }


Reply via email to