From 128bb958fc8be57388d43810f3e8e6323f385e08 Mon Sep 17 00:00:00 2001 From: Szymon Mucha Date: Wed, 7 Sep 2022 18:52:57 -0500 Subject: [PATCH] Only allow a single content-type header to be applied while using googlehttpclient (#1737) * Only allow a single content-type header to be applied while using google client * Run code formatter * Run formatter again via cmd line Co-authored-by: Marvin Froeder --- .../googlehttpclient/GoogleHttpClient.java | 7 +++++- .../GoogleHttpClientTest.java | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/googlehttpclient/src/main/java/feign/googlehttpclient/GoogleHttpClient.java b/googlehttpclient/src/main/java/feign/googlehttpclient/GoogleHttpClient.java index 2dbe52ab0b..068d7680b3 100644 --- a/googlehttpclient/src/main/java/feign/googlehttpclient/GoogleHttpClient.java +++ b/googlehttpclient/src/main/java/feign/googlehttpclient/GoogleHttpClient.java @@ -87,7 +87,12 @@ private final HttpRequest convertRequest(final Request inputRequest, // Setup headers final HttpHeaders headers = new HttpHeaders(); for (final Map.Entry> 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) { diff --git a/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java b/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java index 478f49fd1e..6207572dee 100644 --- a/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java +++ b/googlehttpclient/src/test/java/feign/googlehttpclient/GoogleHttpClientTest.java @@ -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 { @@ -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"); + } }