Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import feign.Param;
import feign.RequestLine;
import feign.Response;
import feign.Util;
import feign.assertj.MockWebServerAssertions;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -218,6 +219,30 @@ public void testResponseLength() throws Exception {
assertEquals(expected, actual);
}

@Test
public void testContentTypeWithCharset() throws Exception {
server.enqueue(new MockResponse()
.setBody("AAAAAAAA"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());

Response response = api.postWithContentType("foo", "text/plain;charset=utf-8");
// Response length should not be null
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
}

@Test
public void testContentTypeWithoutCharset() throws Exception {
server.enqueue(new MockResponse()
.setBody("AAAAAAAA"));
TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());

Response response = api.postWithContentType("foo", "text/plain");
// Response length should not be null
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
}

public interface TestInterface {

@RequestLine("POST /?foo=bar&foo=baz&qux=")
Expand All @@ -241,6 +266,10 @@ public interface TestInterface {

@RequestLine("PUT")
String noPutBody();

@RequestLine("POST /?foo=bar&foo=baz&qux=")
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"})
Response postWithContentType(String body, @Param("contentType") String contentType);
}

}
12 changes: 6 additions & 6 deletions httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
private ContentType getContentType(Request request) {
ContentType contentType = ContentType.DEFAULT_TEXT;
for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
Collection values = entry.getValue();
if (values != null && !values.isEmpty()) {
contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
break;
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
Collection<String> values = entry.getValue();
if (values != null && !values.isEmpty()) {
contentType = ContentType.parse(values.iterator().next());
break;
}
}
}
return contentType;
}

Expand Down