wicket-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thran...@apache.org
Subject svn commit: r722039 - in /wicket/trunk/wicket/src: main/java/org/apache/wicket/protocol/http/MockWebApplication.java test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Date Mon, 01 Dec 2008 11:26:01 GMT
Author: thrantal
Date: Mon Dec  1 03:25:55 2008
New Revision: 722039

URL: http://svn.apache.org/viewvc?rev=722039&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.

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
    wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java?rev=722039&r1=722038&r2=722039&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
Mon Dec  1 03:25:55 2008
@@ -16,16 +16,18 @@
  */
 package org.apache.wicket.protocol.http;
 
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
 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 javax.servlet.FilterConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletResponse;
+import java.util.Set;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
@@ -121,6 +123,7 @@
 	private final ServletContext context;
 
 	private final WicketFilter filter;
+	private Set<Cookie> cookiesOfThisSession = new HashSet<Cookie>();
 
 	/**
 	 * Create the mock http tester that can be used for testing.
@@ -195,7 +198,15 @@
 		servletSession = new MockHttpSession(context);
 		servletSession.setTemporary(initializeHttpSessionAsTemporary());
 		servletRequest = new MockHttpServletRequest(this.application, servletSession, context);
-		servletResponse = new MockHttpServletResponse(servletRequest);
+		servletResponse = new MockHttpServletResponse(servletRequest)
+		{
+			@Override
+			public void addCookie(Cookie cookie)
+			{
+				super.addCookie(cookie);
+				cookiesOfThisSession.add(cookie);
+			}
+		};
 
 		// Construct request and response using factories
 		wicketRequest = this.application.newWebRequest(servletRequest);
@@ -620,6 +631,7 @@
 	{
 		servletRequest.initialize();
 		servletResponse.initialize();
+		servletRequest.addCookies(cookiesOfThisSession);
 		servletRequest.setParameters(parametersForNextRequest);
 		if (isAjax)
 		{

Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=722039&r1=722038&r2=722039&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
(original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Mon Dec  1 03:25:55 2008
@@ -16,12 +16,16 @@
  */
 package org.apache.wicket.util.tester;
 
+import javax.servlet.http.Cookie;
+import java.util.Collection;
 import java.util.Locale;
 
 import junit.framework.TestCase;
-
-import org.apache.wicket.*;
-import org.apache.wicket.protocol.http.WebRequestCycle;
+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 +35,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;
@@ -604,6 +609,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<Cookie> cookies = tester.getServletResponse().getCookies();
+		assertEquals(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