Skip to content

Commit 36a1ce8

Browse files
committed
fix: convert media type with charset
1 parent 46f6ef2 commit 36a1ce8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

riptide-httpclient/src/main/java/org/zalando/riptide/httpclient/BufferingApacheClientHttpRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public ClientHttpResponse execute() throws IOException {
7878
@Nullable
7979
private ContentType toContentType(@Nullable MediaType mediaType) {
8080
return Optional.ofNullable(mediaType)
81-
.map(MediaType::toString)
82-
.map(ContentType::create)
81+
.map(ContentTypeConverter::toContentType)
8382
.orElse(null);
8483
}
8584

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.zalando.riptide.httpclient;
2+
3+
import org.apache.hc.core5.http.ContentType;
4+
import org.springframework.http.MediaType;
5+
6+
public class ContentTypeConverter {
7+
8+
public static ContentType toContentType(MediaType mediaType) {
9+
return ContentType.create(mediaType.getType() + "/" + mediaType.getSubtype(), mediaType.getCharset());
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.zalando.riptide.httpclient;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.http.MediaType;
5+
6+
import java.nio.charset.StandardCharsets;
7+
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.hamcrest.Matchers.is;
10+
import static org.hamcrest.Matchers.nullValue;
11+
12+
class ContentTypeConverterTest {
13+
14+
@Test
15+
void shouldConvertMediaTypeWithCharset() {
16+
var contentType = ContentTypeConverter.toContentType(new MediaType(MediaType.TEXT_XML, StandardCharsets.UTF_8));
17+
assertThat(contentType.getCharset(), is(StandardCharsets.UTF_8));
18+
assertThat(contentType.getMimeType(), is("text/xml"));
19+
}
20+
21+
@Test
22+
void shouldConvertMediaTypeWithoutCharset() {
23+
var contentType = ContentTypeConverter.toContentType(MediaType.TEXT_XML);
24+
assertThat(contentType.getCharset(), is(nullValue()));
25+
assertThat(contentType.getMimeType(), is("text/xml"));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)