SF.net SVN: htmlunit: [1236] trunk/htmlunit/src

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SF.net SVN: htmlunit: [1236] trunk/htmlunit/src

mguillem
Revision: 1236
          http://svn.sourceforge.net/htmlunit/?rev=1236&view=rev
Author:   mguillem
Date:     2006-09-20 12:29:08 -0700 (Wed, 20 Sep 2006)

Log Message:
-----------
Follow redirection to the same Url if original method was a POST. (#1531821)
Handle redirections like browsers do rather than according to the RFC.

Modified Paths:
--------------
    trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java
    trunk/htmlunit/src/xdocs/changes.xml

Modified: trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java
===================================================================
--- trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java 2006-09-17 15:15:57 UTC (rev 1235)
+++ trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java 2006-09-20 19:29:08 UTC (rev 1236)
@@ -1388,19 +1388,21 @@
             getLog().debug("Got a redirect status code ["+statusCode
                 +"] new location=["+locationString+"]");
 
-            if( webResponse.getUrl().toExternalForm().equals(locationString) ) {
+            if (webRequestSettings.getSubmitMethod().equals(SubmitMethod.GET)
+                    && webResponse.getUrl().toExternalForm().equals(locationString) ) {
                 getLog().warn("Got a redirect but the location is the same as the page we just loaded ["
-                    +locationString+"]. Skipping redirection.");
+                    + locationString + "]. Skipping redirection.");
             }
-            else if( ( statusCode == 301 || statusCode == 307 )
+            else if ((statusCode == 301 || statusCode == 307)
                 && method.equals(SubmitMethod.GET) ) {
 
                 final WebRequestSettings wrs = new WebRequestSettings(webRequestSettings, newUrl);
                 wrs.setRequestParameters(parameters);
                 return loadWebResponse(wrs);
             }
-            else if( statusCode == 302 || statusCode == 303 ) {
+            else if (statusCode <= 303) {
                 final WebRequestSettings wrs = new WebRequestSettings(webRequestSettings, newUrl);
+                wrs.setSubmitMethod(SubmitMethod.GET);
                 return loadWebResponse(wrs);
             }
         }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java 2006-09-17 15:15:57 UTC (rev 1235)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java 2006-09-20 19:29:08 UTC (rev 1236)
@@ -271,20 +271,68 @@
         doTestRedirection( statusCode, initialRequestMethod, expectedRedirectedRequestMethod );
     }
 
+    /**
+     * Common utility for GET after POST redirection on same urls
+     * @param statusCode The code to return from the initial request
+     * @throws Exception if the test fails.
+     */
+    private void doTestRedirectionSameUrlAfterPost(final int statusCode)
+        throws
+             Exception {
 
+        final String firstContent = "<html><head><title>First</title></head><body></body></html>";
+        final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
+
+        final WebClient webClient = new WebClient();
+
+        final List headers = Collections.singletonList(
+                new KeyValuePair("Location", URL_FIRST.toExternalForm()) );
+        
+        // builds a webconnection that first sends a redirect and then a "normal" response for
+        // the same requested url
+        final MockWebConnection webConnection = new MockWebConnection(webClient) {
+            private int count_ = 0;
+            public WebResponse getResponse(final WebRequestSettings _webRequestSettings) throws IOException {
+                ++count_;
+                if (count_ == 1) {
+                    final WebResponse response = super.getResponse(_webRequestSettings);
+                    setResponse(_webRequestSettings.getURL(), secondContent);
+                    return response;
+                }
+                else {
+                    return super.getResponse(_webRequestSettings);
+                }
+            }
+        };
+        webConnection.setResponse(
+                URL_FIRST, firstContent, statusCode,
+                "Some error", "text/html", headers );
+        webClient.setWebConnection( webConnection );
+
+        HtmlPage page = (HtmlPage) webClient.getPage(new WebRequestSettings(URL_FIRST, SubmitMethod.POST));
+        final WebResponse webResponse = page.getWebResponse();
+        // A redirect should have happened
+        assertEquals(200, webResponse.getStatusCode());
+        assertEquals(URL_FIRST, webResponse.getUrl());
+        assertEquals("Second", page.getTitleText());
+        assertEquals(SubmitMethod.GET, webResponse.getRequestMethod());
+    }
+
     /**
      * From the http spec:  If the 301 status code is received in response
      * to a request other than GET or HEAD, the user agent MUST NOT automatically
      * redirect the request unless it can be confirmed by the user, since this
      * might change the conditions under which the request was issued.
+     * BUT Firefox follows the redirection
      * @throws Exception If something goes wrong.
      */
     public void testRedirection301_MovedPermanently_PostMethod() throws Exception {
         final int statusCode = 301;
         final SubmitMethod initialRequestMethod = SubmitMethod.POST;
-        final SubmitMethod expectedRedirectedRequestMethod = null;
+        final SubmitMethod expectedRedirectedRequestMethod = SubmitMethod.GET;
 
         doTestRedirection( statusCode, initialRequestMethod, expectedRedirectedRequestMethod );
+        doTestRedirectionSameUrlAfterPost(statusCode);
     }
 
 
@@ -304,6 +352,7 @@
         final SubmitMethod expectedRedirectedRequestMethod = SubmitMethod.GET;
 
         doTestRedirection( statusCode, initialRequestMethod, expectedRedirectedRequestMethod );
+        doTestRedirectionSameUrlAfterPost(statusCode);
     }
 
 
@@ -351,6 +400,7 @@
         final SubmitMethod expectedRedirectedRequestMethod = SubmitMethod.GET;
 
         doTestRedirection( statusCode, initialRequestMethod, expectedRedirectedRequestMethod );
+        doTestRedirectionSameUrlAfterPost(statusCode);
     }
 
 

Modified: trunk/htmlunit/src/xdocs/changes.xml
===================================================================
--- trunk/htmlunit/src/xdocs/changes.xml 2006-09-17 15:15:57 UTC (rev 1235)
+++ trunk/htmlunit/src/xdocs/changes.xml 2006-09-20 19:29:08 UTC (rev 1236)
@@ -7,6 +7,10 @@
 
     <body>
         <release version="1.10-prerelease">
+            <action type="update" dev="mguillem" id="1531821">
+                Follow redirection to the same Url if original method was a POST. Handle redirections
+                like browsers do rather than according to the RFC.
+            </action>
             <action type="update" dev="mguillem" id="1345136">
                 JavaScript: allow script to set a "javascript:..." location.
             </action>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop