From c854c3b58953a56295a7c8d0c1c0a3642e5ee8a4 Mon Sep 17 00:00:00 2001 From: dongfangtianyu <7629022+dongfangtianyu@users.noreply.github.com> Date: Sun, 17 Mar 2024 21:58:21 +0800 Subject: [PATCH] fix: The HTTP Header of `multipart/form-data` no longer includes `charset`. --- .../protocol/http/sampler/HTTPHC4Impl.java | 1 - .../protocol/http/sampler/TestHTTPHC4Impl.java | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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 bc4478eecb3..c160ec77402 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 @@ -1562,7 +1562,6 @@ protected String setupHttpEntityEnclosingRequestData(HttpEntityEnclosingRequestB } // Write the request to our own stream MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); - multipartEntityBuilder.setCharset(charset); if (doBrowserCompatibleMultipart) { multipartEntityBuilder.setLaxMode(); } else { 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 315806d1b87..bb5c2620c43 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 @@ -97,6 +97,22 @@ void testFileargWithMimeTypeWithCharset() throws Exception { Assertions.assertTrue(requestData.contains("charset=utf-8")); } + @Test + void testMultipartFormHeaderWithoutCharset() 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; charset=utf-8"); + sampler.setHTTPFiles(new HTTPFileArg[] {new HTTPFileArg("filename", "file", "application/octect; charset=utf-8")}); + HTTPHC4Impl hc = new HTTPHC4Impl(sampler); + String requestData = hc.setupHttpEntityEnclosingRequestData(post); + + String contentTypeWithEntity = post.getEntity().getContentType().getValue(); + Assertions.assertFalse(contentTypeWithEntity.contains("charset")); + } + @Test public void testNotifyFirstSampleAfterLoopRestartWhenThreadIterationIsSameUser() { jmvars.putObject(SAME_USER, true);