Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow a single content-type header to be applied while using googlehttpclient #1737

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ private final HttpRequest convertRequest(final Request inputRequest,
// Setup headers
final HttpHeaders headers = new HttpHeaders();
for (final Map.Entry<String, Collection<String>> header : inputRequest.headers().entrySet()) {
headers.set(header.getKey(), header.getValue());
// 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")) {
headers.set(header.getKey(), header.getValue());
}
}
// Some servers don't do well with no Accept header
if (inputRequest.headers().get("Accept") == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

import feign.Feign;
import feign.Feign.Builder;
import feign.Response;
import feign.Util;
import feign.assertj.MockWebServerAssertions;
import feign.client.AbstractClientTest;
import okhttp3.mockwebserver.MockResponse;
import org.junit.Test;
import java.util.Collections;
import static feign.Util.UTF_8;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;

public class GoogleHttpClientTest extends AbstractClientTest {
Expand Down Expand Up @@ -55,4 +64,20 @@ public void canExceptCaseInsensitiveHeader() throws Exception {
assumeFalse("Google HTTP client client do not support gzip compression", false);
}

@Test
public void testContentTypeHeaderGetsAddedOnce() 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(UTF_8)));

MockWebServerAssertions.assertThat(server.takeRequest())
.hasHeaders(entry("Content-Type", Collections.singletonList("text/plain")),
entry("Content-Length", Collections.singletonList("3")))
.hasMethod("POST");
}
}