You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by cm...@apache.org on 2012/05/14 17:55:58 UTC

git commit: WICKET-4558 WicketTester shouldn't fail on external redirects

Updated Branches:
  refs/heads/wicket-1.5.x 8b32c3d62 -> 0c7903420


WICKET-4558 WicketTester shouldn't fail on external redirects


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

Branch: refs/heads/wicket-1.5.x
Commit: 0c790342081abedc3677c3a5bd258664ebb96f88
Parents: 8b32c3d
Author: Carl-Eric Menzel <cm...@wicketbuch.de>
Authored: Mon May 14 17:54:20 2012 +0200
Committer: Carl-Eric Menzel <cm...@wicketbuch.de>
Committed: Mon May 14 17:54:20 2012 +0200

----------------------------------------------------------------------
 .../wicket/util/tester/BaseWicketTester.java       |   84 +++++++++----
 .../apache/wicket/util/tester/WicketTester.java    |   17 ++-
 .../wicket/util/tester/WicketTesterTest.java       |   96 ++++++++++++---
 3 files changed, 152 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/0c790342/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 3ee10e2..2ab8058 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
@@ -16,8 +16,7 @@
  */
 package org.apache.wicket.util.tester;
 
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.fail;
+import static junit.framework.Assert.*;
 
 import java.io.IOException;
 import java.io.Serializable;
@@ -33,7 +32,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import javax.servlet.FilterConfig;
@@ -662,6 +660,14 @@ public class BaseWicketTester
 				Url newUrl = Url.parse(lastResponse.getRedirectLocation(),
 					Charset.forName(request.getCharacterEncoding()));
 
+				if (isExternalRedirect(lastRequest.getUrl(), newUrl))
+				{
+					// we can't handle external redirects here
+					// just bail out here and let the user's test code
+					// check #assertRedirectUrl
+					return true;
+				}
+
 				if (newUrl.isAbsolute())
 				{
 					request.setUrl(newUrl);
@@ -708,6 +714,35 @@ public class BaseWicketTester
 	}
 
 	/**
+	 * Determine whether a given response contains a redirect leading to an external site (which
+	 * cannot be replicated in WicketTester). This is done by comparing the previous request's
+	 * hostname with the hostname given in the redirect.
+	 * 
+	 * @param requestUrl
+	 *            request...
+	 * @param newUrl
+	 *            ...and the redirect generated in its response
+	 * @return true if there is a redirect and it is external, false otherwise
+	 */
+	private boolean isExternalRedirect(Url requestUrl, Url newUrl)
+	{
+		String originalHost = requestUrl.getHost();
+		String redirectHost = newUrl.getHost();
+		if (originalHost == redirectHost)
+		{
+			return false; // identical or both null
+		}
+		else if (redirectHost == null)
+		{
+			return false; // no new host
+		}
+		else
+		{
+			return !(redirectHost.equals(originalHost));
+		}
+	}
+
+	/**
 	 * Allows to set Request header value any time. They'll be applied (add/modify) on process
 	 * execution {@link #processRequest(MockHttpServletRequest, IRequestHandler, boolean)}. They are
 	 * reset immediately after and thus are not re-used for a sequence of requests.
@@ -1266,10 +1301,10 @@ public class BaseWicketTester
 	 * Process a component. A web page will be automatically created with the markup created in
 	 * {@link #createPageMarkup(String)}.
 	 * <p>
-	 *     <strong>Note</strong>: the instantiated component will have an auto-generated id. To
-	 *     reach any of its children use their relative path to the component itself. For example
-	 *     if the started component has a child a Link component with id "link" then after starting
-	 *     the component you can click it with: <code>tester.clickLink("link")</code>
+	 * <strong>Note</strong>: the instantiated component will have an auto-generated id. To reach
+	 * any of its children use their relative path to the component itself. For example if the
+	 * started component has a child a Link component with id "link" then after starting the
+	 * component you can click it with: <code>tester.clickLink("link")</code>
 	 * </p>
 	 * 
 	 * @param <C>
@@ -1289,10 +1324,10 @@ public class BaseWicketTester
 	 * provided. In case pageMarkup is null, the markup will be automatically created with
 	 * {@link #createPageMarkup(String)}.
 	 * <p>
-	 *     <strong>Note</strong>: the instantiated component will have an auto-generated id. To
-	 *     reach any of its children use their relative path to the component itself. For example
-	 *     if the started component has a child a Link component with id "link" then after starting
-	 *     the component you can click it with: <code>tester.clickLink("link")</code>
+	 * <strong>Note</strong>: the instantiated component will have an auto-generated id. To reach
+	 * any of its children use their relative path to the component itself. For example if the
+	 * started component has a child a Link component with id "link" then after starting the
+	 * component you can click it with: <code>tester.clickLink("link")</code>
 	 * </p>
 	 * 
 	 * @param <C>
@@ -1322,7 +1357,8 @@ public class BaseWicketTester
 		catch (Exception e)
 		{
 			log.error(e.getMessage(), e);
-			fail(String.format("Cannot instantiate component with type '%s' because of '%s'", componentClass.getName(), e.getMessage()));
+			fail(String.format("Cannot instantiate component with type '%s' because of '%s'",
+				componentClass.getName(), e.getMessage()));
 		}
 
 		// process the component
@@ -1333,10 +1369,10 @@ public class BaseWicketTester
 	 * Process a component. A web page will be automatically created with markup created by the
 	 * {@link #createPageMarkup(String)}.
 	 * <p>
-	 *     <strong>Note</strong>: the component id is set by the user. To
-	 *     reach any of its children use this id + their relative path to the component itself. For example
-	 *     if the started component has id <em>compId</em> and a Link child component component with id "link"
-	 *     then after starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
+	 * <strong>Note</strong>: the component id is set by the user. To reach any of its children use
+	 * this id + their relative path to the component itself. For example if the started component
+	 * has id <em>compId</em> and a Link child component component with id "link" then after
+	 * starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
 	 * </p>
 	 * 
 	 * @param <C>
@@ -1356,10 +1392,10 @@ public class BaseWicketTester
 	 * provided. In case {@code pageMarkup} is null, the markup will be automatically created with
 	 * {@link #createPageMarkup(String)}.
 	 * <p>
-	 *     <strong>Note</strong>: the component id is set by the user. To
-	 *     reach any of its children use this id + their relative path to the component itself. For example
-	 *     if the started component has id <em>compId</em> and a Link child component component with id "link"
-	 *     then after starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
+	 * <strong>Note</strong>: the component id is set by the user. To reach any of its children use
+	 * this id + their relative path to the component itself. For example if the started component
+	 * has id <em>compId</em> and a Link child component component with id "link" then after
+	 * starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
 	 * </p>
 	 * 
 	 * @param <C>
@@ -1546,7 +1582,7 @@ public class BaseWicketTester
 			String componentIdPageId = componentInPage.component.getId() + ':';
 			if (path.startsWith(componentIdPageId) == false)
 			{
-				path =  componentIdPageId + path;
+				path = componentIdPageId + path;
 			}
 		}
 
@@ -2129,8 +2165,8 @@ public class BaseWicketTester
 
 	/**
 	 * Tests that a <code>Component</code> has been added to a <code>AjaxRequestTarget</code>, using
-	 * {@link AjaxRequestTarget#add(org.apache.wicket.Component...)}. This method actually tests that a
-	 * <code>Component</code> is on the Ajax response sent back to the client.
+	 * {@link AjaxRequestTarget#add(org.apache.wicket.Component...)}. This method actually tests
+	 * that a <code>Component</code> is on the Ajax response sent back to the client.
 	 * <p>
 	 * PLEASE NOTE! This method doesn't actually insert the <code>Component</code> in the client DOM
 	 * tree, using JavaScript. But it shouldn't be needed because you have to trust that the Wicket
@@ -2215,7 +2251,7 @@ public class BaseWicketTester
 	 * Simulates the firing of all ajax timer behaviors on the page
 	 * 
 	 * @param page
-	 *      the page which timers will be executed
+	 *            the page which timers will be executed
 	 */
 	public void executeAllTimerBehaviors(final MarkupContainer page)
 	{

http://git-wip-us.apache.org/repos/asf/wicket/blob/0c790342/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 01c70b8..c4098de 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
@@ -16,10 +16,7 @@
  */
 package org.apache.wicket.util.tester;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
+import static junit.framework.Assert.*;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -733,6 +730,18 @@ public class WicketTester extends BaseWicketTester
 	}
 
 	/**
+	 * Assert that the last request redirected to the given Url.
+	 * 
+	 * @param expectedRedirectUrl
+	 *            expected
+	 */
+	public void assertRedirectUrl(String expectedRedirectUrl)
+	{
+		String actualRedirectUrl = getLastResponse().getRedirectLocation();
+		assertEquals(expectedRedirectUrl, actualRedirectUrl);
+	}
+
+	/**
 	 * Returns the current Maven build directory taken from the <tt>basedir</tt> system property, or
 	 * null if not set
 	 * 

http://git-wip-us.apache.org/repos/asf/wicket/blob/0c790342/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 729ed9f..f3657e0 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
@@ -53,6 +53,7 @@ import org.apache.wicket.request.IRequestHandler;
 import org.apache.wicket.request.IRequestParameters;
 import org.apache.wicket.request.Url;
 import org.apache.wicket.request.component.IRequestableComponent;
+import org.apache.wicket.request.flow.RedirectToUrlException;
 import org.apache.wicket.request.handler.BookmarkablePageRequestHandler;
 import org.apache.wicket.request.handler.IPageProvider;
 import org.apache.wicket.request.handler.PageProvider;
@@ -450,7 +451,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void assertComponentOnAjaxResponse()
@@ -684,7 +685,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void submittingFormWithAjaxEventSubmitsFormValues()
@@ -701,7 +702,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void redirectWithPageParameters()
@@ -832,7 +833,8 @@ public class WicketTesterTest extends WicketTestCase
 	@Test(expected = PackageResourceBlockedException.class)
 	public void loadPageMarkupTemplate()
 	{
-		String url = "wicket/resource/"+BlockedResourceLinkPage.class.getName()+"/"+BlockedResourceLinkPage.class.getSimpleName()+".html";
+		String url = "wicket/resource/" + BlockedResourceLinkPage.class.getName() + "/" +
+			BlockedResourceLinkPage.class.getSimpleName() + ".html";
 		tester.executeUrl(url);
 	}
 
@@ -842,21 +844,23 @@ public class WicketTesterTest extends WicketTestCase
 	@Test
 	public void loadNonPageMarkupTemplate()
 	{
-		String url = "wicket/resource/"+BlockedResourceLinkPage.class.getName()+"/test.html";
+		String url = "wicket/resource/" + BlockedResourceLinkPage.class.getName() + "/test.html";
 		tester.executeUrl(url);
 		assertEquals("This is a test!\n", tester.getLastResponseAsString());
 	}
 
 	/**
-	 * Comma separated extensions should not be allowed.
-	 * The result is kinda error code 404 (resource not found)
+	 * Comma separated extensions should not be allowed. The result is kinda error code 404
+	 * (resource not found)
 	 */
 	@Test
 	public void clickResourceLinkWithSomeCommaAppendedUrl()
 	{
-		String url = "wicket/resource/"+BlockedResourceLinkPage.class.getName()+"/"+BlockedResourceLinkPage.class.getSimpleName()+".html,xml";
+		String url = "wicket/resource/" + BlockedResourceLinkPage.class.getName() + "/" +
+			BlockedResourceLinkPage.class.getSimpleName() + ".html,xml";
 		tester.executeUrl(url);
-		assertNull("Comma separated extensions are not supported and wont find any resource", tester.getLastResponse());
+		assertNull("Comma separated extensions are not supported and wont find any resource",
+			tester.getLastResponse());
 	}
 
 	/**
@@ -895,7 +899,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void cookieIsFoundWhenAddedToRequest()
@@ -905,7 +909,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void cookieIsFoundWhenAddedToResponse()
@@ -917,7 +921,7 @@ public class WicketTesterTest extends WicketTestCase
 	}
 
 	/**
-	 * 
+	 *
 	 */
 	@Test
 	public void cookieIsFoundOnNextRequestWhenAddedToResponse()
@@ -1152,7 +1156,7 @@ public class WicketTesterTest extends WicketTestCase
 
 	/**
 	 * https://issues.apache.org/jira/browse/WICKET-4437
-	 *
+	 * 
 	 * Clicking on ResourceLink should deliver the resource content
 	 */
 	@Test
@@ -1160,7 +1164,8 @@ public class WicketTesterTest extends WicketTestCase
 	{
 		MockPageWithLink page = new MockPageWithLink();
 		String content = "content";
-		ByteArrayResource resource = new ByteArrayResource("text/plain", content.getBytes(), "fileName.txt");
+		ByteArrayResource resource = new ByteArrayResource("text/plain", content.getBytes(),
+			"fileName.txt");
 		ResourceLink<Void> link = new ResourceLink<Void>(MockPageWithLink.LINK_ID, resource);
 		page.add(link);
 		tester.startPage(page);
@@ -1171,9 +1176,9 @@ public class WicketTesterTest extends WicketTestCase
 
 	/**
 	 * https://issues.apache.org/jira/browse/WICKET-4507
-	 *
-	 * When WicketTester#startComponentInPage() is used then #getLastResponseAsString()
-	 * should return only the component's markup, without the autogenerated markup for the page
+	 * 
+	 * When WicketTester#startComponentInPage() is used then #getLastResponseAsString() should
+	 * return only the component's markup, without the autogenerated markup for the page
 	 */
 	@Test
 	public void renderOnlyComponentsMarkup()
@@ -1193,4 +1198,61 @@ public class WicketTesterTest extends WicketTestCase
 		assertEquals("<span wicket:id=\"label\" test=\"123\">content</span>",
 			tester.getLastResponseAsString());
 	}
+
+	@Test
+	public void catchExternalRedirect() throws Exception
+	{
+		class RedirectPage extends WebPage
+		{
+			@Override
+			protected void onConfigure()
+			{
+				throw new RedirectToUrlException(
+					"https://issues.apache.org/jira/browse/WICKET-4558");
+			}
+		}
+		tester.startPage(new RedirectPage());
+		tester.assertRedirectUrl("https://issues.apache.org/jira/browse/WICKET-4558");
+	}
+
+	@Test
+	public void catchExternalRedirectFailure() throws Exception
+	{
+		class RedirectPage extends WebPage
+		{
+			@Override
+			protected void onConfigure()
+			{
+				throw new RedirectToUrlException("http://some.url/");
+			}
+		}
+		tester.startPage(new RedirectPage());
+		boolean caught;
+		try
+		{
+			tester.assertRedirectUrl("http://this.did.not.happen");
+			caught = false;
+		}
+		catch (AssertionFailedError e)
+		{
+			caught = true;
+		}
+		assertTrue(caught);
+	}
+
+	@Test
+	public void dontCatchInternalRedirect() throws Exception
+	{
+		class RedirectPage extends WebPage
+		{
+			@Override
+			protected void onConfigure()
+			{
+				throw new RedirectToUrlException("wicket/bookmarkable/" +
+					CreateBook.class.getName());
+			}
+		}
+		tester.startPage(new RedirectPage());
+		tester.assertRenderedPage(CreateBook.class);
+	}
 }