HTTPCLIENT-2144: RequestBuilder fails to correctly copy charset of requests with form url-encoded body

This commit is contained in:
Oleg Kalnichevski 2021-03-27 17:50:47 +01:00
parent cad651a198
commit 33acdb602e
2 changed files with 20 additions and 3 deletions

View File

@ -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;

View File

@ -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
@ -200,6 +201,21 @@ public class TestRequestBuilder {
Assert.assertSame(entity, builder.getEntity());
}
@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");