[HtmlUnit] SVN: [15456] trunk/htmlunit/src

rbri--- via HtmlUnit-develop <[email protected]>
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15456
          http://sourceforge.net/p/htmlunit/code/15456
Author:   rbri
Date:     2018-07-12 19:10:53 +0000 (Thu, 12 Jul 2018)
Log Message:
-----------
url encoding has to use UTF-8 as codepage
Issue 1973

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/PrimitiveWebServer.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient2Test.java

Added Paths:
-----------
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient7Test.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java	2018-07-12 15:36:52 UTC (rev 15455)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java	2018-07-12 19:10:53 UTC (rev 15456)
@@ -1312,8 +1312,7 @@
                 return makeWebResponseForDataUrl(webRequest);
 
             default:
-                return loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL,
-                                                            webRequest.getCharset());
+                return loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL);
         }
     }
 
@@ -1321,12 +1320,11 @@
      * Loads a {@link WebResponse} from the server through the WebConnection.
      * @param webRequest the request
      * @param allowedRedirects the number of allowed redirects remaining
-     * @param charset the charset to use
      * @throws IOException if an IO problem occurs
      * @return the resultant {@link WebResponse}
      */
     private WebResponse loadWebResponseFromWebConnection(final WebRequest webRequest,
-        final int allowedRedirects, final Charset charset) throws IOException {
+        final int allowedRedirects) throws IOException {
 
         URL url = webRequest.getUrl();
         final HttpMethod method = webRequest.getHttpMethod();
@@ -1336,7 +1334,7 @@
         WebAssert.notNull("method", method);
         WebAssert.notNull("parameters", parameters);
 
-        url = UrlUtils.encodeUrl(url, getBrowserVersion().hasFeature(URL_MINIMAL_QUERY_ENCODING), charset);
+        url = UrlUtils.encodeUrl(url, getBrowserVersion().hasFeature(URL_MINIMAL_QUERY_ENCODING), UTF_8);
         webRequest.setUrl(url);
 
         if (LOG.isDebugEnabled()) {
@@ -1451,7 +1449,7 @@
                 for (final Map.Entry<String, String> entry : webRequest.getAdditionalHeaders().entrySet()) {
                     wrs.setAdditionalHeader(entry.getKey(), entry.getValue());
                 }
-                return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1, UTF_8);
+                return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1);
             }
             else if (status == HttpStatus.SC_TEMPORARY_REDIRECT
                         || status == 308) {
@@ -1460,7 +1458,7 @@
                 for (final Map.Entry<String, String> entry : webRequest.getAdditionalHeaders().entrySet()) {
                     wrs.setAdditionalHeader(entry.getKey(), entry.getValue());
                 }
-                return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1, UTF_8);
+                return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1);
             }
         }
 

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/PrimitiveWebServer.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/PrimitiveWebServer.java	2018-07-12 15:36:52 UTC (rev 15455)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/PrimitiveWebServer.java	2018-07-12 19:10:53 UTC (rev 15456)
@@ -54,7 +54,7 @@
      *
      * @param port the port
      * @param firstResponse the first response, must contain the full response (to start with "HTTP/1.1 200 OK")
-     * @param otherResponse the sebsequent response, must contain the full response (to start with "HTTP/1.1 200 OK")
+     * @param otherResponse the subsequent response, must contain the full response (to start with "HTTP/1.1 200 OK")
      */
     public PrimitiveWebServer(final int port, final String firstResponse, final String otherResponse) {
         port_ = port;

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient2Test.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient2Test.java	2018-07-12 15:36:52 UTC (rev 15455)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient2Test.java	2018-07-12 19:10:53 UTC (rev 15456)
@@ -55,59 +55,6 @@
     }
 
     /**
-     * Test that the path and query string are encoded to be valid.
-     * @throws Exception if something goes wrong
-     */
-    @Test
-    public void loadPage_EncodeRequest() throws Exception {
-        final String htmlContent
-            = "<html><head><title>foo</title></head><body>\n"
-            + "</body></html>";
-
-        final WebClient client = getWebClient();
-
-        final MockWebConnection webConnection = new MockWebConnection();
-        webConnection.setDefaultResponse(htmlContent);
-        client.setWebConnection(webConnection);
-
-        // with query string not encoded
-        HtmlPage page = client.getPage("http://first?a=b c&d=\u00E9\u00E8");
-        final String expected;
-        final boolean ie = getBrowserVersion().isIE();
-        if (ie) {
-            expected = "?a=b%20c&d=\u00E9\u00E8";
-        }
-        else {
-            expected = "?a=b%20c&d=%E9%E8";
-        }
-        assertEquals("http://first/" + expected, page.getUrl());
-
-        // with query string already encoded
-        page = client.getPage("http://first?a=b%20c&d=%C3%A9%C3%A8");
-        assertEquals("http://first/?a=b%20c&d=%C3%A9%C3%A8", page.getUrl());
-
-        // with query string partially encoded
-        page = client.getPage("http://first?a=b%20c&d=e f");
-        assertEquals("http://first/?a=b%20c&d=e%20f", page.getUrl());
-
-        // with anchor
-        page = client.getPage("http://first?a=b c#myAnchor");
-        assertEquals("http://first/?a=b%20c#myAnchor", page.getUrl());
-
-        // with query string containing encoded "&", "=", "+", ",", and "$"
-        page = client.getPage("http://first?a=%26%3D%20%2C%24");
-        assertEquals("http://first/?a=%26%3D%20%2C%24", page.getUrl());
-
-        // with character to encode in path
-        page = client.getPage("http://first/page 1.html");
-        assertEquals("http://first/page%201.html", page.getUrl());
-
-        // with character to encode in path
-        page = client.getPage("http://first/page 1.html");
-        assertEquals("http://first/page%201.html", page.getUrl());
-    }
-
-    /**
      * Test for 3151939. The Browser removes leading '/..' from the path.
      * @throws Exception if something goes wrong
      */

Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient7Test.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient7Test.java	                        (rev 0)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient7Test.java	2018-07-12 19:10:53 UTC (rev 15456)
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2002-2018 Gargoyle Software Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gargoylesoftware.htmlunit;
+
+import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.IE;
+
+import java.net.URL;
+
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.openqa.selenium.WebDriver;
+
+import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
+import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented;
+
+/**
+ * Tests using the {@link PrimitiveWebServer}.
+ *
+ * @author Ahmed Ashour
+ * @author Ronald Brill
+ */
+@RunWith(BrowserRunner.class)
+public class WebClient7Test extends WebDriverTestCase {
+
+    private PrimitiveWebServer primitiveWebServer_;
+
+    /**
+     * @throws Exception if an error occurs
+     */
+    @After
+    public void stopServer() throws Exception {
+        if (primitiveWebServer_ != null) {
+            primitiveWebServer_.stop();
+        }
+        shutDownAll();
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts(DEFAULT = "/test.html?a=b%20c&d=%C3%A9%C3%A8",
+            IE = "/test.html?a=b%20c&d=\u00E9\u00E8")
+    @NotYetImplemented(IE)
+    public void loadPage_EncodeRequest() throws Exception {
+        // with query string not encoded
+        testRequestUrlEncoding("test.html?a=b c&d=\u00E9\u00E8");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts("/test.html?a=b%20c&d=%C3%A9%C3%A8")
+    public void loadPage_EncodeRequest2() throws Exception {
+        // with query string already encoded
+        testRequestUrlEncoding("test.html?a=b%20c&d=%C3%A9%C3%A8");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts("/test.html?a=b%20c&d=e%20f")
+    public void loadPage_EncodeRequest3() throws Exception {
+        // with query string partially encoded
+        testRequestUrlEncoding("test.html?a=b%20c&d=e f");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts("/test.html?a=b%20c")
+    public void loadPage_EncodeRequest4() throws Exception {
+        // with anchor
+        testRequestUrlEncoding("test.html?a=b c#myAnchor");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts("/test.html?a=%26%3D%20%2C%24")
+    public void loadPage_EncodeRequest5() throws Exception {
+        // with query string containing encoded "&", "=", "+", ",", and "$"
+        testRequestUrlEncoding("test.html?a=%26%3D%20%2C%24");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts("/page%201.html")
+    public void loadPage_EncodeRequest6() throws Exception {
+        // with character to encode in path
+        testRequestUrlEncoding("page 1.html");
+    }
+
+    /**
+     * Test that the path and query string are encoded to be valid.
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    @Alerts(DEFAULT = "/test.html?param=%C2%A9",
+            IE = "/test.html?param=\u00A9")
+    @NotYetImplemented(IE)
+    public void loadPage_EncodeRequest7() throws Exception {
+        // unicode
+        testRequestUrlEncoding("test.html?param=\u00A9");
+    }
+
+    private void testRequestUrlEncoding(final String url) throws Exception {
+        final String response = "HTTP/1.1 200 OK\r\n"
+                + "Content-Length: 2\r\n"
+                + "Content-Type: text/html\r\n"
+                + "\r\n"
+                + "<html><head><title>foo</title></head><body>"
+                + "</body></html>";
+
+        primitiveWebServer_ = new PrimitiveWebServer(PORT, response, response);
+        primitiveWebServer_.start();
+
+        final WebDriver driver = getWebDriver();
+
+        driver.get(new URL(URL_FIRST, url).toString());
+        String reqUrl = primitiveWebServer_.getRequests().get(0);
+        if (reqUrl.contains("/favicon.ico")) {
+            reqUrl = primitiveWebServer_.getRequests().get(1);
+        }
+        reqUrl = reqUrl.substring(4, reqUrl.indexOf("HTTP/1.1") - 1);
+
+        assertEquals(getExpectedAlerts()[0], reqUrl);
+    }
+}


Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClient7Test.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.