From 3c407a6404a30462084339d2ad7b7817d9fec6b5 Mon Sep 17 00:00:00 2001 From: Nishant-Sehgal Date: Sun, 4 Oct 2020 21:51:28 +0530 Subject: [PATCH] Issue-1181 ApacheHttpClient custom content type quotes issue fix --- karate-apache/pom.xml | 7 ++ .../karate/http/apache/ApacheHttpUtils.java | 28 +++---- .../http/apache/ApacheHttpUtilsTest.java | 73 +++++++++++++++++++ 3 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 karate-apache/src/test/java/com/intuit/karate/http/apache/ApacheHttpUtilsTest.java diff --git a/karate-apache/pom.xml b/karate-apache/pom.xml index e3ec01b7a..fd523dd38 100755 --- a/karate-apache/pom.xml +++ b/karate-apache/pom.xml @@ -47,6 +47,13 @@ 1.7.25 runtime + + + junit + junit + ${junit.version} + test + diff --git a/karate-apache/src/main/java/com/intuit/karate/http/apache/ApacheHttpUtils.java b/karate-apache/src/main/java/com/intuit/karate/http/apache/ApacheHttpUtils.java index aee3a4a5a..6e95b4a03 100644 --- a/karate-apache/src/main/java/com/intuit/karate/http/apache/ApacheHttpUtils.java +++ b/karate-apache/src/main/java/com/intuit/karate/http/apache/ApacheHttpUtils.java @@ -29,12 +29,16 @@ import com.intuit.karate.http.HttpUtils; import com.intuit.karate.http.MultiPartItem; import com.intuit.karate.http.MultiValuedMap; + import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; + +import io.netty.handler.codec.http.HttpUtil; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; @@ -78,21 +82,17 @@ private static ContentType getContentType(String mediaType, Charset charset) { return null; } } - Map map = HttpUtils.parseContentTypeParams(mediaType); - if (map != null) { - String cs = map.get(HttpUtils.CHARSET); - if (cs != null) { - charset = Charset.forName(cs); - map.remove(HttpUtils.CHARSET); - } - } - ContentType ct = ContentType.parse(mediaType).withCharset(charset); - if (map != null) { - for (Map.Entry entry : map.entrySet()) { - ct = ct.withParameters(new BasicNameValuePair(entry.getKey(), entry.getValue())); - } + + // if charset is null that means mediaType does not contains any charset + Charset existingCharset = HttpUtil.getCharset(mediaType, null); + + // appending charset if not present + if(Objects.isNull(existingCharset)) { + mediaType = new StringBuilder(mediaType).append("; ").append(HttpUtils.CHARSET) + .append("=").append(charset.name()).toString(); } - return ct; + + return ContentType.parse(mediaType); } public static HttpEntity getEntity(InputStream is, String mediaType, Charset charset) { diff --git a/karate-apache/src/test/java/com/intuit/karate/http/apache/ApacheHttpUtilsTest.java b/karate-apache/src/test/java/com/intuit/karate/http/apache/ApacheHttpUtilsTest.java new file mode 100644 index 000000000..85bacb03f --- /dev/null +++ b/karate-apache/src/test/java/com/intuit/karate/http/apache/ApacheHttpUtilsTest.java @@ -0,0 +1,73 @@ +package com.intuit.karate.http.apache; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.nio.charset.StandardCharsets; + +import org.apache.http.HttpEntity; +import org.junit.Test; + +/** @author nsehgal */ +public class ApacheHttpUtilsTest { + + @Test + public void testContentTypeWithQuotes() { + final String originalContentType = + "multipart/related; charset=UTF-8; boundary=\"----=_Part_19_1913847857.1592612068756\"; type=\"application/xop+xml\"; start-info=\"text/xml\""; + + HttpEntity httpEntity = + ApacheHttpUtils.getEntity( + "content is not important", originalContentType, StandardCharsets.UTF_8); + + assertEquals(originalContentType, httpEntity.getContentType().getValue()); + } + + @Test + public void testContentTypeWithoutQuotes() { + final String originalContentType = + "multipart/related; charset=UTF-8; boundary=----=_Part_19_1913847857.1592612068756; type=application/xop+xml; start-info=text/xml"; + + final String expectedContentType = + "multipart/related; charset=UTF-8; boundary=\"----=_Part_19_1913847857.1592612068756\"; type=\"application/xop+xml\"; start-info=\"text/xml\""; + + HttpEntity httpEntity = + ApacheHttpUtils.getEntity( + "content is not important", originalContentType, StandardCharsets.UTF_8); + + assertNotEquals(originalContentType, httpEntity.getContentType().getValue()); + assertEquals(expectedContentType, httpEntity.getContentType().getValue()); + } + + @Test + public void testContentTypeWithoutQuotesCharsetInLast() { + final String originalContentType = + "multipart/related; boundary=----=_Part_19_1913847857.1592612068756; type=application/xop+xml; start-info=text/xml; charset=UTF-8"; + + final String expectedContentType = + "multipart/related; boundary=\"----=_Part_19_1913847857.1592612068756\"; type=\"application/xop+xml\"; start-info=\"text/xml\"; charset=UTF-8"; + + HttpEntity httpEntity = + ApacheHttpUtils.getEntity( + "content is not important", originalContentType, StandardCharsets.UTF_8); + + assertNotEquals(originalContentType, httpEntity.getContentType().getValue()); + assertEquals(expectedContentType, httpEntity.getContentType().getValue()); + } + + @Test + public void testContentTypeWithCustomCharset() { + final String originalContentType = + "multipart/related; boundary=----=_Part_19_1913847857.1592612068756; type=application/xop+xml; start-info=text/xml"; + + final String expectedContentType = + "multipart/related; boundary=\"----=_Part_19_1913847857.1592612068756\"; type=\"application/xop+xml\"; start-info=\"text/xml\"; charset=UTF-8"; + + HttpEntity httpEntity = + ApacheHttpUtils.getEntity( + "content is not important", originalContentType, StandardCharsets.UTF_8); + + assertNotEquals(originalContentType, httpEntity.getContentType().getValue()); + assertEquals(expectedContentType, httpEntity.getContentType().getValue()); + } +}