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
2 changes: 1 addition & 1 deletion core/src/main/java/feign/RequestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ private RequestTemplate appendHeader(String name, Iterable<String> values, boole
this.headers.remove(name);
return this;
}
if (name.equals("Content-Type")) {
if (name.equalsIgnoreCase("Content-Type")) {
// a client can only produce content of one single type, so always override Content-Type and
// only add a single type
this.headers.remove(name);
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/feign/FeignBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,28 @@ void provideRequestInterceptors() throws Exception {
.hasBody("request data");
}

@Test
void requestInterceptorReplacesContentTypeWithLowerCaseHeaderName() throws Exception {
server.enqueue(new MockResponse());

String url = "http://localhost:" + server.getPort();
RequestInterceptor requestInterceptor =
template -> template.header("content-type", "text/plain");

JsonPostInterface api =
Feign.builder().requestInterceptor(requestInterceptor).target(JsonPostInterface.class, url);
api.post("request data");

assertThat(server.takeRequest().getHeaders().values("Content-Type"))
.containsExactly("text/plain");
}

interface JsonPostInterface {
@RequestLine("POST /")
@Headers("Content-Type: application/json")
void post(String data);
}

@Test
void provideInvocationHandlerFactory() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/feign/RequestTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@ void useCaseInsensitiveHeaderFieldNames() {
assertThat(template.headers().get("tesT")).hasSize(2);
}

@ParameterizedTest
@ValueSource(strings = {"content-type", "CONTENT-TYPE", "Content-type"})
void contentTypeIsReplacedWhateverTheCaseOfTheHeaderName(String headerName) {
final RequestTemplate template = new RequestTemplate();
template.header("Content-Type", "application/json");

template.header(headerName, "text/plain");

assertThat(template.headers()).hasSize(1);
assertThat(template.headers().get("Content-Type")).containsExactly("text/plain");
}

@Test
void encodeSlashTest() {
RequestTemplate template =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ private final HttpRequest convertRequest(
// Setup the request body
HttpContent content = null;
if (inputRequest.length() > 0) {
final Collection<String> contentTypeValues = inputRequest.headers().get("Content-Type");
String contentType = null;
if (contentTypeValues != null && contentTypeValues.size() > 0) {
contentType = contentTypeValues.iterator().next();
} else {
contentType = "application/octet-stream";
String contentType = "application/octet-stream";
for (final Map.Entry<String, Collection<String>> header : inputRequest.headers().entrySet()) {
if (isContentType(header.getKey())
&& header.getValue() != null
&& !header.getValue().isEmpty()) {
contentType = header.getValue().iterator().next();
break;
}
}
content = new ByteArrayContent(contentType, inputRequest.body());
}
Expand All @@ -89,7 +91,7 @@ private final HttpRequest convertRequest(
// We already set the Content-Type header via ByteArrayContent
// Content-Type is defined as a singleton field
// https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3-7
if (!header.getKey().equals("Content-Type")) {
if (!isContentType(header.getKey())) {
headers.set(header.getKey(), header.getValue());
}
}
Expand All @@ -108,6 +110,11 @@ private final HttpRequest convertRequest(
return request;
}

// Header names are case-insensitive, and Request does not guarantee a case-insensitive map
private static boolean isContentType(final String headerName) {
return "Content-Type".equalsIgnoreCase(headerName);
}

private final Response convertResponse(
final Request inputRequest, final HttpResponse inputResponse) throws IOException {
final HttpHeaders headers = inputResponse.getHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@

import feign.Feign;
import feign.Feign.Builder;
import feign.Headers;
import feign.Request;
import feign.RequestLine;
import feign.Response;
import feign.Util;
import feign.assertj.MockWebServerAssertions;
import feign.client.AbstractClientTest;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -82,4 +89,42 @@ void contentTypeHeaderGetsAddedOnce() throws Exception {
entry("Content-Length", Collections.singletonList("3")))
.hasMethod("POST");
}

@Test
void lowerCaseContentTypeHeaderIsSentOnce() throws Exception {
server.enqueue(new MockResponse());
LowerCaseContentTypeApi api =
newBuilder().target(LowerCaseContentTypeApi.class, "http://localhost:" + server.getPort());

api.post("foo");

RecordedRequest request = server.takeRequest();
assertThat(request.getHeaders().values("Content-Type")).containsExactly("text/plain");
}

@Test
void lowerCaseContentTypeIsUsedForTheBodyWithCaseSensitiveHeaders() throws Exception {
server.enqueue(new MockResponse());
Map<String, Collection<String>> headers = new HashMap<>();
headers.put("content-type", Collections.singletonList("application/json"));
Request request =
Request.create(
Request.HttpMethod.POST,
"http://localhost:" + server.getPort(),
headers,
"{}".getBytes(UTF_8),
UTF_8,
null);

new GoogleHttpClient().execute(request, new Request.Options());

assertThat(server.takeRequest().getHeaders().values("Content-Type"))
.containsExactly("application/json");
}

interface LowerCaseContentTypeApi {
@RequestLine("POST /")
@Headers("content-type: text/plain")
void post(String body);
}
}