This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 7ac71f1  Don't let users override content-type header when using 
multipart/form-data
7ac71f1 is described below

commit 7ac71f15f8b11792d394d2b1bc596a73bd901cfa
Author: Felix Schumacher <[email protected]>
AuthorDate: Sun May 30 13:58:33 2021 +0200

    Don't let users override content-type header when using multipart/form-data
    
    The content-type header will contain the boundary from the multiparts. That
    value can't be guessed by the user and using the original given content-type
    will very likely be wrong.
    
    Apart from that reason, the Java implementation of HTTPSampler will use
    the generated content-type anyway and therefore this patch will make the
    two implementations behave consistently.
    
    Bugzilla Id: 65310
---
 .../jmeter/protocol/http/sampler/HTTPHC4Impl.java       |  6 ++++++
 .../jmeter/protocol/http/sampler/TestHTTPHC4Impl.java   | 17 +++++++++++++++++
 xdocs/changes.xml                                       |  2 ++
 3 files changed, 25 insertions(+)

diff --git 
a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
 
b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
index 3396ddb..bf66dc2 100644
--- 
a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
+++ 
b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java
@@ -1531,6 +1531,12 @@ public class HTTPHC4Impl extends HTTPHCAbstractImpl {
         // Check if we should do a multipart/form-data or an
         // application/x-www-form-urlencoded post request
         if(getUseMultipart()) {
+            if 
(entityEnclosingRequest.getHeaders(HTTPConstants.HEADER_CONTENT_TYPE).length > 
0) {
+                log.info(
+                        "Content-Header is set already on the request! Will be 
replaced by a Multipart-Header. Old headers: {}",
+                        
Arrays.asList(entityEnclosingRequest.getHeaders(HTTPConstants.HEADER_CONTENT_TYPE)));
+                
entityEnclosingRequest.removeHeaders(HTTPConstants.HEADER_CONTENT_TYPE);
+            }
             // If a content encoding is specified, we use that as the
             // encoding of any parameter values
             Charset charset;
diff --git 
a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC4Impl.java
 
b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC4Impl.java
index 0d2223f..5194852 100644
--- 
a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC4Impl.java
+++ 
b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestHTTPHC4Impl.java
@@ -19,6 +19,7 @@ package org.apache.jmeter.protocol.http.sampler;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.io.File;
 import java.util.Collections;
@@ -27,6 +28,7 @@ import 
org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
 import org.apache.jmeter.protocol.http.util.HTTPArgument;
+import org.apache.jmeter.protocol.http.util.HTTPConstants;
 import org.apache.jmeter.protocol.http.util.HTTPFileArg;
 import org.apache.jmeter.threads.JMeterContext;
 import org.apache.jmeter.threads.JMeterContextService;
@@ -63,6 +65,21 @@ public class TestHTTPHC4Impl {
     }
 
     @Test
+    void testParameterWithMultipartAndExplicitHeader() throws Exception {
+        HTTPSamplerBase sampler = (HTTPSamplerBase) new 
HttpTestSampleGui().createTestElement();
+        sampler.setThreadContext(jmctx);
+        sampler.setDoMultipart(true);
+        sampler.setDoBrowserCompatibleMultipart(true);
+        HttpEntityEnclosingRequestBase post = new HttpPost();
+        post.addHeader(HTTPConstants.HEADER_CONTENT_TYPE, "application/json");
+        sampler.setHTTPFiles(new HTTPFileArg[] {new HTTPFileArg("filename", 
"file", "application/octect; charset=utf-8")});
+        HTTPHC4Impl hc = new HTTPHC4Impl(sampler);
+        String requestData = hc.setupHttpEntityEnclosingRequestData(post);
+        assertEquals(0, 
post.getHeaders(HTTPConstants.HEADER_CONTENT_TYPE).length);
+        assertTrue(requestData.contains("charset=utf-8"));
+    }
+
+    @Test
     void testFileargWithMimeTypeWithCharset() throws Exception {
         HTTPSamplerBase sampler = (HTTPSamplerBase) new 
HttpTestSampleGui().createTestElement();
         sampler.setThreadContext(jmctx);
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 49b53ed..845f5e9 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -144,6 +144,8 @@ Summary
 
 <h3>HTTP Samplers and Test Script Recorder</h3>
 <ul>
+  <li><bug>65310</bug>Don't let users override 
<code>multipart/form-data</code> <code>content-type</code>
+    header in HC4 sampler.</li>
 </ul>
 
 <h3>Other Samplers</h3>

Reply via email to