From: <rb...@us...> - 2013-08-07 19:15:35
|
Revision: 8444 http://sourceforge.net/p/htmlunit/code/8444 Author: rbri Date: 2013-08-07 19:15:32 +0000 (Wed, 07 Aug 2013) Log Message: ----------- In case the server reports an error via http error code, the curretn page content was not replaced with the content of that error page if the error was the result of a form submit or an anchor click. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/FailingHttpStatusCodeExceptionTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/NoHttpResponseTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-08-07 16:20:25 UTC (rev 8443) +++ trunk/htmlunit/src/changes/changes.xml 2013-08-07 19:15:32 UTC (rev 8444) @@ -8,6 +8,11 @@ <body> <release version="2.13" date="???" description="Bugfixes"> + <action type="fix" dev="rbri"> + In case the server reports an error via http error code, the curretn page content + was not replaced with the content of that error page if the error was the result + of a form submit or an anchor click. + </action> <action type="fix" dev="mguillem" issue="1481"> JavaScript: added (simple) support for window.postMessage. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2013-08-07 16:20:25 UTC (rev 8443) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2013-08-07 19:15:32 UTC (rev 8444) @@ -1968,9 +1968,6 @@ else { try { final WebResponse response = loadWebResponse(request); - // check and report problems if needed - throwFailingHttpStatusCodeExceptionIfNecessary(response); - loadJob = new LoadJob(request, requestingWindow, target, response); } catch (final IOException e) { @@ -2040,6 +2037,9 @@ if (pageBeforeLoad != win.getEnclosedPage()) { updatedWindows.add(win); } + + // check and report problems if needed + throwFailingHttpStatusCodeExceptionIfNecessary(downloadedResponse.response_); } else { LOG.info("No usage of download: " + downloadedResponse); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/FailingHttpStatusCodeExceptionTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/FailingHttpStatusCodeExceptionTest.java 2013-08-07 16:20:25 UTC (rev 8443) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/FailingHttpStatusCodeExceptionTest.java 2013-08-07 19:15:32 UTC (rev 8444) @@ -19,9 +19,11 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.http.HttpStatus; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.util.NameValuePair; @@ -30,6 +32,7 @@ * * @version $Revision$ * @author Marc Guillemot + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public final class FailingHttpStatusCodeExceptionTest extends SimpleWebTestCase { @@ -55,20 +58,62 @@ /** * @throws Exception if the test fails */ - @Test(expected = FailingHttpStatusCodeException.class) + @Test public void failureByGetPage() throws Exception { - getMockWebConnection().setDefaultResponse("", 404, "Not Found", "text/html"); - getWebClientWithMockWebConnection().getPage(getDefaultUrl()); + getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", "text/html"); + final WebClient client = getWebClientWithMockWebConnection(); + try { + client.getPage(getDefaultUrl()); + Assert.fail("FailingHttpStatusCodeException expected"); + } + catch (final FailingHttpStatusCodeException e) { + // expected + } + assertEquals("Error: not found", + client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString()); } /** * @throws Exception if the test fails */ - @Test(expected = FailingHttpStatusCodeException.class) + @Test public void failureByClickLink() throws Exception { final String html = "<html><body><a href='doesntExist'>go</a></body></html>"; - getMockWebConnection().setDefaultResponse("", 404, "Not Found", "text/html"); - final HtmlPage page = loadPageWithAlerts(html); - page.getAnchors().get(0).click(); + getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", "text/html"); + + final WebClient client = getWebClientWithMockWebConnection(); + try { + final HtmlPage page = loadPageWithAlerts(html); + page.getAnchors().get(0).click(); + } + catch (final FailingHttpStatusCodeException e) { + // expected + } + assertEquals("Error: not found", + client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString()); } + + /** + * @throws Exception if the test fails + */ + @Test + public void failureBySubmit() throws Exception { + final String html = "<html><body>\n" + + "<form name='form1' method='get' action='foo.html'>\n" + + " <input name='button' type='submit' id='mySubmit'/>\n" + + "</form>\n" + + "</body></html>"; + getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", "text/html"); + + final WebClient client = getWebClientWithMockWebConnection(); + try { + final HtmlPage page = loadPageWithAlerts(html); + ((HtmlElement) page.getElementById("mySubmit")).click(); + } + catch (final FailingHttpStatusCodeException e) { + // expected + } + assertEquals("Error: not found", + client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString()); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/NoHttpResponseTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/NoHttpResponseTest.java 2013-08-07 16:20:25 UTC (rev 8443) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/NoHttpResponseTest.java 2013-08-07 19:15:32 UTC (rev 8444) @@ -26,7 +26,6 @@ import java.nio.CharBuffer; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; @@ -50,9 +49,9 @@ public class NoHttpResponseTest { private static final String html = "<html><body><script>\n" - + "function fillField(){\n" - + " document.forms.loginform.textfield.value = 'new value';\n" - + "}\n" + + " function fillField(){\n" + + " document.forms.loginform.textfield.value = 'new value';\n" + + " }\n" + "</script>\n" + "<form name='loginform' action='page2' method='get'>\n" + " <input type='text' name='textfield' value='' />\n" @@ -115,9 +114,6 @@ final HtmlPage page = getWebClient().getPage(getDefaultUrl()); ((HtmlElement) page.getElementById("loginButton")).click(); } - catch (final Exception e) { - throw ExceptionUtils.getRootCause(e); - } finally { miniServer.shutDown(); } |