wicket-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thran...@apache.org
Subject svn commit: r722044 - in /wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src: main/java/org/apache/wicket/protocol/http/ test/java/org/apache/wicket/util/tester/
Date Mon, 01 Dec 2008 11:47:54 GMT
Author: thrantal
Date: Mon Dec  1 03:47:48 2008
New Revision: 722044

URL: http://svn.apache.org/viewvc?rev=722044&view=rev
Log:
WICKET-1886: Fixed testing things that require cookies persisted over multiple requests, such
as CSRF protection in a hidden Form fields.
An even more straight forward option would have been to remove clearing cookies in MockWebApplication.initialize(),
and copy cookies to each request from response, because after all the lifecycle of a WicketTester
(MockWebApplication) instance should be such that cookies could be preserved there.
Backport from trunk.

Modified:
    wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java
    wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
    wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java

Modified: wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java?rev=722044&r1=722043&r2=722044&view=diff
==============================================================================
--- wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java
(original)
+++ wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java
Mon Dec  1 03:47:48 2008
@@ -213,6 +213,16 @@
 		cookies.add(cookie);
 	}
 
+	public void addCookies(Iterable cookies)
+	{
+		for (Iterator it = cookies.iterator(); it.hasNext();)
+		{
+			Cookie cookie = (Cookie) it.next();
+			addCookie(cookie);
+		}
+	}
+
+
 	/**
 	 * Add an uploaded file to the request. Use this to simulate a file that has been uploaded
to a
 	 * field.

Modified: wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java?rev=722044&r1=722043&r2=722044&view=diff
==============================================================================
--- wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
(original)
+++ wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
Mon Dec  1 03:47:48 2008
@@ -19,13 +19,16 @@
 import java.io.File;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeSet;
 
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.wicket.Application;
@@ -122,6 +125,7 @@
 	private final ServletContext context;
 
 	private final WicketFilter filter;
+	private Set cookiesOfThisSession = new HashSet();
 
 	/**
 	 * Create the mock http tester that can be used for testing.
@@ -195,7 +199,14 @@
 		servletSession = new MockHttpSession(context);
 		servletSession.setTemporary(initializeHttpSessionAsTemporary());
 		servletRequest = new MockHttpServletRequest(this.application, servletSession, context);
-		servletResponse = new MockHttpServletResponse(servletRequest);
+		servletResponse = new MockHttpServletResponse(servletRequest)
+		{
+			public void addCookie(Cookie cookie)
+			{
+				super.addCookie(cookie);
+				cookiesOfThisSession.add(cookie);
+			}
+		};
 
 		// Construct request and response using factories
 		wicketRequest = this.application.newWebRequest(servletRequest);
@@ -619,6 +630,7 @@
 	{
 		servletRequest.initialize();
 		servletResponse.initialize();
+		servletRequest.addCookies(cookiesOfThisSession);
 		servletRequest.setParameters(parametersForNextRequest);
 		if (isAjax)
 		{

Modified: wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=722044&r1=722043&r2=722044&view=diff
==============================================================================
--- wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
(original)
+++ wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Mon Dec  1 03:47:48 2008
@@ -16,12 +16,17 @@
  */
 package org.apache.wicket.util.tester;
 
+import java.util.Collection;
 import java.util.Locale;
 
-import junit.framework.TestCase;
+import javax.servlet.http.Cookie;
 
-import org.apache.wicket.*;
-import org.apache.wicket.protocol.http.WebRequestCycle;
+import junit.framework.TestCase;
+import org.apache.wicket.Component;
+import org.apache.wicket.MockPageWithLink;
+import org.apache.wicket.MockPageWithOneComponent;
+import org.apache.wicket.Page;
+import org.apache.wicket.Session;
 import org.apache.wicket.ajax.AjaxEventBehavior;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.AjaxLink;
@@ -31,6 +36,7 @@
 import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.link.Link;
 import org.apache.wicket.model.IModel;
+import org.apache.wicket.protocol.http.WebRequestCycle;
 import org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy;
 import org.apache.wicket.util.tester.MockPageParameterPage.MockInnerClassPage;
 import org.apache.wicket.util.tester.MockPageWithFormAndAjaxFormSubmitBehavior.Pojo;
@@ -596,6 +602,26 @@
 		setTextFieldAndAssertSubmit(false);
 	}
 
+	public void testCookieIsFoundWhenAddedToServletRequest()
+	{
+		tester.getServletRequest().addCookie(new Cookie("name", "value"));
+		assertEquals("value", tester.getWicketRequest().getCookie("name").getValue());
+	}
+
+	public void testCookieIsFoundWhenAddedToServletResponse()
+	{
+		tester.getServletResponse().addCookie(new Cookie("name", "value"));
+		Collection cookies = tester.getServletResponse().getCookies();
+		assertEquals(((Cookie) cookies.iterator().next()).getValue(), "value");
+	}
+
+	public void testCookieIsFoundOnNextRequestWhenAddedToWicketResponse()
+	{
+		tester.getWicketResponse().addCookie(new Cookie("name", "value"));
+		tester.setupRequestAndResponse();
+		assertEquals("value", tester.getWicketRequest().getCookie("name").getValue());
+	}
+
 	private void setTextFieldAndAssertSubmit(boolean expected)
 	{
 		tester.executeAjaxEvent("form:text", "onkeyup");



Mime
View raw message