You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/03/27 16:53:49 UTC

[httpcomponents-client] branch 4.5.x updated: HTTPCLIENT-2144: RequestBuilder fails to correctly copy charset of requests with form url-encoded body

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

olegk pushed a commit to branch 4.5.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/4.5.x by this push:
     new 33acdb6  HTTPCLIENT-2144: RequestBuilder fails to correctly copy charset of requests with form url-encoded body
33acdb6 is described below

commit 33acdb602e10dec891b8a204daa1668b8a8e0e61
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Mar 27 17:50:47 2021 +0100

    HTTPCLIENT-2144: RequestBuilder fails to correctly copy charset of requests with form url-encoded body
---
 .../apache/http/client/methods/RequestBuilder.java |  1 +
 .../http/client/methods/TestRequestBuilder.java    | 22 +++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
index 818ec66..9caa466 100644
--- a/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
+++ b/httpclient/src/main/java/org/apache/http/client/methods/RequestBuilder.java
@@ -278,6 +278,7 @@ public class RequestBuilder {
             if (contentType != null &&
                     contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
                 try {
+                    charset = contentType.getCharset();
                     final List<NameValuePair> formParams = URLEncodedUtils.parse(originalEntity);
                     if (!formParams.isEmpty()) {
                         parameters = formParams;
diff --git a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java
index 84fea77..7b7b0a4 100644
--- a/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java
+++ b/httpclient/src/test/java/org/apache/http/client/methods/TestRequestBuilder.java
@@ -27,9 +27,7 @@
 
 package org.apache.http.client.methods;
 
-import java.net.URI;
-import java.util.List;
-
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -46,6 +44,9 @@ import org.apache.http.util.EntityUtils;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.net.URI;
+import java.util.List;
+
 public class TestRequestBuilder {
 
     @Test
@@ -201,6 +202,21 @@ public class TestRequestBuilder {
     }
 
     @Test
+    public void testCopyWithStringEntityAndCharset() throws Exception {
+        final HttpPost post = new HttpPost("/stuff?p1=wtf");
+        final HttpEntity entity = new StringEntity("p1=this&p2=that",
+                ContentType.APPLICATION_FORM_URLENCODED.withCharset(Consts.ISO_8859_1));
+        post.setEntity(entity);
+        final RequestBuilder builder = RequestBuilder.copy(post);
+        final List<NameValuePair> parameters = builder.getParameters();
+        Assert.assertNotNull(parameters);
+        Assert.assertEquals(2, parameters.size());
+        Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
+        Assert.assertNull(builder.getEntity());
+        Assert.assertEquals(Consts.ISO_8859_1, builder.getCharset());
+    }
+
+    @Test
     public void testCopyAndSetUri() throws Exception {
         final URI uri1 = URI.create("http://host1.com/path?param=something");
         final URI uri2 = URI.create("http://host2.com/path?param=somethingdifferent");