Skip to content

Commit

Permalink
Only allow a single content-type header to be applied while using goo…
Browse files Browse the repository at this point in the history
…glehttpclient (#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 <velo@users.noreply.github.com>
  • Loading branch information
skrzepto and velo authored Sep 7, 2022
1 parent 7be51e9 commit 128bb95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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");
}
}

0 comments on commit 128bb95

Please sign in to comment.