Skip to content

Commit

Permalink
Merge pull request #1299 from Nishant-sehgal/develop
Browse files Browse the repository at this point in the history
Issue-1181 ApacheHttpClient custom content type quotes issue fix
  • Loading branch information
ptrthomas authored Oct 5, 2020
2 parents ddbd716 + 3c407a6 commit 95706fe
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
7 changes: 7 additions & 0 deletions karate-apache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
<version>1.7.25</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,21 +82,17 @@ private static ContentType getContentType(String mediaType, Charset charset) {
return null;
}
}
Map<String, String> 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<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 95706fe

Please sign in to comment.