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

rbri--- via HtmlUnit-develop <[email protected]>
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15373
          http://sourceforge.net/p/htmlunit/code/15373
Author:   rbri
Date:     2018-06-20 18:42:28 +0000 (Wed, 20 Jun 2018)
Log Message:
-----------
WebClient default request header should not overwrite request-specific ones

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/WebClientTest.java

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml	2018-06-20 17:47:33 UTC (rev 15372)
+++ trunk/htmlunit/src/changes/changes.xml	2018-06-20 18:42:28 UTC (rev 15373)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.32" date="xx 2018" description="Bugfixes, FIREFOX_45 removed, FIREFOX_60 added">
+            <action type="fix" dev="rbri" issue="1970">
+                WebClient default request header should not overwrite request-specific ones.
+            </action>
             <action type="fix" dev="rbri" issue="1962">
                 Various fixes for the selector specificity calculation (CssParser).
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java	2018-06-20 17:47:33 UTC (rev 15372)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java	2018-06-20 18:42:28 UTC (rev 15373)
@@ -1480,8 +1480,13 @@
                 && !wrs.isAdditionalHeader(HttpHeader.UPGRADE_INSECURE_REQUESTS)) {
             wrs.setAdditionalHeader(HttpHeader.UPGRADE_INSECURE_REQUESTS, "1");
         }
+
         // Add user-specified headers last so that they can override HtmlUnit defaults.
-        wrs.getAdditionalHeaders().putAll(requestHeaders_);
+        requestHeaders_.forEach((name, value) -> {
+            if (!wrs.isAdditionalHeader(name)) {
+                wrs.setAdditionalHeader(name, value);
+            }
+        });
     }
 
     /**

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java	2018-06-20 17:47:33 UTC (rev 15372)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java	2018-06-20 18:42:28 UTC (rev 15373)
@@ -20,6 +20,7 @@
 import static org.easymock.EasyMock.expectLastCall;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
@@ -1519,6 +1520,32 @@
     }
 
     /**
+     * @throws Exception if something goes wrong
+     */
+    @Test
+    public void requestHeaderDoNotOverwriteExisting() throws Exception {
+        final String content = "<html></html>";
+        final WebClient client = getWebClient();
+
+        final MockWebConnection webConnection = new MockWebConnection();
+        webConnection.setDefaultResponse(content);
+        client.setWebConnection(webConnection);
+
+        client.getPage(URL_FIRST);
+        assertNotNull(webConnection.getLastAdditionalHeaders().get(HttpHeader.ACCEPT_LANGUAGE));
+        assertNotEquals("foo value", webConnection.getLastAdditionalHeaders().get(HttpHeader.ACCEPT_LANGUAGE));
+
+        client.addRequestHeader(HttpHeader.ACCEPT_LANGUAGE, "foo value");
+        client.getPage(URL_FIRST);
+        assertNotEquals("foo value", webConnection.getLastAdditionalHeaders().get(HttpHeader.ACCEPT_LANGUAGE));
+
+        client.removeRequestHeader(HttpHeader.ACCEPT_LANGUAGE);
+        client.getPage(URL_FIRST);
+        assertNotNull(webConnection.getLastAdditionalHeaders().get(HttpHeader.ACCEPT_LANGUAGE));
+        assertNotEquals("foo value", webConnection.getLastAdditionalHeaders().get(HttpHeader.ACCEPT_LANGUAGE));
+    }
+
+    /**
      * Test that content type is looked in a case insensitive way.
      * Cf <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>:
      * "All media type values, subtype values, and parameter names as defined


------------------------------------------------------------------------------
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.